Application.java import javax.swing.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.event.*; import java.util.*; public class Application extends JFrame implements ChangeListener, WindowListener, MouseListener, MouseMotionListener { public Application() { super("application"); setlayout(new GridLayout(1, 1)); addwindowlistener(this); addmouselistener(this); addmousemotionlistener(this); pendialog = new JDialog(this, "Pen tool properties"); pendialog.setsize(300, 300); pendialog.setlayout(new BorderLayout()); colorchooser = new JColorChooser(); colorchooser.getselectionmodel().addchangelistener(this); pendialog.add(colorchooser, BorderLayout.CENTER); pendialog.setvisible(true); curvedialog = new JDialog(this, "Curve tool properties"); curvedialog.setsize(200, 200); curvedialog.setvisible(true); curvedialog.setlayout(new GridLayout(1, 1)); pen = new Pen(); pen.setposition(100, 100); pen.setcolor(colorchooser.getcolor()); ct = new CurveTool(); ct.setposition(110, 110); paths = new ArrayList(); pathcolors = new ArrayList(); public void statechanged(changeevent e) { Color newcolor = colorchooser.getcolor(); pen.setcolor(newcolor);
public void paint(graphics g) { super.paint(g); // Fetch back buffer's Graphics2D and draw into it Graphics2D g2 = (Graphics2D)getImGraphics(); g2.setcolor(color.white); g2.fillrect( 0, 0, getwidth(), getheight() ); pen.draw(g2); ct.draw(g2); for (int i = 0; i < paths.size(); i++) { g2.setcolor((color)pathcolors.get(i)); g2.draw((generalpath)paths.get(i)); // Now draw the back buffer g.drawimage(image, 0, 0, this); public void windowactivated(windowevent e) { public void windowclosed(windowevent e) { System.exit(0); public void windowclosing(windowevent e) { public void windowdeactivated(windowevent e) { public void windowdeiconified(windowevent e) { public void windowiconified(windowevent e) { public void windowopened(windowevent e) { public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { public void mousepressed(mouseevent e) { if (currentpath == null) { currentpath = new GeneralPath(); paths.add(currentpath); pathcolors.add(pen.getcolor()); currentpath.moveto(e.getx(), e.gety()); public void mousereleased(mouseevent e) { if (currentpath!= null) { currentpath = null; public void mousedragged(mouseevent e) { pen.setposition(e.getx(), e.gety()); repaint(); if (currentpath!= null) { currentpath.lineto(e.getx(), e.gety());
public void mousemoved(mouseevent e) { pen.setposition(e.getx(), e.gety()); repaint(); protected Graphics getimgraphics() { if (image == null) { Dimension dim = getsize(); image = createimage(dim.width, dim.height); img = image.getgraphics(); return img; public static void main(string[] args) { Application app = new Application(); app.setvisible(true); app.setsize(600, 600); private JDialog curvedialog; private JDialog pendialog; private JColorChooser colorchooser; private Pen pen; private CurveTool ct; private Image image = null; private Graphics img = null; private GeneralPath currentpath; private ArrayList paths; private ArrayList pathcolors;
Pen.java import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; public class Pen implements ImageObserver { public Pen() { ImageIcon iic = new ImageIcon("c:/pen.gif"); img = Transparency.makeColorTransparent(iic.getImage(), new Color(0).white); x = 0; y = 0; public void setcolor(color c) { col = c; public Color getcolor() { return col; public void setposition(int x, int y) { this.x = x; this.y = y; public void draw(graphics2d graphics) { graphics.drawimage(img, AffineTransform.getTranslateInstance(x, y), this); public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { return true; private Color col; private int x; private int y; private Image img; // Current color // On-screen position // On-screen position // Image representation
CurveTool.java import java.awt.color; import java.awt.graphics2d; import java.awt.image.*; import java.awt.geom.affinetransform; import javax.swing.imageicon; public class CurveTool implements ImageObserver { public CurveTool() { ImageIcon iic = new ImageIcon("c:/curve.gif"); img = Transparency.makeColorTransparent(iic.getImage(), new Color(0).white); x = 0; y = 0; public void setcolor(color c) { col = c; System.out.println(c); public Color getcolor() { return col; public void setposition(float x, float y) { this.x = x; this.y = y; public void draw(graphics2d graphics) { graphics.drawimage(img, AffineTransform.getTranslateInstance(x, y), this); public boolean imageupdate(image img, int infoflags, int x, int y, int width, int height) { return true; private Color col; // Current color private float x; // On-screen position private float y; // On-screen position private Image img; // Image representation
Transparency.java import java.awt.image.*; public class Transparency { public static Image makecolortransparent (Image im, final Color color) { ImageFilter filter = new RGBImageFilter() { // the color we are looking for... Alpha bits are set to opaque public int markerrgb = color.getrgb() 0xFF000000; public final int filterrgb(int x, int y, int rgb) { if ( ( rgb 0xFF000000 ) == markerrgb ) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb; else { // nothing to do return rgb; ; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip);