The Artima Developer Community
Sponsored Link

Java Answers Forum
Circle Class

4 replies on 1 page. Most recent reply: Mar 15, 2005 4:01 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Timothy

Posts: 4
Nickname: houston
Registered: Mar, 2005

Circle Class Posted: Mar 14, 2005 6:20 AM
Reply to this message Reply
Advertisement
Hi,

I am creating a class called Circle. An object of this class must hold the value of its radius and its position on screen, these values, which are all whole numbers, should be set only at a time a circle is created. A circle object must be capable of reporting its area and its circumference. It also has a paint method which draws the specified circle on a graphics screen, with the area and circumference printed below the circle.

I am using the fillOval method which works in the same way as the drawOval. I am running the class in a frame shown below:

import java.awt.*;
public class Circle5_2
{
public static void main(String[] args)
{
Frame frame = new Frame();
Circle myCircle = new Circle(100,50,50);
frame.setSize(300.300);
frame.setBackground(Color.yellow);
frame.add(myCircle);
frame.setVisible(true);
}
}

I am having problems finishing off the circle class, I would greatly appreciate any help on this.
Everything I have so far is correct, I am just having problems doing the area, circumference and radius part as explained in the first paragraph.:

import java.awt.*;
import java.applet.*;

class Circle extends Applet
{

private int a;
private int b;
private int c;

public Circle5_2(int position, int length, int height)
{
a = x;
b = y;
c = z;
}
public void paint(Graphics g)
{
g.drawString(,150, 40)
////// radius, area and circumference part???

Any help would be appreciated
Thanks


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Circle Class Posted: Mar 14, 2005 3:20 PM
Reply to this message Reply
import java.awt.*;
public class Circle5_2
{
public static void main(String[] args)
{
Frame frame = new Frame();
Circle myCircle = new Circle(100,100,50);
frame.setSize(300, 300);
frame.setBackground(Color.yellow);
frame.add(myCircle);
frame.setVisible(true);
}
}


import java.awt.*;
import java.applet.*;
 
class Circle extends Applet
{
 
private int x = 0;
private int y = 0;
private double radius = 0;
 
public Circle(int x, int y, double radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
 
public void paint(Graphics g)
{
g.drawString("Radius: " + getRadius(), 50, 20);
g.drawString("Area: " + getArea(), 50, 40);
g.drawString("Area: " + getCircumference(), 50, 60);
int width = (int)(radius * 2);
int height = (int)(radius * 2);
g.fillOval(x, y, width, height);
}
 
private double getRadius(){
    return radius;
}
 
private double getArea(){
    return Math.PI * radius * radius;
}
 
private double getCircumference(){
    return Math.PI * radius * 2d;
}
 
}

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Circle Class Posted: Mar 15, 2005 3:46 AM
Reply to this message Reply
Hi,

Check this code:
public interface IDraw
{
	public void draw(Graphics g);
}
 
public class Circle implements IDraw
{
	private int x;
	private int y;
	private int radius;
	
	public Circle(int x, int y, int radius)
	{
		this.x = x;
		this.y = y;
		this.radius = radius;
	}
	
	public void draw(Graphics g)
	{
		int width = radius*2;
		g.fillOval(x, y, width, width);
		// draw your string here.
	}
}
 
 
public class Canvas extends JPanel
{
	private Vector circles;
	
	public Canvas()
	{
		circles = new Vector();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Iterator it = circles.iterator();
		while(it.hasNext())
		{
			IDraw d = (IDraw)it.next();
			d.draw(g);
		}
	}
	
	public void addCircle(Circle c)
	{
		circles.add(c);
	}
}
 
 
public void TestApp
{
	public static void main(String args[])
	{
		JFrame f = new JFrame("test");
		Canvas canvas = new Canvas();
		Circle circle = new Circle(100,100,20);
		f.getContentPane().add(canvas);
		f.setSize(500,500);
		f.setVisible(true);
	}
}
 


You are extending Circle from an Applet, this will make your circle very heavy and if you want 100 circles then your application will die. Look at my code and if you don't get it then revert back.

I have not tested the code but logic will work.

Regards,
Amol Brid.

Timothy

Posts: 4
Nickname: houston
Registered: Mar, 2005

Re: Circle Class Posted: Mar 15, 2005 5:06 AM
Reply to this message Reply
Thanks both of you for your help.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Circle Class Posted: Mar 15, 2005 4:01 PM
Reply to this message Reply
Be careful.
You can't mix java.awt components with javax.swing components.

Your started out using java.awt with the Applet extension.

The other solution used Swing components, i.e. JPanel, which will work fine.

Flat View: This topic has 4 replies on 1 page
Topic: Search Function for Soccer tournement system. searching strings of arrays?? Previous Topic   Next Topic Topic: Hi all


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us