1 / 15

A Java Case Study: An Applet for Drawing Polygons

A Java Case Study: An Applet for Drawing Polygons. Motivation -- See a number of AWT features integrated. Get ideas for doing the J2 assignment. Motivation -- See a number of AWT features integrated. Get ideas for doing the J2 assignment. Features of PolyDraw.

deon
Download Presentation

A Java Case Study: An Applet for Drawing Polygons

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Java Case Study: An Applet for Drawing Polygons Motivation -- See a number of AWT features integrated. Get ideas for doing the J2 assignment. CSE 341, S. Tanimoto Java-PolyDraw-

  2. Motivation -- See a number of AWT features integrated. Get ideas for doing the J2 assignment. CSE 341, S. Tanimoto Java-PolyDraw-

  3. Features of PolyDraw 1. User can draw any number of polygons. 2. Polygons can be open or closed. 3. User can select groups of points, not necessarily belonging to a single polygon. Groups of vertices can be moved together or deleted all at once. 4. Polygons can be colored with any RGB color available. 5. Vertices can be constrained to lie on grid intersections. 6. The grid spacing is adjustable. CSE 341, S. Tanimoto Java-PolyDraw-

  4. List of Source Files PolyDraw.java -- the main applet/application class. DrawCanvas.java -- most of the drawing functionality is here. Controls.java -- sets up the control panel for the grid and for color selection. Valuator.java -- used to encapsulate scrollbar + textfield combinations. Toolbar.java -- subclasses Canvas to create an array of button-like tools. Polygon.java -- defines the polygon objects, including their paint method. DrawnObject.java -- a superclass of Polygon, included to allow later incorporation of other kinds of objects than polygons. PDPoint.java -- subclasses Point, adding facilities for being selected and displaying themselves in selected mode. CSE 341, S. Tanimoto Java-PolyDraw-

  5. PolyDraw.java (beginning) // PolyDraw.java // An applet for drawing polygons // S. Tanimoto, 1 February 2000. import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class PolyDraw extends Applet implements WindowListener { DrawCanvas theCanvas; Toolbar theToolbar; Controls theControls; static Frame theApp; // used only in Application mode CSE 341, S. Tanimoto Java-PolyDraw-

  6. PolyDraw.java (major component accessor functions and applet initialization) public Toolbar getToolbar() { return theToolbar; } public DrawCanvas getDrawCanvas() { return theCanvas; } public Controls getControls() { return theControls; } public void init() { setBackground(Color.gray); theControls = new Controls(this); theCanvas = new DrawCanvas(this); theToolbar = new Toolbar(this); theToolbar.setSize(200,150); setLayout(new BorderLayout()); add(theCanvas, BorderLayout.CENTER); add(theToolbar, BorderLayout.NORTH); add(theControls, BorderLayout.WEST); } CSE 341, S. Tanimoto Java-PolyDraw-

  7. PolyDraw.java(static main method for Application mode) public static void main(String [] args) { PolyDraw aPD = new PolyDraw(); theApp = new Frame(); theApp.setSize(800,600); theApp.setLayout(new BorderLayout()); aPD.init(); theApp.addWindowListener(aPD); theApp.add(aPD, BorderLayout.CENTER); theApp.show(); } CSE 341, S. Tanimoto Java-PolyDraw-

  8. PolyDraw.java(implementation of WindowListener interface) public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) {} public void windowOpened(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} CSE 341, S. Tanimoto Java-PolyDraw-

  9. PDPoint.java(a small class derived from the AWT class Point) // PDPoint.java-a special markable point for the PolyDraw program. import java.awt.*; public class PDPoint extends Point { public boolean selected; private static final int r = 5; PDPoint(int anX, int aY) { super(anX, aY); selected = false; } public void mark() { selected = true; } public void unmark() { selected = false; } public void paint(Graphics g) { if (selected) { g.fillOval(x - r, y - r, r+r, r+r); } } } CSE 341, S. Tanimoto Java-PolyDraw-

  10. DrawCanvas.java (beginning) // DrawCanvas.java // Most of the drawing functionality is provided here, // although a little bit is done in Toolbar.java and other files. // S. Tanimoto, 1 Feb. 2000. import java.awt.*; import java.awt.event.*; import java.util.*; public class DrawCanvas extends Canvas { PolyDraw pd; // reference to Applet Vector theDrawnObjects; DrawnObject currentObject; Controls ct; // reference to Controls panel boolean selecting, movingSelection; SelectionRect selectionRect; Vector selection; CSE 341, S. Tanimoto Java-PolyDraw-

  11. DrawCanvas.java (constructor) DrawCanvas(PolyDraw thePD) { pd = thePD; ct = pd.getControls(); theDrawnObjects = new Vector(); MouseHandlerForDrawCanvas mh = new MouseHandlerForDrawCanvas(); addMouseListener(mh); addMouseMotionListener(mh); selectionRect = new SelectionRect(); selecting = false; movingSelection = false; selection = new Vector(); } CSE 341, S. Tanimoto Java-PolyDraw-

  12. DrawCanvas.java (Mouse handling overview) class MouseHandlerForDrawCanvas extends MouseAdapter implements MouseMotionListener { private int savedMouseX, savedMouseY; public void mousePressed(MouseEvent e) { ... } public void mouseDragged (MouseEvent e) { ... } public void mouseMoved (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {} public void mouseClicked (MouseEvent e) { ... } public void mouseReleased(MouseEvent e) { ... } } CSE 341, S. Tanimoto Java-PolyDraw-

  13. DrawCanvas.java (mousePressed -- handling “Poly” mode) public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); if (ct.usingGrid()) { x = snapToGrid(x); y = snapToGrid(y); } String currentTool = pd.getToolbar().whichTool(); if (currentTool.equals("Poly")) { if (currentObject == null) return; Polygon thisPoly = (Polygon)currentObject; if (thisPoly.lastVertexNotAt(x, y)) thisPoly.addVertex(new PDPoint(x, y)); } else { ... } repaint(); } CSE 341, S. Tanimoto Java-PolyDraw-

  14. DrawCanvas.java (mousePressed -- handling “Edit” mode) public void mousePressed(MouseEvent e) { ... else if (currentTool.equals("Edit")) { if (hitSelected(selection, e.getX(), e.getY())) { savedMouseX = e.getX(); savedMouseY = e.getY(); movingSelection = true; } else { for (Enumeration vEnum = selection.elements(); vEnum.hasMoreElements();) { ((PDPoint)vEnum.nextElement()).unmark(); } selectionRect.setP1(e.getX(), e.getY()); selecting = true; movingSelection = false; } } repaint(); } CSE 341, S. Tanimoto Java-PolyDraw-

  15. Concluding Remarks • A drawing program needs: • a drawing area (e.g., a canvas) • a mouse handler for events in the drawing area. • a tool menu (e.g., a panel of buttons, a menu, or a canvas fixed up as a toolbar) • a mouse handler for events in the tool area • a class of drawn objects that can paint themselves and mark themselves as selected or unselected. • The object-oriented paradigm suggests associating the functionality for each component with that component. CSE 341, S. Tanimoto Java-PolyDraw-

More Related