
Class
Graphics
(from package
java.awt)
provides various methods for drawing text and shapes onto the screen.
Class
JPanel
(from package
javax.swing)
provides an area on which we can draw.
Click here for the codes.



The
keyword
extends
creates a so-called inheritance relationship.
-
The class from which
DrawPanel
inherits,
JPanel,
appears to the right of keyword
extends.
- In
this inheritance relationship,
JPanel
is called the
superclass
and
DrawPanel
is called the
subclass.
JPanel
has a
paintComponent
method, which the system calls every time it needs to display the
JPanel.
-
The first statement in every
paintComponent
method you create should always be
super.paintComponent(
g );
-
JPanel
methods
getWidth
and
getHeight
return the
JPanel’s
width and height, respectively.
-
Graphics
method
drawLine
draws a line between two points represented by its four arguments. The
first two are the
x-
and
y-coordinates
for one endpoint, and the last two arguments are the coordinates for the other
endpoint.
To
display the
DrawPanel
on the screen, place it in a window.
-
Create a window with an object of class
JFrame.
-
JFrame
method
setDefaultCloseOperation
with the argument
JFrame.EXIT_ON_CLOSE
indicates that the application should terminate when the user closes the
window.
-
JFrame’s
add
method
attaches the
DrawPanel
(or any other GUI component) to a
JFrame.
-
JFrame
method
setSize
takes two parameters that represent the width and height of the
JFrame,
respectively.
-
JFrame
method
setVisible
with the argument
true
displays the
JFrame.
-
When a
JFrame
is displayed, the
DrawPanel’s
paintComponent
method is implicitly called.
More topics:
a.
Empty Frame
b.
Rectangle Component
c.
Rectangle Applet
d. Ellipse Component
e. Line
Segment
f. Draw
a Face