Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 341, S. Tanimoto Java-PolyDraw- 1 A Java Case Study: An Applet for Drawing Polygons Motivation -- See a number of AWT features integrated. Get ideas."— Presentation transcript:

1 CSE 341, S. Tanimoto Java-PolyDraw- 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.

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

3 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.

4 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.

5 CSE 341, S. Tanimoto Java-PolyDraw- 5 PolyDraw.java (beginning) // PolyDraw.java // An applet for drawing polygons // S. Tanimoto, 1 February 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

6 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); }

7 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(); }

8 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) {}

9 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); } }

10 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 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;

11 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(); }

12 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) {... } }

13 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(); }

14 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(); }

15 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.


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

Similar presentations


Ads by Google