# drawing ellipse by drawline method?

• 04-18-2009, 10:04 PM
hopey
drawing ellipse by drawline method?
hi all
hope to be fine
this is my first time to be here
but i feel happy

can any one help me in doing my task

i want to draw an ellipse with drawline method
i made this code but it doesn't work

Quote:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication19;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
/**
*
* @author Free User
*/
public class Ellipse extends JPanel {

/**
* @param args the command line arguments
*/
Point [] points;
GeneralPath ellipse;
final int INC = 1;
public Ellipse()
{
initPoints();
// to fill the circle we need to make something
// that we can fill: a geometry primitive
initEllipse();
}

private void initPoints() {
int numberOfPoints = 360/INC;
points = new Point[numberOfPoints];
//double cx = 200.0;
//double cy = 200.0;
double w = 100.0;
double h = 30.0;
int count = 0;
for(int t = 0; t <=360; t+=INC)
{
int x = (int)( w * Math.cos(Math.toRadians(t)));
int y = (int)( h * Math.sin(Math.toRadians(t)));
points[count++] = new Point(x, y);
}
}
private void initEllipse() {
GeneralPath Ellipse = new GeneralPath();
for(int j = 0; j < points.length; j++)
{
if(j == 0)
Ellipse.moveTo(points[j].x, points[j].y);
else
Ellipse.lineTo(points[j].x, points[j].y);
}
Ellipse.closePath();
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Ellipse() ); // here we have something wrong ??
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
i need some help plz

• 04-18-2009, 10:16 PM
OrangeDog
drawLine() is for drawing lines, drawOval() is for drawing ovals.
• 04-18-2009, 10:21 PM
hopey
oh i know that
but my task is how to draw ellipse without using any methods except drawline method
• 04-18-2009, 10:27 PM
OrangeDog
Ah right, good.

At the line you have marked you should be adding your component to the contentPane, not replacing the contentPane. You can do this by using getContentPane().add(Component)
• 04-18-2009, 10:54 PM
hopey
i have done wht u say but still there are 3 errors

here is the code again
Quote:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication19;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
/**
*
* @author Free User
*/
public class Ellipse extends JPanel {

/**
* @param args the command line arguments
*/
Point [] points;
GeneralPath ellipse;
final int INC = 1;
public Ellipse()
{
initPoints(); //here there is an error1???
// to fill the circle we need to make something
// that we can fill: a geometry primitive
initEllipse();

}

private void initPoints() {
int numberOfPoints = 360/INC;
points = new Point[numberOfPoints];
//double cx = 200.0;
//double cy = 200.0;
double w = 100.0;
double h = 30.0;
int count = 0;
for(int t = 0; t <=360; t+=INC)
{
int x = (int)( w * Math.cos(Math.toRadians(t)));
int y = (int)( h * Math.sin(Math.toRadians(t)));
points[count++] = new Point(x, y); //here there is an error2 ?
}
}
private void initEllipse() {
GeneralPath Ellipse = new GeneralPath();
for(int j = 0; j < points.length; j++)
{
if(j == 0)
Ellipse.moveTo(points[j].x, points[j].y);
else
Ellipse.lineTo(points[j].x, points[j].y);
}
Ellipse.closePath();
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new Ellipse() ); // here we heve error3 again??
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
• 04-18-2009, 10:58 PM
OrangeDog
1) If you actually tell us what the errors are it will be a lot easier
2) If you read and try to understand the error messages yourself first, you may well work it out
3) You haven't done what I said in the last post, you're still trying to replace the contentPane with an Ellipse
• 04-18-2009, 11:06 PM
hopey
ok sorry for this miss understanding
but be patient on me as i am a beginner

here are the errors

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 360

at javaapplication19.Ellipse.initPoints(Ellipse.java: 44)
at javaapplication19.Ellipse.<init>(Ellipse.java:26)
at javaapplication19.Ellipse.main(Ellipse.java:61)
• 04-18-2009, 11:33 PM
OrangeDog
This stack trace only tells you about one error that was last thrown at line 44. The rest of the trace tells you which calls were executed to get there.

It means you've gone past the end of the array at
Code:

`points[count++] = new Point(x, y);`
This is because the for loop goes around 361 times (0 to 360 inclusive), but the array only goes up to 359 (360 entries including zero). Note that when you fix this, it will still break whenever INC doesn't equal 1.
• 04-18-2009, 11:52 PM
hopey
thanks alot

i will try to fix this

and if i faced any problem i will ask

thanks again