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

Slides:



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

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.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
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.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
© 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.
© 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.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
© 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 Outline 27.1 Test-Driving the Drawing Shapes Application.
© 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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
© 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.
Lesson 28: More on the GUI button, frame and actions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introducing, the JFrame Gives us a work area beside System.out.println.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
CSC 205 Programming II Lecture 5 AWT - I.
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 20.1 Test-Driving the Screen Saver Application 20.2 Inheritance Overview 20.3 Graphics Overview 20.4 Creating the Screen Saver Application 20.5 Using Inheritance to create the MyRectangle Class 20.6 Graphics in Java 20.7Completing the Screen Saver Application 20.8 Wrap-Up Tutorial 20 – Screen Saver Application Introducing Inheritance and Graphics

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Understand inheritance. –Form new classes quickly from existing classes using inheritance. –Use the Graphics object to draw rectangles on a JPanel.

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Screen Saver Application (Cont.) Running the completed application –Open a Command Prompt Change to ScreenSaver directory Type java ScreenSaver

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Screen Saver Application (Cont.) Figure 20.2 Screen Saver application after running for a few seconds.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Screen Saver Application (Cont.) Figure 20.3 Screen Saver application after clicking the Clear JButton.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inheritance Overview New class inherits members from an existing class –The pre-existing class is called the superclass Direct superclass Indirect superclass –The new class is called the subclass Class hierarchy –Defines the inheritance relationship “is-a” relationship “has-a” relationship

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inheritance Overview (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inheritance Overview (Cont.) Figure 20.5 Inheritance hierarchy for university CommunityMember s.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inheritance Overview (Cont.) Figure 20.6 Inheritance hierarchy for Shape s.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inheritance Overview (Cont.) extend keyword Overriding methods –New method supercedes the inherited method

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Overview Graphics terms –Pixel – a display monitor’s smallest unit of resolution –Coordinate pair – composed of an x-coordinate and a y-coordinate. –x-coordinate – the horizontal distance moving right from the upper-left corner. –y-coordinate – the vertical distance moving down from the upper-left corner. –The x-axis describes every horizontal coordinate –The y-axis describes every vertical coordinate.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Overview (Cont.) Figure 20.7 Java coordinate system. Units are measured in pixels.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics Overview (Cont.) The Graphics object –Allows you to draw pixels on the screen to represent text and other graphical objects Lines Ellipses Rectangles Other polygons

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating the Screen Saver Application When the application is executed Start the timer When the timer interval expires (every quarter second) Get random values for x, y, width and height Pick a random color Create a rectangle Place the rectangle in the ArrayList Repaint the DrawJPanel When paintComponent is called Create an Iterator for the ArrayList Use the Iterator to reference each Rectangle in the ArrayList and draw each rectangle When the user clicks the Clear Jbutton Clear the ArrayList Repaint the DrawJPanel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating the Screen Saver Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Creating the Screen Saver Application (Cont.)

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Inheritance to create the MyRectangle Class Figure 20.9 Declaring class MyRectangle. MyRectangle inherits from Rectangle extends keyword indicates that MyRectangle inherits from Rectangle.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Inheritance to create the MyRectangle Class (Cont.) Figure Instance variable to hold the fill color. Creating an instance variable for color

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Inheritance to create the MyRectangle Class (Cont.) Declaring class MyRectangle ’ s constructor Figure Calling the superclass’s constructor. superclass constructor call syntax super keyword, followed by the arguments for the superclass’ constructor

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Inheritance to create the MyRectangle Class (Cont.) Figure Calling method setFillColor to set the color of MyRectangle. Setting the rectangle’s color

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Inheritance to create the MyRectangle Class (Cont.) Figure Set and get methods for instance variable fillColor. Get and set methods for accessing and modifying the fillColor instance variable

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Inheritance to create the MyRectangle Class (Cont.) Figure Creating the drawMyRectangle method. setColor method set the color of the next object to be drawn. fillRect method draws a solid rectangle. Draw MyRectangle object on JPanel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java Figure Declaring class drawJPanel. DrawJPanel inherits from JPanel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Declaring instance variables. Creating a Random objectCreating a Timer objectCreating an ArrayList Array to hold MyRectangle colors

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Calling the superclass’s no-argument constructor. Call the constructor of superclass

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Starting the drawTimer object. Start the timer

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Creating random color and dimensions for the rectangle. Random dimensions for the MyRectangle

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Adding rectangle to rectangleArrayList. Add MyRectangle object to rectangleArrayList

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Calling the repaint method. Call repaint method to update the JPanel repaint method calls the paintComponent method

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Overriding the superclass’s paintComponent method. Call paintComponent method of superclass

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Iterating through rectangleArrayList. Draw MyRectangle object Iterate through rectangleArrayList

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Graphics in Java (Cont.) Figure Declaring the clearArray method. Creating a method to clear the rectangleArrayList

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Completing the Screen Saver Application Figure Creating a new drawJPanel object. Set up a new drawJPanel

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Completing the Screen Saver Application Figure Calling the clear method to clear drawingJPanel. Clearing the rectangleArrayList Save the changes to your code Compile and run in the Command Prompt

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Completing the Screen Saver Application (Cont.) Figure Completed Screen Saver application.

 2004 Prentice Hall, Inc. All rights reserved. Outline 39 ScreenSaver.java (1 of 4) 1 // Tutorial 20: ScreenSaver.java 2 // Application simulates screen saver by drawing random shapes. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 7 public class ScreenSaver extends JFrame 8 { 9 // JButton to clear drawingJPanel 10 private JButton clearJButton; // DrawJPanel for displaying rectangles 13 private DrawJPanel drawingJPanel; // no-argument constructor 16 public ScreenSaver() 17 { 18 createUserInterface(); 19 } // create and position GUI components; register event handlers 22 private void createUserInterface() 23 { 24 // get content pane for attaching GUI components 25 Container contentPane = getContentPane();

 2004 Prentice Hall, Inc. All rights reserved. Outline 40 ScreenSaver.java (2 of 4) // enable explicit positioning of GUI components 28 contentPane.setLayout( null ); // set up clearJButton 31 clearJButton = new JButton(); 32 clearJButton.setBounds( 189, 16, 72, 23 ); 33 clearJButton.setText( "Clear" ); 34 contentPane.add( clearJButton ); 35 clearJButton.addActionListener( new ActionListener() // anonymous inner class 38 { 39 // event handler called when clearJButton is pressed 40 public void actionPerformed( ActionEvent event ) 41 { 42 clearJButtonActionPerformed( event ); 43 } } // end anonymous inner class ); // end call to addActionListener 48

 2004 Prentice Hall, Inc. All rights reserved. Outline 41 ScreenSaver.java (3 of 4) 49 // set up drawingJPanel 50 drawingJPanel = new DrawJPanel(); 51 drawingJPanel.setBounds( 0, 40, 450, 450 ); 52 contentPane.add( drawingJPanel ); // set properties of application’s window 55 setTitle( "Screen Saver" ); // set title bar text 56 setSize( 450, 450 ); // set window size 57 setVisible( true ); // display window } // end method createUserInterface // clear drawingJPanel 62 private void clearJButtonActionPerformed( ActionEvent event ) 63 { 64 drawingJPanel.clear(); } // end method clearJButtonActionPerformed 67 Creating and customizing the drawingJPanel Clearing the DrawJPanel

 2004 Prentice Hall, Inc. All rights reserved. Outline 42 ScreenSaver.java (4 of 4) 68 // main method 69 public static void main( String[] args ) 70 { 71 ScreenSaver application = new ScreenSaver(); 72 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class ScreenSaver

 2004 Prentice Hall, Inc. All rights reserved. Outline 43 MyRectangle.java (1 of 2) 1 // Tutorial 20: MyRectangle.java 2 // This class defines the MyRectangle object 3 import java.awt.*; 4 5 public class MyRectangle extends Rectangle 6 { 7 // instance variable to hold fillColor of MyRectangle 8 private Color fillColor; 9 10 // constructor 11 public MyRectangle( int xValue, int yValue, int widthValue, 12 int heightValue, Color colorValue ) 13 { 14 // call constructor of superclass 15 super( xValue, yValue, widthValue, heightValue ); // set color of MyRectangle 18 setColor( colorValue ); } // end constructor 21 Creating an instance variable Declaring class MyRectangle ’s constructor

 2004 Prentice Hall, Inc. All rights reserved. Outline 44 MyRectangle.java (2 of 2) 22 // set fillColor value 23 public void setFillColor( Color colorValue ) 24 { 25 fillColor = colorValue; } // end method setFillColor // get fillColor value 30 public Color getFillColor() 31 { 32 return fillColor; } // end method getFillColor // draw MyRectangle 37 public void draw( Graphics g ) 38 { 39 g.setColor( color ); 40 g.fillRect( x, y, width, height ); } // end method draw } // end class MyRectangle Get and set methods for Color instance variable Draw MyRectangle object on JPanel

 2004 Prentice Hall, Inc. All rights reserved. Outline 45 DrawJPanel.java (1 of 4) 1 // Tutorial 20: DrawJPanel.java 2 // This class draws a random rectangle every.25 seconds. 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.ArrayList; 6 import java.util.Iterator; 7 import java.util.Random; 8 import javax.swing.*; 9 10 public class DrawJPanel extends JPanel 11 { 12 // Random object to create random numbers 13 private Random randomNumber = new random(): // Timer object to generate events 16 private Timer drawTimer; // ArrayList object to hold MyRectangle objects 19 private ArrayList rectangleArrayList = new ArrayList(); // array of possible MyRectangle colors 22 private Color[] colors = { Color.BLUE, Color.ORANGE, Color.PINK, 23 Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.BLACK, 24 Color.WHITE, Color.RED, Color.GREEN }; 25 Creating a class header Declaring instance variable randomNumber

 2004 Prentice Hall, Inc. All rights reserved. Outline 46 DrawJPanel.java (2 of 4) 26 // no-argument constructor 27 public DrawJPanel() 28 { 29 super(); drawTimer = new Timer( 250, new ActionListener() // anonymous inner class 34 { 35 // event handler called every 250 microseconds 36 public void actionPerformed( ActionEvent event ) 37 { 38 drawTimerActionPerformed(); 39 } } // end anonymous inner class ); // end call to new Timer drawTimer.start(); // start timer } // end constructor 48 Call constructor of superclass Calling method start

 2004 Prentice Hall, Inc. All rights reserved. Outline 47 DrawJPanel.java (3 of 4) 49 // create MyRectangle object and add it to rectangleArrayList 50 private void drawTimerActionPerformed() 51 { 52 // get random color and dimensions 53 int x = randomNumber.nextInt( 380 ); 54 int y = randomNumber.nextInt( 380 ); 55 int width = randomNumber.nextInt( 150 ); 56 int height = randomNumber.nextInt( 150 ); 57 int color = randomNumber.nextInt( 10 ); // create MyRectangle object and add it to rectangleArrayList 60 MyRectangle rectangle = new MyRectangle( x, y, width, height, 61 colors[ color ] ); 62 rectangleArrayList.add( rectangle ); repaint(); } // end method drawTimerActionPerformed // draw all rectangles 69 public void paintComponent( Graphics g ) 70 { 71 super.paintComponent( g ); 72 Add rectangle object to rectangleArrayList Call repaint method to update JPanel Call paintComponent method of superclass

 2004 Prentice Hall, Inc. All rights reserved. Outline 48 DrawJPanel.java (4 of 4) 73 // create iterator 74 Iterator rectangleIterator = rectangleArrayList.iterator(); MyRectangle currentRectangle; // create MyRectangle // iterate through ArrayList and draw all MyRectangles 79 while ( rectangleIterator.hasNext() ) 80 { 81 MyRectangle currentRectangle = 82 ( MyRectangle ) rectangleIterator.next(); currentRectangle.draw( g ); // draw rectangle 85 } } // end method paintComponent // clear rectangleArrayList 90 public void clear() 91 { 92 rectangleArrayList.clear(); // clear ArrayList repaint(); // repaint JPanel } // end method clear } // end class DrawJPanel Iterate through rectangleArrayList Draw MyRectangle object Call repaint method to update JPanel