Results 1 to 9 of 9
  1. #1
    hopey is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default 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

    /*
    * 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

    thanks in advance

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    hopey is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    oh i know that
    but my task is how to draw ellipse without using any methods except drawline method

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  5. #5
    hopey is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    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);
    }
    }

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    hopey is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    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)

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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
    Java 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.
    Last edited by OrangeDog; 04-19-2009 at 01:11 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    hopey is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    thanks alot

    i will try to fix this

    and if i faced any problem i will ask

    thanks again

Similar Threads

  1. Replies: 6
    Last Post: 04-06-2009, 12:28 AM
  2. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 01:26 PM
  3. How to Draw Ellipse in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:13 AM
  4. GradientPaint Ellipse
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:48 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •