Results 1 to 9 of 9
- 04-18-2009, 10:04 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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
i need some help plz/*
* 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);
}
}
thanks in advance
- 04-18-2009, 10:16 PM #2
drawLine() is for drawing lines, drawOval() is for drawing ovals.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-18-2009, 10:21 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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 #4
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)Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-18-2009, 10:54 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
i have done wht u say but still there are 3 errors
here is the code again
/*
* 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.getContentPane().add(new Ellipse());
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 #6
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 EllipseDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-18-2009, 11:06 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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 #8
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
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.Java Code:points[count++] = new Point(x, y);
Last edited by OrangeDog; 04-19-2009 at 12:11 AM.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-18-2009, 11:52 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Need help with drawing multiple objects with mouseClicked method.
By busdude in forum New To JavaReplies: 6Last Post: 04-05-2009, 11:28 PM -
Drawing a map
By Karp in forum AWT / SwingReplies: 4Last Post: 11-07-2008, 12:26 PM -
How to Draw Ellipse in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:13 PM -
GradientPaint Ellipse
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:48 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks