-
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
-
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.
-
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
-
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
 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);
}
}
}
-
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
-
Replies: 4
Last Post: 04-14-2006, 09:09 AM
-
Replies: 0
Last Post: 04-04-2003, 05:13 PM
-
By Steve Oliver in forum VB Classic
Replies: 0
Last Post: 07-26-2002, 12:30 PM
-
By Steve Oliver in forum VB Classic
Replies: 0
Last Post: 07-25-2002, 04:29 PM
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks