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.

Slides:



Advertisements
Similar presentations
Chapter 14 Advanced GUI ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Advertisements

Handling Mouse Events Event-Driven Programming Approccio standard per programmare GUI In contrapposizione al polling offre: - Maggiore flessibilità - Minor.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
User Interfaces II GUI – Awt, Swing, Web
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Made with love, by Zachary Langley Applets The Graphics Presentation.
JAVA1 1. JFrame. JAVA2 JApplet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HerhalingAppletH10 extends JApplet implements.
Web Design & Development Lecture 19. Java Graphics 2.
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
CSE 341, S. Tanimoto Java-AWT- 1 A Brief Overview of the Abstract Windowing Toolkit (AWT) Motivation. Typical Frame setup. BorderLayout, FlowLayout, GridLayout.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
CS221 © 2007 Ray S. Babcock Menus, Toolbars, and a Mouse Appendix C.6-C.7.
Event Handling Events and Listeners Timers and Animation.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 16 Java Fundamentals Java2 Graphical User Interfaces.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUIs and Graphics CNS Outline  Introduction  Events  Components  Layout managers  Drawing  Introduction  Events  Components  Layout.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
12/5/00SEM107, Kamin & ReddyReview - 34 Events Event types Catching different event types Getting information from components and events Distinguishing.
Layout Managers Arranges and lays out the GUI components on a container.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Universidad Nacional de Colombia Facultad de Ingeniería Departamento de Sistemas ertificación en AVA.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Event Handling. The signals that a program receives from the operating system as a result of the actions are called events. A window based program is.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Events in JAVA Needed for Applets and GUI based programs. Applet uses GUI to interact with user. Most of the events are generated by Mouse, Keyboard.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Events (Chapter 11) Java Certification Study Group January 25, 1999 Mark Roth.
Laboratory Study November, Demonstrates Life Cycle of an Applet + Mouse Events + Scrolling.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
1 Java and AWT CPS 470 Spring 1998 Laura Campbell.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Handling User Events with Swing
Mouse Event Handling in Java (Review)
JAVA AWT.
Introduction to Computing Using Java
Swing II.
GUI Programming using Java - Mouse Events
Web Design & Development Lecture 12
A Brief Overview of the Abstract Windowing Toolkit (AWT)
Web Design & Development Lecture 13
A Brief Overview of the Abstract Windowing Toolkit (AWT)
A Brief Overview of the Abstract Windowing Toolkit (AWT)
Border Layout, using Panels, Introduction to PinBallGame
Presentation transcript:

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.

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