This example shows how to draw circle in a swing application.
This example uses the CirclePanel component for drawing the circle.
How to run the example code?
Create a new java file called CircleMain.java and copy the code given below.
use javac to compile the class:
javac CircleMain.java
To run the example type following command on dos prompt:
java CircleMain
The above command will execute the example.
Here is the full code of the application:
/**
* Program: CircleMain.java - Uses the CirclePanel component
* @version 2002-00-00
* @author Mickey Mouse
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/////////////////////////////////////////////////////////////////// CircleMain
class CircleMain extends JFrame{
//================================================ instance variables
CirclePanel drawing = new CirclePanel(); // Note 1
//======================================================= constructor
CircleMain() {
//--- Get content pane, set layout, add components
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER); // Note 2
this.setTitle("Circles");
this.pack(); // finalize the layout
}//end constructor
//============================================================== main
public static void main(String[] args) {
JFrame myWindow = new CircleMain();
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}//end main
}//endclass CircleMain
content.add(new CirclePanel(), BorderLayout.CENTER);