Java Reference
In-Depth Information
File:
its/SimpleGraphics/SimpleGraphicsPanel.java
1.
package
its.SimpleGraphics;
2.
3.
import
java.awt.*;
4.
import
javax.swing.JPanel;
5.
6.
public class
SimpleGraphicsPanel
extends
JPanel
7.
{
8.
public
SimpleGraphicsPanel()
9.
{
10.
this
.setBackground(Color.white);
11.
this
.setPreferredSize(
new
Dimension(300,300));
12.
}
13.
14.
public void
paintComponent(Graphics g)
15.
{
16.
super
.paintComponent(g);
17.
g.setColor(Color.black);
18.
g.drawLine(10,10,100,100);
19.
g.setColor(Color.red);
20.
g.drawLine(10,100,100,10);
21.
g.setColor(Color.green);
22.
g.drawOval(120,60,70,40);
23.
g.setColor(Color.yellow);
24.
g.fillOval(230,150,30,30);
25.
g.setColor(Color.red);
26.
g.fillOval(245,150,30,30);
27.
g.setColor(Color.black);
28.
g.fillOval(238,160,30,30);
29.
g.setColor(Color.cyan);
30.
g.fillOval(10,120,100,60);
31.
g.setColor(
this
.getBackground());
32.
g.fillOval(50,140,100,60);
33.
g.setColor(Color.blue);
34.
g.drawString("Swing is nice.",100,200);
35.
}
36.
}
The next two listings define class
SimpleGraphicsFrame
and
SimpleGraphics-
Driver
.Inthe constructor of
SimpleGraphicsFrame
a
SimpleGraphicsPanel
is
created and glued into the frames content pane. The
pack()
command ensures
that the frame is packed around the embedded panel and the latter is visible in
its full size. Class
SimpleGraphicsDriver
is the driver class. Figure 5.2 shows the
result.
Search WWH ::
Custom Search