// file Sketch.java // This program is a shell for a sketch program. // by Harriet Fell // fell@ccs.neu.edu // December 2001 //file Iguana.java import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.awt.image.*; import java.util.Random; public class Sketch extends JComponent implements MouseMotionListener, ActionListener { // The Sketch menu static JMenuBar sketchMenuBar = new JMenuBar(); // Mouse Points int mmX, mmY; // last mouse moved point coordinates int mdX, mdY; // last mouse dragged point coordinates // Current Attributes Color foreColor; // default color int lineWidth = 1; // default line width // Selected Actions static final int Rect = 1; static final int Oval = 2; static final int Line = 3; static final int Sqig = 4; int currentAct = 0; // no act - may become select // All the drawn squiggle pieces GeneralPath poly = new GeneralPath(); // All the Sketch Rectangles // This only allows for 20 rectangles. Make this structure dynamic. SketchRect theRectangles [] = new SketchRect[20]; int numRects = 0; // keeps track of how many rectangles were created. boolean rectDone = false; // helps the mouseListener find the right information public Sketch(){ // Listen for mouse addMouseMotionListener(this); // Add the menu bar setMenus(); // Init polygon position poly.moveTo(300, 300); // Set foreColor to black foreColor = new Color ( 0, 0, 0 ); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; // These are not currently used but might be useful. int wd = getSize().width; int ht = getSize().height; // These squiggles are all black with line width 3 g2.setPaint(Color.black); g2.setStroke(new BasicStroke(3)); g2.draw(poly); for (int i = 0; i < numRects; i++) { // Each rectangle is drawn in it's own color and line width g2.setPaint(theRectangles[i].theColor); // g2.setStroke(new BasicStroke(lineWidth)); // not yet implemented // so we use a default of 5 for the line width g2.setStroke(new BasicStroke(5)); g2.draw(theRectangles[i].theRect); // Fix this so it draws all rectangles. } } public void mouseDragged(MouseEvent e) { mdX = e.getX(); mdY = e.getY(); if (currentAct == Sqig) { poly.lineTo(e.getX(), e.getY()); repaint(); } else if (currentAct == Rect) { if (!rectDone) { rectDone = true; numRects++; } // allocate space for the new rectangle theRectangles[numRects - 1] = new SketchRect(); theRectangles[numRects - 1].theRect = new Rectangle2D.Float(mmX, mmY, mdX-mmX, mdY-mmY); theRectangles[numRects - 1].theColor = new Color(0, 0, 0); theRectangles[numRects - 1].theColor = foreColor; repaint(); } } public void mouseMoved(MouseEvent e) { mmX = e.getX(); mmY = e.getY(); if (currentAct == Sqig) { poly.moveTo(e.getX(), e.getY()); } else if (currentAct == Rect) { if (rectDone) { rectDone = false; } } } public void actionPerformed(ActionEvent e) { } public void setMenus() { JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); // Make "Quit" work properly JMenuItem quitItem = new JMenuItem("Quit"); quitItem.setMnemonic(KeyEvent.VK_Q); quitItem.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK)); quitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); fileMenu.add(quitItem); // Create Shape menu JMenu shapeMenu = new JMenu("Shape"); shapeMenu.setMnemonic(KeyEvent.VK_S); JMenuItem mItem = new JMenuItem("Rectangle"); shapeMenu.add(mItem); mItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentAct = Rect; System.out.println("Rectangle"); // numRects++; } }); shapeMenu.add(new JMenuItem("Ellipse")); // Doesn't yet do anything shapeMenu.add(new JMenuItem("Line")); // Doesn't yet do anything mItem = new JMenuItem("Squiggle"); shapeMenu.add(mItem); mItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentAct = Sqig; System.out.println("Squiggle"); } }); // Color Chooser button JButton colorButton = new JButton("Color..."); colorButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color c = JColorChooser.showDialog( Sketch.this, "Choose a color...", getBackground()); if (c != null) foreColor = c; } }); // Put the menus and color button in the menu bar. sketchMenuBar.add(fileMenu); sketchMenuBar.add(shapeMenu); sketchMenuBar.add(colorButton); } public static void main(String[] args){ JFrame f = new JFrame("JSketch"); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); c.add(new Sketch(), BorderLayout.CENTER); f.setSize(600, 622); f.setJMenuBar(sketchMenuBar); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setVisible(true); sketchMenuBar.setVisible(true); } }