© 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