how to create circle


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: how to create circle

  1. #1
    Join Date
    Oct 2005
    Posts
    7

    how to create circle

    hi i am neer
    i am new to this forum.......and to java coding too.......
    plz help me in creating a circle class with radius r, and class point which is the center of circle
    Thanx
    Neer

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    If you want to draw a circle, see the Graphics class. It has draw... methods that will draw a circle (an oval with width = height)

    For your class, define a class with two variables.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Ive assembled some basic code for it here

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    /**
     * A Simple circle class with radius, location and color
     */
    
    public class Circle {
    
      int r=0;
      Point location=new Point();
      Color color=Color.blue;
    
      // CONSTRUCTORS
    
      public Circle() {}
      public Circle(int r, Point location) {
        this.r=r;
        this.location=location;
      }
      public Circle(int r, Point location, Color color) {
        this(r,location);
        this.color=color;
      }
    
      /**
       * Draws the circle
       */
      public void draw(Graphics g) {
        Color c=g.getColor(); // save context color
        g.setColor(color);
        g.fillOval(location.x,location.y,r,r);
        g.setColor(Color.black);
        g.drawOval(location.x,location.y,r,r);
        g.setColor(c); // restore context color
      }
      /**
       * Main for testing
       */
      public static void main(String[] args) {
        JFrame f=new JFrame("Circle test");
        DrawPanel pan=new DrawPanel(); // make a drawingpanel
        f.getContentPane().setLayout(new GridLayout());
        f.getContentPane().add(pan);
        // add two circles
        pan.addCircle(new Circle(20,new Point(100,100),Color.orange));
        pan.addCircle(new Circle(100,new Point(300,200)));
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        f.setBounds(10,10,500,400);
        f.setVisible(true);
      }
    }
    /**
     * A panel extension for drawing circles
     */
    class DrawPanel extends JPanel {
      ArrayList circleList=new ArrayList();
      public void addCircle(Circle aCircle) {
        circleList.add(aCircle);
      }
      public void paint (Graphics g) {
        update(g);
      }
      public void update(Graphics g) {
        for (int i=0; i<circleList.size(); i++) {
          Circle aCircle=(Circle)circleList.get(i);
          aCircle.draw(g);
        }
      }
    }
    eschew obfuscation

  4. #4
    Join Date
    Oct 2005
    Posts
    7

    Thanx

    Thanx buddy
    Your help is highly appreciated..........as i am beginer to java
    can u plz give me one more lil favour.......
    if u can send me the comments for the following block
    /**
    * A panel extension for drawing circles
    */
    class DrawPanel extends JPanel {
    ArrayList circleList=new ArrayList();
    public void addCircle(Circle aCircle) {
    circleList.add(aCircle);
    }
    public void paint (Graphics g) {
    update(g);
    }
    public void update(Graphics g) {
    for (int i=0; i<circleList.size(); i++) {
    Circle aCircle=(Circle)circleList.get(i);
    aCircle.draw(g);
    }
    }
    }
    Thanx in Advance
    Neer

    Quote Originally Posted by sjalle
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    /**
     * A Simple circle class with radius, location and color
     */
    
    public class Circle {
    
      int r=0;
      Point location=new Point();
      Color color=Color.blue;
    
      // CONSTRUCTORS
    
      public Circle() {}
      public Circle(int r, Point location) {
        this.r=r;
        this.location=location;
      }
      public Circle(int r, Point location, Color color) {
        this(r,location);
        this.color=color;
      }
    
      /**
       * Draws the circle
       */
      public void draw(Graphics g) {
        Color c=g.getColor(); // save context color
        g.setColor(color);
        g.fillOval(location.x,location.y,r,r);
        g.setColor(Color.black);
        g.drawOval(location.x,location.y,r,r);
        g.setColor(c); // restore context color
      }
      /**
       * Main for testing
       */
      public static void main(String[] args) {
        JFrame f=new JFrame("Circle test");
        DrawPanel pan=new DrawPanel(); // make a drawingpanel
        f.getContentPane().setLayout(new GridLayout());
        f.getContentPane().add(pan);
        // add two circles
        pan.addCircle(new Circle(20,new Point(100,100),Color.orange));
        pan.addCircle(new Circle(100,new Point(300,200)));
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        f.setBounds(10,10,500,400);
        f.setVisible(true);
      }
    }
    /**
     * A panel extension for drawing circles
     */
    class DrawPanel extends JPanel {
      ArrayList circleList=new ArrayList();
      public void addCircle(Circle aCircle) {
        circleList.add(aCircle);
      }
      public void paint (Graphics g) {
        update(g);
      }
      public void update(Graphics g) {
        for (int i=0; i<circleList.size(); i++) {
          Circle aCircle=(Circle)circleList.get(i);
          aCircle.draw(g);
        }
      }
    }

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Like the comment says: A panel extension for drawing circles. Check the
    java doc on the use of the Component paint/update methods and using ArrayList
    eschew obfuscation

Similar Threads

  1. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 09:09 AM
  2. Re: (No subject)
    By Joe in forum Database
    Replies: 0
    Last Post: 04-04-2003, 05:13 PM
  3. Can't create a PARADOX file and insert records using ODBC
    By Steve Oliver in forum VB Classic
    Replies: 0
    Last Post: 07-26-2002, 12:30 PM
  4. Replies: 0
    Last Post: 07-25-2002, 04:29 PM
  5. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center