Home Java Java-tips Flow Methods Example - CircleMain.java

Ask Questions?

View Latest Questions



Example - CircleMain.java
Posted on: July 26, 2006 at 12:00 AM
Here is the program which uses the CirclePanel class .

Java Notes

Example - CircleMain.java

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:

Here is the program which uses the CirclePanel class (see Example - CirclePanel.java).
 * 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.pack();       // finalize the layout
    }//end constructor
    //============================================================== main
    public static void main(String[] args) {
        JFrame myWindow = new CircleMain();
    }//end main
}//endclass CircleMain


  1. The drawing is declared as an instance variable, which can be referenced in all methods. There is no need for that here since no one every references after it has been built. However, it is very common to interact with a drawing during execution of a program, and an instance variable is useful for later interaction.
  2. Because the variable doesn't need to be an instance variable or indeed any variable, this could be rewritten as
    content.add(new CirclePanel(), BorderLayout.CENTER);
Copyleft 2003 Fred Swartz MIT License, Last update: 2003-10-31