用java画圆圈的程式问题
这是我书上的程式,我想问下面的三个程式中, 哪一个才是画圈, 还是3个要一起用才能运行???如果三个要一起用,如何写成一个程式。 我刚接触java,一窍不通, 所以请高手指点//*****************************************************************************
// CirclePanel.java Programming with Alice and Java
//
// Represents the drawing panel in the DrawCircles program.
//*****************************************************************************
import java.awt.*;
import javax.swing.JPanel;
public class CirclePanel extends JPanel
{
private Circle circle1, circle2, circle3;
//--------------------------------------------------------------------------
// Creates three Circle objects and sets panel characteristics.
//--------------------------------------------------------------------------
public CirclePanel()
{
circle1 = new Circle(150, 200, 100, Color.red);
circle2 = new Circle(200, 50, 35, Color.yellow);
circle3 = new Circle(285, 150, 60, Color.green);
setBackground(Color.black);
setPreferredSize(new Dimension(400, 350));
}
//--------------------------------------------------------------------------
// Draws three circles on the panel.
//--------------------------------------------------------------------------
public void paintComponent(Graphics gc)
{
super.paintComponent(gc);
circle1.drawFilled(gc);
circle2.drawFilled(gc);
circle3.drawFilled(gc);
}
}
//*****************************************************************************
// DrawCircles.java Programming with Alice and Java
//
// Demonstrates the use of a frame containing a panel on which shapes can
// be drawn.
//*****************************************************************************
import javax.swing.JFrame;
public class DrawCircles
{
//--------------------------------------------------------------------------
// Creates and displays the application frame, which contains the drawing
// panel.
//--------------------------------------------------------------------------
public static void main(String[] args)
{
CirclePanel panel = new CirclePanel();
JFrame frame = new JFrame("Draw Circles");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
//*****************************************************************************
// Circle.java Programming with Alice and Java
//
// Represents a circle with a particular size, color, and location. The
// circle can be drawn filled or unfilled.
//*****************************************************************************
import java.awt.*;
public class Circle
{
private int radius;
private Color color;
private int x, y; // the circle's center point
//--------------------------------------------------------------------------
// Sets up the circle with the specified location, size, and color.
//--------------------------------------------------------------------------
public Circle(int xCenter, int yCenter, int size, Color circleColor)
{
x = xCenter;
y = yCenter;
radius = size;
color = circleColor;
}
//--------------------------------------------------------------------------
// Draws the circle, filled, in the specified graphics context.
//--------------------------------------------------------------------------
public void drawFilled(Graphics gc)
{
gc.setColor(color);
gc.fillOval(x-radius, y-radius, radius*2, radius*2);
}
//--------------------------------------------------------------------------
// Draws the circle, unfilled (outline only), in the specified graphics
// context.
//--------------------------------------------------------------------------
public void drawUnfilled(Graphics gc)
{
gc.setColor(color);
gc.drawOval(x-radius, y-radius, radius*2, radius*2);
}
}