/** * CircleWindow shows how to create a simple window with a green * circle in it. Following CircleWindow, note the definition for the * class DrawingPanel, which is used in CircleWindow. * * @author Dave Musicant */ import java.awt.*; import javax.swing.*; public class CircleWindow { public static void main(String[] args) { // Instantiate a JFrame object, which is a generic "shell" for // holding graphics objects. Set size, location, and instruct // the jframe that it should cause your program to exit when // the jframe is closed. JFrame jframe = new JFrame("Drawing demo"); jframe.setSize(250,300); jframe.setLocation(50,50); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Instantiate a DrawingPanel object, which is a subclass of // JPanel. It is on this drawingPanel object that we will do // our actual drawing (see the paintComponent method below). // Add the drawingPanel to the frame. DrawingPanel drawingPanel = new DrawingPanel(); jframe.add(drawingPanel); // Finally, make the window appear. jframe.setVisible(true); } } /* * DrawingPanel is a subclass of JPanel, where the paintComponent * method is overridden with instructions on how to draw a circle. The * Java Virtual Machine calls this paintComponent method whenever a * DrawingPanel object needs to be repainted, such as when the panel * first appears or when it is brought in front of other windows. * * @author Dave Musicant */ class DrawingPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.green); g.fillOval(100,10,100,100); } }