Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application."— Presentation transcript:

1 © Copyright 1992-2004 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

2 © Copyright 1992-2004 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.

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

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

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

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

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

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 20.2 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

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

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

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

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

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 20.3 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.

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

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 20.3 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

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 20.4 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

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

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

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 20.5 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.

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

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

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

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

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 20.5 Using Inheritance to create the MyRectangle Class (Cont.) Figure 20.14 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

39  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; 11 12 // DrawJPanel for displaying rectangles 13 private DrawJPanel drawingJPanel; 14 15 // no-argument constructor 16 public ScreenSaver() 17 { 18 createUserInterface(); 19 } 20 21 // 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();

40  2004 Prentice Hall, Inc. All rights reserved. Outline 40 ScreenSaver.java (2 of 4) 26 27 // enable explicit positioning of GUI components 28 contentPane.setLayout( null ); 29 30 // 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( 36 37 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 } 44 45 } // end anonymous inner class 46 47 ); // end call to addActionListener 48

41  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 ); 53 54 // 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 58 59 } // end method createUserInterface 60 61 // clear drawingJPanel 62 private void clearJButtonActionPerformed( ActionEvent event ) 63 { 64 drawingJPanel.clear(); 65 66 } // end method clearJButtonActionPerformed 67 Creating and customizing the drawingJPanel Clearing the DrawJPanel

42  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 ); 73 74 } // end method main 75 76 } // end class ScreenSaver

43  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 ); 16 17 // set color of MyRectangle 18 setColor( colorValue ); 19 20 } // end constructor 21 Creating an instance variable Declaring class MyRectangle ’s constructor

44  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; 26 27 } // end method setFillColor 28 29 // get fillColor value 30 public Color getFillColor() 31 { 32 return fillColor; 33 34 } // end method getFillColor 35 36 // draw MyRectangle 37 public void draw( Graphics g ) 38 { 39 g.setColor( color ); 40 g.fillRect( x, y, width, height ); 41 42 } // end method draw 43 44 } // end class MyRectangle Get and set methods for Color instance variable Draw MyRectangle object on JPanel

45  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(): 14 15 // Timer object to generate events 16 private Timer drawTimer; 17 18 // ArrayList object to hold MyRectangle objects 19 private ArrayList rectangleArrayList = new ArrayList(); 20 21 // 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

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

47  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 ); 58 59 // create MyRectangle object and add it to rectangleArrayList 60 MyRectangle rectangle = new MyRectangle( x, y, width, height, 61 colors[ color ] ); 62 rectangleArrayList.add( rectangle ); 63 64 repaint(); 65 66 } // end method drawTimerActionPerformed 67 68 // 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

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


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

Similar presentations


Ads by Google