twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Drawing Method Problem
|
Posted: Feb 16, 2004 7:20 PM
|
|
I noticed a couple of things that I didn't see before that also may be part of the problem.
a. You are using i as the incrementing variable in the for loop as well as an instance variable. The i in the for loop is overriding i the instance variable in that method. That might explain why the print statement isn't working right.
b. You overrode the paintComponent method instead of the paint method. Usually the paint method gets overridden. May or may not be a problem.
c. The site is treating the letter i in brackets as the beginning of italic formatting. That doesn't affect your code, but it affects what I see in your post.
Try these lines in the appropriate places. May not fix everything, but could be a step in the right direction. (NOTE: I changed i to num, and the for loop variable to j for clarity)
int num = 0;
if(vertexCheck==true){
num++;
System.out.println("Test i in testPass:"+num);}}
public void paint(Graphics g){
super.paint(g);
for(int j=0; j< p.length; j++){
if (p[j]!=null){
g.fillOval(p[j].x, p[j].y, 10, 10);
System.out.println("Test i in Paint:"+num);}}}
}
> twc, that is what I am trying to achieve. The testPass > method is called from another program when a button is > pressed. Basically the program is for drawing a series of > vertices and edges with their weights. Later I will then > use this in the minimum spanning tree problem. Currently I > am working on the vertices part. I only want to be able to > draw a point on screen when the button is pressed. I have > tried your corrections, but still without success: > > class DrawingArea extends JPanel{
>
> Point[] p = new Point[100];
> int i = 0;
>
> public void testPass(boolean vertexCheck){
> if(vertexCheck==true){
> i++;
> System.out.println("Test i in testPass:"+i);}}
>
> public DrawingArea() {
> MouseEventListener mlistener = new
> ew MouseEventListener();
> addMouseListener(mlistener);}
>
> class MouseEventListener implements MouseListener{
> public void mouseEntered(MouseEvent e){}
> public void mouseReleased(MouseEvent e){}
> public void mousePressed(MouseEvent e){}
> public void mouseExited(MouseEvent e){}
>
> public void mouseClicked(MouseEvent e){
> int x = e.getX();
> int y = e.getY();
> p[i] = new Point(x,y);
> repaint();}}
>
> public void paintComponent(Graphics g){
> super.paintComponent(g);
> for(int i=0; i< p.length;i++){
> if (p[i]!=null){
> g.fillOval(p[i].x, p[i].y, 10, 10);
> System.out.println("Test i in Paint:"+i);}}}
> }
>
> > I added the two tests just to check the value of i at each > point of the program. Within the testPass method, i > increments correctly, however in the paint method, i is > always equal to zero. It seems no matter what variables I > declare or give values to in the testPass method are not > recognised by the paint method. I can now paint one point > on screen at a time, but when the button is clicked, the > point does not remain when a new point is clicked. Thanks > for all the help and I hope this explains my problem a bit > better.
|
|