Deitel PowerPoint Slides
Download
Report
Transcript Deitel PowerPoint Slides
1
Tutorial 27 – Drawing Shapes
Application
Introduction to Polymorphism; an Expanded
Discussion of Graphics
Outline
27.1
27.2
27.3
27.4
27.5
Test-Driving the Drawing Shapes Application
Polymorphism
More Graphics Methods
Adding to the MyShape Inheritance Hierarchy
Wrap-Up
© Copyright 1992-2004 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 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
3
27.1
Test-Driving the Drawing Shapes
Application
Application Requirements
The principle of the elementary school from Tutorial 21 has asked you
to modify your Painter application. The user should now be able to
choose a color from a JColorChooser dialog and a type of shape
to be drawn from a JComboBox. The possible shapes include lines,
rectangles and ovals. The user should be able to click a mouse button
to create a shape and drag the mouse anywhere on the drawing area
to resize that shape. Multiple shapes can be drawn on the drawing
area, allowing the user to draw a picture by combining shapes.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
4
27.1
Test-Driving the Drawing Shapes
Application (Cont.)
• Run the Drawing Shapes application
– Type cd c:\Examples\Tutorial27\
CompletedApplication\DrawingShapes
– Type java DrawingShapes
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
5
27.1
Figure 27.1
Test-Driving the Drawing Shapes
Application (Cont.)
Running the completed Drawing Shapes application.
JComboBox for
JButton for
selecting shapes
selecting the
drawing color
Drawing area
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
6
27.1
Test-Driving the Drawing Shapes
Application (Cont.)
Figure 27.2
Running the completed Drawing Shapes application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
7
27.1
Test-Driving the Drawing Shapes
Application (Cont.)
Figure 27.3 Drawing a shape on the application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
8
27.2
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.
MyShape
MyLine
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
MyRectangle
MyOval
9
27.3 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 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
10
27.3 More Graphics Methods (Cont.)
Action
If Line is selected in JComboBox
Create a line
Component
Event
shapeJComboBox
User presses a mouse
button
currentShape (MyShape)
If Rectangle is selected in
JComboBox
Create a rectangle
If Oval is selected in JComboBox
Create an oval
shapeJComboBox
currentShape (MyShape)
shapeJComboBox
currentShape (MyShape)
Display a JColorChooser dialog
JColorChooser
User clicks the Color
JButton
Update the JButton’s color
Set the current shape color to the
selected color
Get the shape type selected
colorJButton
Set the current shape type to the
selected item
Resize the shape
Repaint the application
paintJPanel
paintJPanel
shapeJComboBox
currentShape (MyShape)
painterPaintJPanel
Figure 27.5 Drawing Shapes application ACE table.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
User selects an item in
the JComboBox
(shapeJComboBox)
User drags the mouse
11
27.3 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 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
12
27.3 More Graphics Methods (Cont.)
Figure 27.7
Declaring abstract
method draw
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Declaring abstract method draw.
13
27.3 More Graphics Methods (Cont.)
Graphics Method
Description
drawLine(x1, y1, x2, y2)
Draws a line from the point (x1, y1) to the point (x2,
y2).
Draws a rectangle of the specified width and height.
The top-left corner of the rectangle is at the point (x, y).
drawRect(x, y, width, height)
fillRect(x, y, width, height)
drawOval(x, y, width, height)
fillOval(x, y, width, height)
setColor(color)
Draws a solid rectangle of the specified width and
height. The top-left corner of the rectangle is at the
point (x, y).
Draws an oval inside a rectangular area of the specified
width and height. The top-left corner of the
rectangular area is at the point (x, y).
Draws a filled oval inside a rectangular area of the
specified width and height. The top-left corner of the
rectangular area is at the point (x, y).
Sets the color of the Graphics object to the specified
color.
Figure 27.8 Graphics methods that draw lines, rectangles and ovals.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
14
27.3 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 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
15
27.3 More Graphics Methods (Cont.)
Figure 27.10
Calculating the coordinates of the upper-left corner.
Determining the xand y-coordinates of
the upper left corner
• min method of Math class
– Returns minimum value of two numbers
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
16
27.3 More Graphics Methods (Cont.)
Figure 27.11
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 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
17
27.3 More Graphics Methods (Cont.)
Figure 27.12
Declaring a MyShape
instance variable
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Declaring a new MyShape object.
18
27.3 More Graphics Methods (Cont.)
Figure 27.13
Creating a
MyLine object
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Creating a new MyLine object.
19
27.3 More Graphics Methods (Cont.)
Figure 27.14
Creating a
MyRectangle
object
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Creating a new MyRectangle object.
20
27.3 More Graphics Methods (Cont.)
Figure 27.15
Adding the new MyShape to shapesArrayList.
Adding currentShape to
the shapesArrayList
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
21
27.3 More Graphics Methods (Cont.)
Figure 27.16
Setting
currentShape’s xand y-coordinates
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Resizing the MyShape object.
22
27.3 More Graphics Methods (Cont.)
Figure 27.17
Drawing the shapes in shapesArrayList polymorphically.
Using a while
statement to
draw each shape
• Cast items in shapesIterator to MyShape
• Drawn polymorphically
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
23
27.3 More Graphics Methods (Cont.)
Figure 27.18
Declaring a PaintJPanel instance variable.
Declaring a
PaintJPanel
instance variable
Figure 27.19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Creating a new PaintJPanel object.
24
27.3 More Graphics Methods (Cont.)
Figure 27.20
Setting the color for the next MyShape.
Setting the
PaintJPanel’s
color
Figure 27.21
Determining which
shape to draw
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Changing the type of shape drawn.
25
27.3 More Graphics Methods (Cont.)
Figure 27.22
Completed Drawing Shapes application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
27.4
Adding to the MyShape Inheritance
Hierarchy
Figure 27.23
Declaring class MyOval to extend MyShape.
Class MyOval extends
class MyShape
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
26
Adding to the MyShape Inheritance
Hierarchy (Cont.)
27.4
Figure 27.24
Declaring a constructor in class MyOval.
MyOval’s constructor,
which takes five
arguments
• Calls the superclass’s constructor
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
27
27.4
Adding to the MyShape Inheritance
Hierarchy (Cont.)
Figure 27.25
Implementing method draw to draw a MyOval object.
Implementing the
draw method
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
28
27.4
Adding to the MyShape Inheritance
Hierarchy (Cont.)
Figure 27.26
Adding the oval option to the String array shapeTypes.
Allow users to
select “Oval” from
shapeTypes
Figure 27.27
Drawing an oval if
the user has
selected this option
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Creating a MyOval object.
29
27.4
Adding to the MyShape Inheritance
Hierarchy (Cont.)
Figure 27.28
Completed Drawing Shapes application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
30
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;
12
13
// JComboBox to allow selection of a shape
14
private JComboBox shapeJComboBox;
15
16
// JButton to select the color
17
private JButton colorJButton;
18
19
// PaintJPanel for drawing shapes
20
private PaintJPanel painterPaintJPanel;
21
22
// array of shape types
23
private String[] shapeTypes = { "Line", "Rectangle", "Oval" };
24
31
Outline
DrawingShapes.java
(1 of 5)
PaintJPanel
instance variable
Array of
shape types
2003 Prentice Hall, Inc.
All rights reserved.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// no-argument constructor
public DrawingShapes()
{
createUserInterface();
}
32
Outline
DrawingShapes.java
(2 of 5)
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up controlsJPanel
controlsJPanel = new JPanel();
controlsJPanel.setBounds( 0, 0, 400, 40 );
controlsJPanel.setLayout( null );
contentPane.add( controlsJPanel );
// set up painterPaintJPanel
painterPaintJPanel = new PaintJPanel();
painterPaintJPanel.setBounds( 0, 40, 400, 340 );
painterPaintJPanel.setBackground( Color.WHITE );
contentPane.add( painterPaintJPanel );
2003 Prentice Hall, Inc.
All rights reserved.
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
33
// set up shapeJComboBox
shapeJComboBox = new JComboBox( shapeTypes );
shapeJComboBox.setBounds( 90, 2, 100, 24 );
controlsJPanel.add( shapeJComboBox );
shapeJComboBox.addActionListener(
Outline
DrawingShapes.java
(3 of 5)
new ActionListener() // anonymous inner class
{
// event method called when shapeJComboBox is selected
public void actionPerformed( ActionEvent event )
{
shapeJComboBoxActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up colorJButton
colorJButton = new JButton();
colorJButton.setBounds( 210, 2, 80, 24 );
colorJButton.setText( "Color" );
controlsJPanel.add( colorJButton );
colorJButton.addActionListener(
2003 Prentice Hall, Inc.
All rights reserved.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
34
new ActionListener() // anonymous inner class
{
// event handler called when colorJButton is pressed
public void actionPerformed( ActionEvent event )
{
colorJButtonActionPerformed( event );
}
Outline
DrawingShapes.java
(4 of 5)
} // end anonymous inner class
); // end call to addActionListener
// set properties of application’s window
setTitle( "Drawing Shapes" ); // set title bar string
setSize( 408, 407 );
// set window size
setVisible( true );
// display window
} // end method createUserInterface
// select a new color for the shape
private void colorJButtonActionPerformed( ActionEvent event )
{
Color selection = JColorChooser.showDialog( null,
"Select a Color", Color.BLACK );
2003 Prentice Hall, Inc.
All rights reserved.
102
if ( selection != null )
103
{
104
colorJButton.setBackground( selection );
105
painterPaintJPanel.setCurrentColor( selection );
106
}
107
108
} // end method colorJButtonActionPerformed
109
110
// set the selected shape in the painting panel
111
private void shapeJComboBoxActionPerformed( ActionEvent event )
112
{
113
painterPaintJPanel.setCurrentShapeType(
114
shapeJComboBox.getSelectedItem().toString() );
115
116
} // end method shapeJComboBoxActionPerformed
117
118
// main method
119
public static void main( String args[] )
120
{
121
DrawingShapes application = new DrawingShapes();
122
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
123
124
} // end method main
125
126 } // end class DrawingShapes
35
Outline
DrawingShapes.java
(5 of 5)
Setting the
color of the
PaintJPanel
Setting the
shape to draw
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Tutorial 27: PaintJPanel.java
// Panel allows user to create a shape.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
36
Outline
PaintJPanel.java
(1 of 6)
public class PaintJPanel extends JPanel {
// ArrayList to hold the shapes
private ArrayList shapesArrayList = new ArrayList();
// current shape that is being drawn
private MyShape currentShape;
// currently selected shape type
private String currentType = "Line";
// currently selected color
private Color currentColor = new Color( 204, 204, 204 );
// no-argument constructor
public PaintJPanel()
{
addMouseListener(
2003 Prentice Hall, Inc.
All rights reserved.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
37
new MouseAdapter() // anonymous inner class
{
// event handler called when mouse button is pressed
public void mousePressed( MouseEvent event )
{
paintJPanelMousePressed( event );
}
Outline
PaintJPanel.java
(2 of 6)
} // end anonymous inner class
); // end call to addMouseListener
addMouseMotionListener(
new MouseMotionAdapter() // anonymous inner class
{
// event handler called when the mouse is dragged
public void mouseDragged( MouseEvent event )
{
paintJPanelMouseDragged( event );
}
2003 Prentice Hall, Inc.
All rights reserved.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
} // end anonymous inner class
38
Outline
); // end call to addMouseMotionListener
} // end constructor
PaintJPanel.java
(3 of 6)
// change the current shape type
public void setCurrentShapeType( String shape )
{
currentType = shape;
} // end method setCurrentShapeType
// change the current color
public void setCurrentColor( Color shapeColor )
{
currentColor = shapeColor;
} // end method setCurrentColor
2003 Prentice Hall, Inc.
All rights reserved.
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// create a new shape
public void paintJPanelMousePressed( MouseEvent event )
{
// user selected line
if ( currentType.equals( "Line" ) )
{
currentShape = new MyLine( event.getX(), event.getY(),
event.getX(), event.getY(), currentColor );
}
// user selected rectangle
else if ( currentType.equals( "Rectangle" ) )
{
currentShape = new MyRectangle( event.getX(), event.getY(),
event.getX(), event.getY(), currentColor );
}
// user selected oval
else if ( currentType.equals( "Oval" ) )
{
currentShape = new MyOval( event.getX(), event.getY(),
event.getX(), event.getY(), currentColor );
}
shapesArrayList.add( currentShape );
39
Outline
PaintJPanel.java
(4 of 6)
Creating a MyLine
object and assigning
it to a MyShape
variable
Creating a
MyRectangle object
and assigning it to a
MyShape variable
Creating a MyOval
object and assigning
it to a MyShape
variable
} // end method paintJPanelMousePressed
Adding
currentShape to
shapesArrayList
2003 Prentice Hall, Inc.
All rights reserved.
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
40
// reset the second point for the shape
public void paintJPanelMouseDragged( MouseEvent event )
{
currentShape.setX2( event.getX() );
currentShape.setY2( event.getY() );
repaint();
Outline
PaintJPanel.java
(5 of 6)
Setting the
} // end method paintJPanelMouseDragged
// paint all the shapes
public void paintComponent( Graphics g )
{
super.paintComponent( g );
currentShape’s x-
and y-coordinates
MyShape nextShape;
Iterator shapesIterator = shapesArrayList.iterator();
2003 Prentice Hall, Inc.
All rights reserved.
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
}
119
120
} // end method paintComponent
121
122 } // end class PaintJPanel
41
Outline
PaintJPanel.java
(6 of 6)
Using a while
statement to draw
each shape
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
42
// Tutorial 27: MyShape.java
// Superclass for all shape objects.
import java.awt.*;
public abstract class MyShape extends Object
{
private int x1;
private int y1;
private int x2;
private int y2;
private Color color;
Outline
MyShape.java
(1 of 5)
Declaring MyShape
abstract
// constructor
public MyShape( int firstX, int firstY, int secondX, int secondY,
Color shapeColor )
{
setX1( firstX );
setY1( firstY );
setX2( secondX );
setY2( secondY );
setColor( shapeColor );
} // end constructor
2003 Prentice Hall, Inc.
All rights reserved.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// set x1 value
public void setX1( int x )
{
x1 = x;
} // end method setX1
43
Outline
MyShape.java
(2 of 5)
// get x1 value
public int getX1()
{
return x1;
} // end method getX1
// set Y1 value
public void setY1( int y )
{
y1 = y;
} // end method setY1
2003 Prentice Hall, Inc.
All rights reserved.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// get Y1 value
public int getY1()
{
return y1;
} // end method getY1
44
Outline
MyShape.java
(3 of 5)
// set x2 value
public void setX2( int x )
{
x2 = x;
} // end method setX2
// get x2 value
public int getX2()
{
return x2;
} // end method getX2
2003 Prentice Hall, Inc.
All rights reserved.
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// set y2 value
public void setY2( int y )
{
y2 = y;
} // end method setY2
45
Outline
MyShape.java
(4 of 5)
// get y2 value
public int getY2()
{
return y2;
} // end method getY2
// set color value
public void setColor( Color c )
{
color = c;
} // end method setColor
2003 Prentice Hall, Inc.
All rights reserved.
88
// get color value
89
public final Color getColor()
90
{
91
return color;
92
93
} // end method getColor
94
95
// empty draw method
96
public void draw( Graphics g );
97
98 } // end class MyShape
46
Outline
MyShape.java
(5 of 5)
Declaring
abstract method
draw
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Tutorial 27: MyLine.java
// Class that declares a line object.
import java.awt.*;
public class MyLine extends MyShape
{
// constructor
public MyLine( int firstX, int firstY, int secondX, int secondY,
Color shapeColor )
{
super( firstX, firstY, secondX, secondY, shapeColor );
47
Outline
MyLine.java
(1 of 1)
} // end constructor
// draw a line
public void draw( Graphics g )
{
g.setColor( getColor() );
g.drawLine( getX1(), getY1(), getX2(), getY2() );
Implementing the
abstract draw
method from
MyShape
} // end method draw
} // end class MyLine
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Tutorial 27: MyRectangle.java
// Class that declares a rectangle object.
import java.awt.*;
public class MyRectangle extends MyShape
{
// constructor
public MyRectangle( int firstX, int firstY, int secondX,
int secondY, Color shapeColor )
{
super( firstX, firstY, secondX, secondY, shapeColor );
48
Outline
MyRectangle.java
(1 of 2)
} // end constructor
// draw a rectangle
public void draw( Graphics g )
{
int upperLeftX = Math.min( getX1(), getX2() );
int upperLeftY = Math.min( getY1(), getY2() );
int width = Math.abs( getX1() - getX2() );
int height = Math.abs( getY1() - getY2() );
Implementing the abstract
draw method from MyShape
Calculating the x- and ycoordinates, width and
height of the rectangle
2003 Prentice Hall, Inc.
All rights reserved.
23
24
25
26
27
28
g.setColor( getColor() );
g.fillRect( upperLeftX, upperLeftY, width, height );
} // end method draw
49
Outline
MyRectangle.java
(2 of 2)
} // end class MyRectangle
Drawing a rectangle
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Tutorial 27: MyOval.java
// Class that declares an oval object.
import java.awt.*;
public class MyOval extends MyShape
{
// constructor
public MyOval( int firstX, int firstY, int secondX, int secondY,
Color shapeColor )
{
super( firstX, firstY, secondX, secondY, shapeColor );
50
Outline
MyOval.java
(1 of 2)
Extending class
MyShape
MyOval’s constructor
takes five arguments
} // end constructor
// draw an oval
public void draw( Graphics g )
{
int upperLeftX = Math.min( getX1(), getX2() );
int upperLeftY = Math.min( getY1(), getY2() );
int width = Math.abs( getX1() - getX2() );
int height = Math.abs( getY1() - getY2() );
Implementing the
abstract draw method
from MyShape
Calculating the xand y-coordinates,
width and height of
the rectangle
2003 Prentice Hall, Inc.
All rights reserved.
22
23
24
25
26
27
28
51
g.setColor( getColor() );
g.fillOval( upperLeftX, upperLeftY, width, height );
} // end method draw
Outline
MyOval.java
(2 of 2)
} // end class MyOval
2003 Prentice Hall, Inc.
All rights reserved.