Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Application.java package grip4; import javax.swing.*; import java.awt.*; 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 public public public public public public void void void void void void void windowActivated(WindowEvent e) {} windowClosed(WindowEvent e) { System.exit(0); } windowClosing(WindowEvent e) {} windowDeactivated(WindowEvent e) {} windowDeiconified(WindowEvent e) {} windowIconified(WindowEvent e) {} 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 package grip4; import import import import java.awt.*; java.awt.geom.*; java.awt.image.*; 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 private private private } Color col; int x; int y; Image img; // // // // Current color On-screen position On-screen position Image representation CurveTool.java package grip4; import import import import import import java.awt.Color; java.awt.Graphics2D; java.awt.image.*; java.awt.geom.AffineTransform; java.awt.*; 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 private private private } Color float float Image col; // Current color x; // On-screen position y; // On-screen position img; // Image representation Transparency.java package grip4; import java.awt.*; 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); } }