DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Radius, area and circumference

  1. #1
    Join Date
    Dec 2004
    Posts
    46

    Radius, area and circumference

    I have this class that draws a circle and then it works out its area, radius and circumference and prints it below the circle that it draws from the class that it links too but I am not to sure what the code is to work out the area etc.

    The code is below:

    Code:
     
        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 = numberone;
            b = numbertwo;
            c = numberthree;
        }
    public void paint(Graphics g)
        {
    
           RADIUS, AREA, CIRCUMFERENCE NEEDS TO GO HERE CODE??             
         } 
    
    }
    Any suggestions would be appreciated.

    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    How are you drawing your circle? Are you using a method similar to the drawOval() method of the graphics class? If so, you have a square as the bounding shape. Your radius is one-half of the width of your square; you can calculate 2-pi-r and pi - r ^2 from there (making use of the static pi value from the Math class ... )

  3. #3
    Join Date
    Dec 2004
    Posts
    46
    To draw the cirlce i am using the fillOval method which works in the same way as the drawOval.

    The circle class i posted earlier is run in a frame with the code shown below:

    Code:
     
    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);
        }
    }
    The circle object needs to be capable of reporting its area and its cirumference. It has a paint method which draws the specified circle on a graphics screen, with the area and circumference printed below.
    This is the section i am having trouble with which i posted earlier in the circle class.
    Last edited by WillisTi; 02-24-2005 at 11:12 AM.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    [Is your question about how you calculate the values or how you display the results?]

    Well, wouldn't your radius be one-half of a side (your second and third parameters).

    You have captured that value ... r = 1/2 * s; circumference is 2 * pi * r; area is pi * r * r ...

    or am I missing something?
    Last edited by nspils; 02-24-2005 at 05:02 PM.

  5. #5
    Join Date
    Dec 2004
    Posts
    46
    ok thanks

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

    I think I have posted this one before...

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.text.*;
    
    
    public class CircleDriver extends JFrame {
      private Circle circle=new Circle(120, new Point(300,200));
      public CircleDriver () {
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
      public void paint (Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.white);
        g.fillRect(0,0,this.getSize().width,this.getSize().height);
        g.setColor(c);
        circle.draw(g);
      }
      public static void main(String[] args) {
        CircleDriver drv=new CircleDriver();
        drv.setBounds(10,10,600,400);
        drv.setVisible(true);
      }
    }
    /**
     * A circle class that draws a circle using the graphics.drawOval() method
     * @author sjalle
     * @version 1.0
     */
    class Circle {
      static DecimalFormat dec2Format=new DecimalFormat("0.00");
      static Font cFont=new Font("monospace",Font.PLAIN,14);
      Color fillColor=Color.CYAN;
      private int radius=-1;
      private Point center=new Point();
      private Point topLeft=new Point();
      FontMetrics fm=null;
    
      public Circle(int radius, Point center) {
        setRadius(radius);
        setCenter(center);
      }
      public void setCenter(Point center) {
        this.center.setLocation(center.x, center.y);
      }
      public void setRadius(int radius) {
        this.radius=radius;
      }
      private void setTopLeft() {
        topLeft.x=this.center.x-this.radius;
        topLeft.y=this.center.y-this.radius;
      }
      public double getArea() {
        return Math.PI*Math.pow((double)this.radius,2.0d);
      }
      public double getCircumference() {
        return 2.0d*Math.PI*this.radius;
      }
      public void draw(Graphics g) {
        setTopLeft();
        g.drawOval(topLeft.x,topLeft.y,this.radius*2,this.radius*2);
    
        Color c=g.getColor();
        Font f=g.getFont();
    
        g.setColor(fillColor);
        g.fillOval(topLeft.x,topLeft.y,this.radius*2,this.radius*2);
    
        drawCircleData(g);
    
        g.setColor(c);
        g.setFont(f);
      }
      /**
       * Draw the circle data centerec below the circle.
       * @param g
       */
      private void drawCircleData (Graphics g) {
        if (fm==null) fm=g.getFontMetrics(cFont);
        String repString="Area: "+
            dec2Format.format(getArea())+
            " Circumeference: "+
            dec2Format.format(getCircumference());
        Rectangle2D rect=fm.getStringBounds(repString, g);
        int xPos=this.center.x-((int)rect.getWidth()/2);
        int yPos=this.center.y+this.radius+(int)rect.getHeight();
        g.setColor(Color.black);
        g.setFont(cFont);
        g.drawString(repString,xPos,yPos);
      }
    }
    eschew obfuscation

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