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

    Default an error in paint method

    hi all ,
    i am having an error
    but can't solve it

    here it is:
    protected void paint(Graphics g)
    1 error
    i am drawing a circle by using the mouse
    my class is:

    public class Circle_mousedraw extends JFrame implements MouseListener,MouseMotionListener
    so the program give this error when i run it

    can any one help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to post more of the error message and more of your code otherwise I doubt anyone will be able to guess what is wrong based on the information presented.

    One problem I see off the bat though is that you seem to be drawing directly to the JFrame. Don't do that. You are far better off extending a JPanel and drawing in that by overriding its paintComponent method. For one, by doing this you automatically get double-buffering.

    Good luck!

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

    Default

    ok this is my code

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

    package javaapplication21;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.GeneralPath;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;

    /**
    *
    * @author Free User
    */
    public class Circle_mousedraw extends JPanel implements MouseListener,MouseMotionListener {

    Point[] points;
    GeneralPath circle;
    final int INC = 8;
    JLabel statusBar; //mouse coordinate bar
    // our constructor

    public Circle_mousedraw()
    {
    initPoints();
    // to fill the circle we need to make something
    // that we can fill: a geometry primitive
    initCircle();
    // the stauts bar of the mouse
    statusBar = new JLabel("mouse outside the panel");
    add(statusBar,BorderLayout.SOUTH);
    //adding mouse events
    addMouseListener(this);
    addMouseMotionListener(this);

    }

    //here we will substitute in the circle equation

    private void initPoints()
    {
    int numberOfPoints = 360/INC;
    points = new Point[numberOfPoints];
    double cx = 200.0;
    double cy = 200.0;
    double r = 100.0;
    int count = 0;
    for(int theta = 0; theta < 360; theta+=INC)
    {
    int x = (int)(cx + r * Math.cos(Math.toRadians(theta)));
    int y = (int)(cy + r * Math.sin(Math.toRadians(theta)));
    points[count++] = new Point(x, y);
    }
    }

    //we begine the first step of the circle drawing

    private void initCircle()
    {
    circle = new GeneralPath();
    for(int j = 0; j < points.length; j++)
    {
    if(j == 0)
    circle.moveTo(points[j].x, points[j].y);
    else
    circle.lineTo(points[j].x, points[j].y);
    }
    circle.closePath();
    }



    ////////////////


    // @Override
    @Override
    protected void paintComponent (Graphics g)
    {
    super.paint(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.white);
    g2.fill(circle);
    g2.setPaint(Color.BLUE);
    Point p1 = points[0];
    for(int j = 1; j <= points.length; j++)
    {
    Point p2 = points[j % points.length];
    g2.drawLine(p1.x, p1.y, p2.x, p2.y);
    p1 = p2;
    }
    }



    // handel event when mouse is clicked

    public void mouseClicked(MouseEvent mouse) {
    //show the coordinates of x&y when the mouse clicked
    statusBar.setText(String.format("clicked at [%d, %d]", mouse.getX(),mouse.getY()));
    // Get the location of the current mouse click.
    int cx = mouse.getX();
    int cy = mouse.getY();
    // Tell the panel that we need to redraw things.
    repaint();
    }



    // handel event when mouse is pressed


    public void mousePressed(MouseEvent mouse) {

    //show the coordinates of x&y when the mouse pressed
    statusBar.setText(String.format("presed at [%d, %d]", mouse.getX(),mouse.getY())); }
    // handel event when mouse is released

    public void mouseReleased(MouseEvent mouse) {

    //show the coordinates of x&y when the mouse pressed
    statusBar.setText(String.format("presed at [%d, %d]", mouse.getX(),mouse.getY())); }


    // handel event when mouse is entered the area

    public void mouseEntered(MouseEvent mouse) {

    //show the coordinates of x&y when the mouse pressed
    statusBar.setText(String.format("presed at [%d, %d]", mouse.getX(),mouse.getY())); }


    //when the mouse exit the area

    public void mouseExited(MouseEvent mouse) {

    statusBar.setText("exit"); }



    ////////////////////////


    //Mousemotionlistener event handlers

    public void mouseDragged(MouseEvent mouse) {
    //show the coordinates of x&y when the mouse dragged
    statusBar.setText(String.format("draged at [%d, %d]", mouse.getX(),mouse.getY())); }

    public void mouseMoved(MouseEvent mouse) {
    //show the coordinates of x&y when the mouse moved
    statusBar.setText(String.format("moved at [%d, %d]", mouse.getX(),mouse.getY())); }



    //////////////



    public static void main(String[] args) {
    Circle_mousedraw c1 = new Circle_mousedraw ();


    c1.setVisible(true);
    JFrame f = new JFrame("my circle");
    f.setContentPane(new Circle_mousedraw());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }


    }
    and this is the error
    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    the program run but it seems to be a run time error
    the window appear but nothing happen in it

    i don't know why?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're calling the wrong super method here:
    Java Code:
      protected void paintComponent(Graphics g)
      {
        super.paint(g);
        //....
    Since paint calls paintComponent, this will result in an endless loop and a heap overrun.

    Much better is to call the super method of the same painting type method, paintComponent,
    Java Code:
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        //....

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

    Default

    oh wht a mistake

    thanks alot

    the code run but didn't make wht i need

    i need to draw the circle by the mouse
    (as in paint program)

    if u have any idea how to do this i will be thankful really

  6. #6
    rexicon is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Worked for me

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    rexicon, why are you spamming the fora with these non-sense posts?

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

    Default

    sorry can i ask once more?

    the code i wrote
    draw the circle with constant radius and center

    i want to make them variables
    also i need the circle be drawn using the mouse not automatically

    can u help me ?
    this is the last code i have wrote
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package javaapplication21;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    //import java.awt.RenderingHints;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.GeneralPath;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;

    /**
    *
    * @author Free User
    */
    public class Circle_mousedraw extends JPanel implements MouseListener,MouseMotionListener {

    Point[] points;
    GeneralPath circle;
    final int INC = 3;
    JLabel statusBar; //mouse coordinate bar
    // constructor
    public Circle_mousedraw()
    {
    initPoints();
    // to fill the circle we need to make something
    // that we can fill: a geometry primitive
    initCircle();

    // the stauts bar of the mouse
    statusBar = new JLabel("mouse outside the panel");
    add(statusBar,BorderLayout.SOUTH);

    //adding mouse events
    addMouseListener(this);
    addMouseMotionListener(this);

    }

    //here we will substitute in the circle equation
    public void initPoints()
    {
    int numberOfPoints = 360/INC;
    points = new Point[numberOfPoints];
    double cx = 200.0;
    double cy = 200.0;
    double r = 100.0;
    int count = 0;
    for(int theta = 0; theta < 360; theta+=INC)
    {
    int x = (int)(cx + r * Math.cos(Math.toRadians(theta)));
    int y = (int)(cy + r * Math.sin(Math.toRadians(theta)));
    points[count++] = new Point(x, y);
    }
    }


    //we begine the first step of the circle drawing
    public void initCircle()
    {
    circle = new GeneralPath();
    for(int j = 0; j < points.length; j++)
    {
    if(j == 0)
    circle.moveTo(points[j].x, points[j].y);
    else
    circle.lineTo(points[j].x, points[j].y);
    }
    circle.closePath();
    }





    ////////////////


    // @Override
    @Override
    protected void paintComponent (Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    // g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
    // RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.white);
    g2.fill(circle);
    g2.setPaint(Color.BLUE);
    Point p1 = points[0];
    for(int j = 1; j <= points.length; j++)
    {
    Point p2 = points[j % points.length];
    g2.drawLine(p1.x, p1.y, p2.x, p2.y);
    p1 = p2;
    }
    }



    // handel event when mouse is clicked

    public void mouseClicked(MouseEvent mouse) {
    //show the coordinates of x&y when the mouse clicked
    statusBar.setText(String.format("clicked at [%d, %d]", mouse.getX(),mouse.getY()));
    }



    // handel event when mouse is pressed


    public void mousePressed(MouseEvent mouse) {

    //show the coordinates of x&y when the mouse pressed
    statusBar.setText(String.format("presed at [%d, %d]", mouse.getX(),mouse.getY()));

    // Get the location of the current mouse click.
    int x = mouse.getX();
    int y = mouse.getY();
    int r = mouse.getX();
    }


    // handel event when mouse is released

    public void mouseReleased(MouseEvent mouse) {

    //show the coordinates of x&y when the mouse pressed
    statusBar.setText(String.format("presed at [%d, %d]", mouse.getX(),mouse.getY()));

    // Get the location of the current mouse click.
    int x = mouse.getX();
    int y = mouse.getY();
    int r = mouse.getX();
    // Tell the panel that we need to redraw things.
    repaint();
    }


    // handel event when mouse is entered the area

    public void mouseEntered(MouseEvent mouse) {

    //show the coordinates of x&y when the mouse pressed
    statusBar.setText(String.format("presed at [%d, %d]", mouse.getX(),mouse.getY())); }

    //when the mouse exit the area

    public void mouseExited(MouseEvent mouse) {

    statusBar.setText("exit"); }



    ////////////////////////


    //Mousemotionlistener event handlers

    public void mouseDragged(MouseEvent mouse) {
    //show the coordinates of x&y when the mouse dragged
    statusBar.setText(String.format("draged at [%d, %d]", mouse.getX(),mouse.getY()));

    // Get the location of the current mouse click.
    int x = mouse.getX();
    int y = mouse.getY();
    int r = mouse.getX();

    // Tell the panel that we need to redraw things.
    repaint();

    }

    public void mouseMoved(MouseEvent mouse) {
    //show the coordinates of x&y when the mouse moved
    statusBar.setText(String.format("moved at [%d, %d]", mouse.getX(),mouse.getY())); }



    //////////////



    public static void main(String[] args) {
    Circle_mousedraw c1 = new Circle_mousedraw ();


    c1.setVisible(true);
    JFrame f = new JFrame("my circle");
    f.setContentPane(new Circle_mousedraw());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }


    }
    i hope i find the true answer
    thanks in advance

Similar Threads

  1. Getting error when using recrusive method
    By jurka in forum New To Java
    Replies: 10
    Last Post: 02-13-2009, 04:15 AM
  2. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  3. Error with getString() method in
    By trivektor in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-14-2008, 07:30 PM
  4. Replies: 10
    Last Post: 09-18-2008, 10:41 AM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 01:10 PM

Posting Permissions

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