Solved

Circle and Ellipse

Posted on 2010-11-18
9
386 Views
Last Modified: 2012-05-10
How do I make an ellipse??? I have mange to do the circle code.
Anyone could show me?

Ellips
Area = p * a * b
import java.awt.geom.*;



/**

 * A circle that can be manipulated and that draws itself on a canvas.

 * 

 *

 */



public class Circle extends Shape

{

    private int diameter;

    

    /**

     * Create a new circle at default position with default color.

     */

    public Circle()

    {

        super(20, 60, "blue", false);

        diameter = 30;

    }

    



    /**

     * Change the size to the new size (in pixels). Size must be >= 0.

     */

    public void changeSize(int newDiameter)

    {

        erase();

        diameter = newDiameter;

        draw();

    }



    /**

     * Draw the circle with current specifications on screen.

     */

    protected void draw()

    {

        if(isVisible) {

            Canvas canvas = Canvas.getCanvas();

            canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, 

                                                          diameter, diameter));

            canvas.wait(10);

        }

    }



     /**

     * Get the area value of the circle.

     */

    public double getArea()

    {

        return Math.PI * diameter * diameter;

    }

        

}

Open in new window

0
Comment
Question by:Mickeys
  • 4
  • 4
9 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 34162708

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.geom.Arc2D;

import java.awt.geom.Ellipse2D;

import java.awt.geom.GeneralPath;

import java.awt.geom.Line2D;

import java.awt.geom.Rectangle2D;

import java.awt.geom.RoundRectangle2D;



import javax.swing.JApplet;

import javax.swing.JFrame;



public class EllipseDemo2D extends JApplet {

  final static BasicStroke stroke = new BasicStroke(2.0f);  

  public void init() {

    setBackground(Color.white);

    setForeground(Color.white);

  }





  public void paint(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

        RenderingHints.VALUE_ANTIALIAS_ON);



    g2.setPaint(Color.gray);

    int x = 5;

    int y = 7;



    g2.setStroke(stroke);

    g2.draw(new Ellipse2D.Double(x, y, 200, 200));

    g2.drawString("Ellipse2D", x, 250);



  }



  public static void main(String s[]) {

    JFrame f = new JFrame("");

    f.addWindowListener(new WindowAdapter() {

      public void windowClosing(WindowEvent e) {

        System.exit(0);

      }

    });

    JApplet applet = new EllipseDemo2D();

    f.getContentPane().add("Center", applet);

    applet.init();

    f.pack();

    f.setSize(new Dimension(300, 300));

    f.show();

  }

}

Open in new window

0
 

Author Comment

by:Mickeys
ID: 34162728
Ehhh??? I need to use it from my code. I could have found copy paste on internet myself.
0
 
LVL 16

Expert Comment

by:Valeri
ID: 34162841
in this line
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
                                                          diameter, diameter));

change to
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
                                                          diameter, diameter + 70));
0
 
LVL 16

Expert Comment

by:Valeri
ID: 34162854
If your parameters 3 and 4 are equal, it's a circle, otherwise if they are different it's a ellipse...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Mickeys
ID: 34162857
so I should like extend from circle or someting?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 34162891
Actually circle is ellipse, a particular kind of ellipse, so you have to have 2 parameters :
diameterX and diameter Y
A proportion between them determine how will your ellipse will look like. If they are equal it's a circle.
so change the line to :
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
                                                          diameterX , diameterY));
0
 

Author Comment

by:Mickeys
ID: 34163282
so what you are saying. Use that line and exchange diameter to x and y.

So a copy of the circle?????? with the above included.
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 34163370
no, just create another class, for Ellipse, it similar to Circle, but it's good to have different classes for Circle and Ellipse. Here it is :

import java.awt.geom.*;

public class Ellipse extends Shape {
    private int diameterX, diameterY;
   
    /**
     * Create a new circle at default position with default color.
     */
    public Ellipse()
    {
        super(20, 60, "blue", false);
        diameterX = 30; diameterY = 30;
    }
   

    /**
     * Change the size to the new size (in pixels). Size must be >= 0.
     */
    public void changeSize(int diameterX, int diameterY)
    {
        erase();
        this.diameterX = diameterX; this.diameterY = diameterY;
        draw();
    }

    /**
     * Draw the circle with current specifications on screen.
     */
    protected void draw()
    {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, diameterX, diameterY));
            canvas.wait(10);
        }
    }

     /**
     * Get the area value of the circle.
     */
    public double getArea()
    {
        return Math.PI * diameterX * diameterY;
    }
       
}
0
 

Author Closing Comment

by:Mickeys
ID: 34163462
Ok. I just thought it should inheritate like Ellipse extend Circle but i guess I was wrong. :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 139
Groovy:unable to resolve class error 2 70
eclipse argument 14 53
Java JRE greater than 1.6 5 51
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now