Results 1 to 8 of 8
Thread: an error in paint method
- 04-20-2009, 01:38 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
an error in paint method
hi all ,
i am having an error
but can't solve it
here it is:
i am drawing a circle by using the mouseprotected void paint(Graphics g)
1 error
my class is:
so the program give this error when i run itpublic class Circle_mousedraw extends JFrame implements MouseListener,MouseMotionListener
can any one help me?
-
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!
- 04-20-2009, 06:07 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
ok this is my code
and this is the error/*
* 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);
}
}
the program run but it seems to be a run time errorException in thread "AWT-EventQueue-0" java.lang.StackOverflowError
the window appear but nothing happen in it
i don't know why?
-
You're calling the wrong super method here:
Since paint calls paintComponent, this will result in an endless loop and a heap overrun.Java Code:protected void paintComponent(Graphics g) { super.paint(g); //....
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); //....
- 04-21-2009, 11:57 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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
- 04-24-2009, 10:47 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
Worked for me
-
rexicon, why are you spamming the fora with these non-sense posts?
- 04-24-2009, 10:12 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 10
- Rep Power
- 0
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
i hope i find the true answer/*
* 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);
}
}
thanks in advance
Similar Threads
-
Getting error when using recrusive method
By jurka in forum New To JavaReplies: 10Last Post: 02-13-2009, 03:15 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Error with getString() method in
By trivektor in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-14-2008, 06:30 PM -
Why am I getting an error when calling this method?
By CirKuT in forum New To JavaReplies: 10Last Post: 09-18-2008, 09:41 AM -
Error: invalid method declaration
By silvia in forum New To JavaReplies: 1Last Post: 07-27-2007, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks