© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.

Slides:



Advertisements
Similar presentations
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.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Web Design & Development Lecture 19. Java Graphics 2.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Handling Events and Listeners Timers and Animation.
CS221 © 2007 Ray S. Babcock Event-Driven Programming Appendix C.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Java Swing One of the most important features of Java is its ability to draw graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
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.
 Figure illustrates a hierarchy containing many event classes from the package java.awt.event.  Used with both AWT and Swing components.  Additional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
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.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Introduction to Graphics
A First Look at GUI Applications
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application 27.2 Polymorphism 27.3 More Graphics Methods 27.4 Adding to the MyShape Inheritance Hierarchy 27.5 Wrap-Up Tutorial 27 – Drawing Shapes Application Introduction to Polymorphism; an Expanded Discussion of Graphics

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use polymorphism to create an application that processes related objects as though they are the same. –Use additional Graphics methods such as drawLine. –Create an application that allows users to draw shapes.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Drawing Shapes Application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Drawing Shapes Application (Cont.) Run the Drawing Shapes application – Type cd c:\Examples\Tutorial27\ CompletedApplication\DrawingShapes – Type java DrawingShapes

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Drawing Shapes Application (Cont.) JComboBox for selecting shapes JButton for selecting the drawing color Drawing area Figure 27.1 Running the completed Drawing Shapes application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Drawing Shapes Application (Cont.) Figure 27.2 Running the completed Drawing Shapes application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Drawing Shapes Application (Cont.) Figure 27.3 Drawing a shape on the application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Polymorphism Polymorphism – allows you to write programs that handle a wide variety of classes related by inheritance – “program in the general” Figure 27.4 UML class diagram for the inheritance hierarchy in the Drawing Shapes application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods When the user presses the mouse button If Line is selected in the JComboBox Create a line If Rectangle is selected in the JComboBox Create a rectangle If Oval is selected in the JComboBox Create an oval When the user clicks the Color JButton Display a JColorChooser dialog Update the JButton’s color with the selected color Set the current shape color to the selected color When the user selects an item in the JComboBox Get the shape type selected Set the current shape type to the selected item When the user drags the mouse Resize the shape Repaint the application

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure 27.6 Declaring the MyShape class abstract. Declaring MyShape abstract Abstract class Cannot be instantiated Concrete class Can be instantiated

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure 27.7 Declaring abstract method draw. Declaring abstract method draw

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure 27.9 Implementing the draw method in class MyLine. Implementing the draw method to draw a line drawLine method of Graphics – Draws a line from one point to another – Takes four int values – First two are the x and y values of the start point – Second two are the x and y values of the end point

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Calculating the coordinates of the upper-left corner. Determining the x- and y-coordinates of the upper left corner min method of Math class – Returns minimum value of two numbers

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Calculating the width and height and drawing the rectangle. Calculating the width and height of the rectangle abs method of Math class – Returns absolute value of a number

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Declaring a new MyShape object. Declaring a MyShape instance variable

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Creating a new MyLine object. Creating a MyLine object

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Creating a new MyRectangle object. Creating a MyRectangle object

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Adding the new MyShape to shapesArrayList. Adding currentShape to the shapesArrayList

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Resizing the MyShape object. Setting currentShape ’s x- and y-coordinates

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Cast items in shapesIterator to MyShape Drawn polymorphically Figure Drawing the shapes in shapesArrayList polymorphically. Using a while statement to draw each shape

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Declaring a PaintJPanel instance variable. Declaring a PaintJPanel instance variable Figure Creating a new PaintJPanel object.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Setting the color for the next MyShape. Setting the PaintJPanel’s color Figure Changing the type of shape drawn. Determining which shape to draw

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved More Graphics Methods (Cont.) Figure Completed Drawing Shapes application.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding to the MyShape Inheritance Hierarchy Figure Declaring class MyOval to extend MyShape. Class MyOval extends class MyShape

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding to the MyShape Inheritance Hierarchy (Cont.) Calls the superclass’s constructor Figure Declaring a constructor in class MyOval. MyOval ’s constructor, which takes five arguments

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding to the MyShape Inheritance Hierarchy (Cont.) Figure Implementing method draw to draw a MyOval object. Implementing the draw method

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding to the MyShape Inheritance Hierarchy (Cont.) Figure Adding the oval option to the String array shapeTypes. Allow users to select “Oval” from shapeTypes Figure Creating a MyOval object. Drawing an oval if the user has selected this option

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Adding to the MyShape Inheritance Hierarchy (Cont.) Figure Completed Drawing Shapes application.

 2003 Prentice Hall, Inc. All rights reserved. Outline 31 DrawingShapes.java (1 of 5) 1 // Tutorial 27: DrawingShapes.java 2 // Application allows user to draw lines, rectangles and ovals and 3 // choose the color of the drawn shape. 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.*; 7 8 public class DrawingShapes extends JFrame 9 { 10 // JPanel for the shape and color controls 11 private JPanel controlsJPanel; // JComboBox to allow selection of a shape 14 private JComboBox shapeJComboBox; // JButton to select the color 17 private JButton colorJButton; // PaintJPanel for drawing shapes 20 private PaintJPanel painterPaintJPanel; // array of shape types 23 private String[] shapeTypes = { "Line", "Rectangle", "Oval" }; 24 PaintJPanel instance variable Array of shape types

 2003 Prentice Hall, Inc. All rights reserved. Outline 32 DrawingShapes.java (2 of 5) 25 // no-argument constructor 26 public DrawingShapes() 27 { 28 createUserInterface(); 29 } // create and position GUI components; register event handlers 32 private void createUserInterface() 33 { 34 // get content pane for attaching GUI components 35 Container contentPane = getContentPane(); // enable explicit positioning of GUI components 38 contentPane.setLayout( null ); // set up controlsJPanel 41 controlsJPanel = new JPanel(); 42 controlsJPanel.setBounds( 0, 0, 400, 40 ); 43 controlsJPanel.setLayout( null ); 44 contentPane.add( controlsJPanel ); // set up painterPaintJPanel 47 painterPaintJPanel = new PaintJPanel(); 48 painterPaintJPanel.setBounds( 0, 40, 400, 340 ); 49 painterPaintJPanel.setBackground( Color.WHITE ); 50 contentPane.add( painterPaintJPanel );

 2003 Prentice Hall, Inc. All rights reserved. Outline 33 DrawingShapes.java (3 of 5) // set up shapeJComboBox 53 shapeJComboBox = new JComboBox( shapeTypes ); 54 shapeJComboBox.setBounds( 90, 2, 100, 24 ); 55 controlsJPanel.add( shapeJComboBox ); 56 shapeJComboBox.addActionListener( new ActionListener() // anonymous inner class 59 { 60 // event method called when shapeJComboBox is selected 61 public void actionPerformed( ActionEvent event ) 62 { 63 shapeJComboBoxActionPerformed( event ); 64 } } // end anonymous inner class ); // end call to addActionListener // set up colorJButton 71 colorJButton = new JButton(); 72 colorJButton.setBounds( 210, 2, 80, 24 ); 73 colorJButton.setText( "Color" ); 74 controlsJPanel.add( colorJButton ); 75 colorJButton.addActionListener(

 2003 Prentice Hall, Inc. All rights reserved. Outline 34 DrawingShapes.java (4 of 5) new ActionListener() // anonymous inner class 78 { 79 // event handler called when colorJButton is pressed 80 public void actionPerformed( ActionEvent event ) 81 { 82 colorJButtonActionPerformed( event ); 83 } } // end anonymous inner class ); // end call to addActionListener // set properties of application’s window 90 setTitle( "Drawing Shapes" ); // set title bar string 91 setSize( 408, 407 ); // set window size 92 setVisible( true ); // display window } // end method createUserInterface // select a new color for the shape 97 private void colorJButtonActionPerformed( ActionEvent event ) 98 { 99 Color selection = JColorChooser.showDialog( null, 100 "Select a Color", Color.BLACK ); 101

 2003 Prentice Hall, Inc. All rights reserved. Outline 35 DrawingShapes.java (5 of 5) 102 if ( selection != null ) 103 { 104 colorJButton.setBackground( selection ); 105 painterPaintJPanel.setCurrentColor( selection ); 106 } } // end method colorJButtonActionPerformed // set the selected shape in the painting panel 111 private void shapeJComboBoxActionPerformed( ActionEvent event ) 112 { 113 painterPaintJPanel.setCurrentShapeType( 114 shapeJComboBox.getSelectedItem().toString() ); } // end method shapeJComboBoxActionPerformed // main method 119 public static void main( String args[] ) 120 { 121 DrawingShapes application = new DrawingShapes(); 122 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class DrawingShapes Setting the color of the PaintJPanel Setting the shape to draw

 2003 Prentice Hall, Inc. All rights reserved. Outline 36 PaintJPanel.java (1 of 6) 1 // Tutorial 27: PaintJPanel.java 2 // Panel allows user to create a shape. 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 6 import javax.swing.*; 7 8 public class PaintJPanel extends JPanel { 9 10 // ArrayList to hold the shapes 11 private ArrayList shapesArrayList = new ArrayList(); // current shape that is being drawn 14 private MyShape currentShape; // currently selected shape type 17 private String currentType = "Line"; // currently selected color 20 private Color currentColor = new Color( 204, 204, 204 ); // no-argument constructor 23 public PaintJPanel() 24 { 25 addMouseListener(

 2003 Prentice Hall, Inc. All rights reserved. Outline 37 PaintJPanel.java (2 of 6) new MouseAdapter() // anonymous inner class 28 { 29 // event handler called when mouse button is pressed 30 public void mousePressed( MouseEvent event ) 31 { 32 paintJPanelMousePressed( event ); 33 } } // end anonymous inner class ); // end call to addMouseListener addMouseMotionListener( new MouseMotionAdapter() // anonymous inner class 42 { 43 // event handler called when the mouse is dragged 44 public void mouseDragged( MouseEvent event ) 45 { 46 paintJPanelMouseDragged( event ); 47 } 48

 2003 Prentice Hall, Inc. All rights reserved. Outline 38 PaintJPanel.java (3 of 6) 49 } // end anonymous inner class ); // end call to addMouseMotionListener } // end constructor // change the current shape type 56 public void setCurrentShapeType( String shape ) 57 { 58 currentType = shape; } // end method setCurrentShapeType // change the current color 63 public void setCurrentColor( Color shapeColor ) 64 { 65 currentColor = shapeColor; } // end method setCurrentColor 68

 2003 Prentice Hall, Inc. All rights reserved. Outline 39 PaintJPanel.java (4 of 6) 69 // create a new shape 70 public void paintJPanelMousePressed( MouseEvent event ) 71 { 72 // user selected line 73 if ( currentType.equals( "Line" ) ) 74 { 75 currentShape = new MyLine( event.getX(), event.getY(), 76 event.getX(), event.getY(), currentColor ); 77 } 78 // user selected rectangle 79 else if ( currentType.equals( "Rectangle" ) ) 80 { 81 currentShape = new MyRectangle( event.getX(), event.getY(), 82 event.getX(), event.getY(), currentColor ); 83 } 84 // user selected oval 85 else if ( currentType.equals( "Oval" ) ) 86 { 87 currentShape = new MyOval( event.getX(), event.getY(), 88 event.getX(), event.getY(), currentColor ); 89 } shapesArrayList.add( currentShape ); } // end method paintJPanelMousePressed Creating a MyLine object and assigning it to a MyShape variable Creating a MyOval object and assigning it to a MyShape variable Creating a MyRectangle object and assigning it to a MyShape variable Adding currentShape to shapesArrayList

 2003 Prentice Hall, Inc. All rights reserved. Outline 40 PaintJPanel.java (5 of 6) // reset the second point for the shape 96 public void paintJPanelMouseDragged( MouseEvent event ) 97 { 98 currentShape.setX2( event.getX() ); 99 currentShape.setY2( event.getY() ); 100 repaint(); } // end method paintJPanelMouseDragged // paint all the shapes 105 public void paintComponent( Graphics g ) 106 { 107 super.paintComponent( g ); MyShape nextShape; 110 Iterator shapesIterator = shapesArrayList.iterator(); 111 Setting the currentShape ’ s x- and y-coordinates

 2003 Prentice Hall, Inc. All rights reserved. Outline 41 PaintJPanel.java (6 of 6) 112 // iterate through all the shapes 113 while ( shapesIterator.hasNext() ) 114 { 115 // draw each shape 116 nextShape = ( MyShape ) shapesIterator.next(); 117 nextShape.draw( g ); 118 } } // end method paintComponent } // end class PaintJPanel Using a while statement to draw each shape

 2003 Prentice Hall, Inc. All rights reserved. Outline 42 MyShape.java (1 of 5) 1 // Tutorial 27: MyShape.java 2 // Superclass for all shape objects. 3 import java.awt.*; 4 5 public abstract class MyShape extends Object 6 { 7 private int x1; 8 private int y1; 9 private int x2; 10 private int y2; 11 private Color color; // constructor 14 public MyShape( int firstX, int firstY, int secondX, int secondY, 15 Color shapeColor ) 16 { 17 setX1( firstX ); 18 setY1( firstY ); 19 setX2( secondX ); 20 setY2( secondY ); 21 setColor( shapeColor ); } // end constructor 24 Declaring MyShape abstract

 2003 Prentice Hall, Inc. All rights reserved. Outline 43 MyShape.java (2 of 5) 25 // set x1 value 26 public void setX1( int x ) 27 { 28 x1 = x; } // end method setX // get x1 value 33 public int getX1() 34 { 35 return x1; } // end method getX // set Y1 value 40 public void setY1( int y ) 41 { 42 y1 = y; } // end method setY1 45

 2003 Prentice Hall, Inc. All rights reserved. Outline 44 MyShape.java (3 of 5) 46 // get Y1 value 47 public int getY1() 48 { 49 return y1; } // end method getY // set x2 value 54 public void setX2( int x ) 55 { 56 x2 = x; } // end method setX // get x2 value 61 public int getX2() 62 { 63 return x2; } // end method getX2 66

 2003 Prentice Hall, Inc. All rights reserved. Outline 45 MyShape.java (4 of 5) 67 // set y2 value 68 public void setY2( int y ) 69 { 70 y2 = y; } // end method setY // get y2 value 75 public int getY2() 76 { 77 return y2; } // end method getY // set color value 82 public void setColor( Color c ) 83 { 84 color = c; } // end method setColor 87

 2003 Prentice Hall, Inc. All rights reserved. Outline 46 MyShape.java (5 of 5) 88 // get color value 89 public final Color getColor() 90 { 91 return color; } // end method getColor // empty draw method 96 public void draw( Graphics g ); } // end class MyShape Declaring abstract method draw

 2003 Prentice Hall, Inc. All rights reserved. Outline 47 MyLine.java (1 of 1) 1 // Tutorial 27: MyLine.java 2 // Class that declares a line object. 3 import java.awt.*; 4 5 public class MyLine extends MyShape 6 { 7 // constructor 8 public MyLine( int firstX, int firstY, int secondX, int secondY, 9 Color shapeColor ) 10 { 11 super( firstX, firstY, secondX, secondY, shapeColor ); } // end constructor // draw a line 16 public void draw( Graphics g ) 17 { 18 g.setColor( getColor() ); 19 g.drawLine( getX1(), getY1(), getX2(), getY2() ); } // end method draw } // end class MyLine Implementing the abstract draw method from MyShape

 2003 Prentice Hall, Inc. All rights reserved. Outline 48 MyRectangle.java (1 of 2) 1 // Tutorial 27: MyRectangle.java 2 // Class that declares a rectangle object. 3 import java.awt.*; 4 5 public class MyRectangle extends MyShape 6 { 7 // constructor 8 public MyRectangle( int firstX, int firstY, int secondX, 9 int secondY, Color shapeColor ) 10 { 11 super( firstX, firstY, secondX, secondY, shapeColor ); } // end constructor // draw a rectangle 16 public void draw( Graphics g ) 17 { 18 int upperLeftX = Math.min( getX1(), getX2() ); 19 int upperLeftY = Math.min( getY1(), getY2() ); 20 int width = Math.abs( getX1() - getX2() ); 21 int height = Math.abs( getY1() - getY2() ); 22 Implementing the abstract draw method from MyShape Calculating the x- and y- coordinates, width and height of the rectangle

 2003 Prentice Hall, Inc. All rights reserved. Outline 49 MyRectangle.java (2 of 2) 23 g.setColor( getColor() ); 24 g.fillRect( upperLeftX, upperLeftY, width, height ); } // end method draw } // end class MyRectangle Drawing a rectangle

 2003 Prentice Hall, Inc. All rights reserved. Outline 50 MyOval.java (1 of 2) 1 // Tutorial 27: MyOval.java 2 // Class that declares an oval object. 3 import java.awt.*; 4 5 public class MyOval extends MyShape 6 { 7 // constructor 8 public MyOval( int firstX, int firstY, int secondX, int secondY, 9 Color shapeColor ) 10 { 11 super( firstX, firstY, secondX, secondY, shapeColor ); } // end constructor // draw an oval 16 public void draw( Graphics g ) 17 { 18 int upperLeftX = Math.min( getX1(), getX2() ); 19 int upperLeftY = Math.min( getY1(), getY2() ); 20 int width = Math.abs( getX1() - getX2() ); 21 int height = Math.abs( getY1() - getY2() ); Extending class MyShape MyOval ’ s constructor takes five arguments Implementing the abstract draw method from MyShape Calculating the x- and y-coordinates, width and height of the rectangle

 2003 Prentice Hall, Inc. All rights reserved. Outline 51 MyOval.java (2 of 2) g.setColor( getColor() ); 24 g.fillOval( upperLeftX, upperLeftY, width, height ); } // end method draw } // end class MyOval