The Artima Developer Community
Sponsored Link

Java Answers Forum
Drawing Method Problem

6 replies on 1 page. Most recent reply: Feb 16, 2004 7:20 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Niall

Posts: 10
Nickname: niall
Registered: Feb, 2004

Drawing Method Problem Posted: Feb 12, 2004 10:11 AM
Reply to this message Reply
Advertisement
The program below currently draws a point on the screen when the user clicks with the mouse. I call the method testPass from another program, and pass the boolean variable vertexCheck. My problem is that I want the paint program only to draw the points when this variable is true. I have been trying to do this for ages, with no luck.


class DrawingArea extends JPanel{
Point[] p = new Point[100];
int i = 0;

public void testPass(boolean vertexCheck){}

public DrawingArea(){
MouseEventListener mlistener = new 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);
i++;
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);}
}
}

}


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Drawing Method Problem Posted: Feb 12, 2004 12:51 PM
Reply to this message Reply
> The program below currently draws a point on the screen
> when the user clicks with the mouse. I call the method
> testPass from another program, and pass the boolean
> variable vertexCheck. My problem is that I want the paint
> program only to draw the points when this variable is
> true. I have been trying to do this for ages, with no
> luck.
>
>
> class DrawingArea extends JPanel{
> Point[] p = new Point[100];
> int i = 0;
>
> public void testPass(boolean vertexCheck){}
>
> public DrawingArea(){
> MouseEventListener mlistener = new
> new 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);
> i++;
> 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);}
> }
> }
>
> }
>


The most obvious thing that I see is that your testPass method has no code! Either you left out a critical part of the code when you typed it in, or you still need to write the code for that method. What is the method supposed to do?

Niall

Posts: 10
Nickname: niall
Registered: Feb, 2004

Re: Drawing Method Problem Posted: Feb 14, 2004 8:23 AM
Reply to this message Reply
I just left the method blank for the purpose of this post, as everything I have tried with it does not work. Basically, the teatPass method is called when a button on my other program is pressed. I only want a point to be drawn when that method is called. I have also tried the following:

public void testPass(boolean vertexCheck, int x, int y){    if(vertexCheck==true)     
     {  p[i] = new Point(x,y);        
        i++;
        repaint();      
     }
}


From the other program, I also pass the x and y values to construct the new point. Within this method, this works fine, and the new point is constructed correctly. However, the paint method does not seem to recognise the new point and still views the point as (0,0).

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Drawing Method Problem Posted: Feb 14, 2004 2:12 PM
Reply to this message Reply
I'm not clear on what you are trying to accomplish, so I'm making a guess. Here is what I think that you are trying to accomplish. When you click on the DrawingArea object, you want the point to be stored, but NOT drawn until you press the button in your other class.


IF I have interpreted you correctly, here are some things that I see that may be part of the problem.

1. p = new Point(x, y) should be p = new Point(x, y)
(I'm guessing this is just a typo on your part as I don't think what you posted will compile, and you've obviously gotten it to compile.)

2. Don't repaint in the mouseClicked method. That will cause the point to get drawn when you click on it. Leave the repaint to the testPass method.

3. Don't store the point in the testPass method. It won't know what the x and y values are - only the MouseClicked method will know that. You are always getting zero's because that is the default value of primitives like int's.

> I just left the method blank for the purpose of this post,
> as everything I have tried with it does not work.
> Basically, the teatPass method is called when a button on
> my other program is pressed. I only want a point to be
> drawn when that method is called. I have also tried the
> following:
>
>
> public void testPass(boolean vertexCheck, int x, int y){
>    if(vertexCheck==true)     
>      {  p[i] = new Point(x,y);        
>         i++;
>         repaint();      
>      }
> }
> 

>
> From the other program, I also pass the x and y values to
> construct the new point. Within this method, this works
> fine, and the new point is constructed correctly. However,
> the paint method does not seem to recognise the new point
> and still views the point as (0,0).

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Drawing Method Problem Posted: Feb 14, 2004 2:50 PM
Reply to this message Reply
Aaargh! I hit *post* when I meant to hit preview during my previous reply! Sorry.

It also occured to me that you may be trying to use the button to verify which points that you want in your drawing. In other words, is this what you want to have happen?

You click on the screen and a point appears. If you want to keep that point in your drawing, you click on a button. But if you click on another spot without having clicked on the button first, the previous point will disappear and the new point will take its place. When you are done, only the points that have been verified by pushing on the button will remain in the drawing.

If that is what you want, then here is a revised set of suggestions.

1. Same as before. p = new Point(x, y) is wrong regardless.

2. Keep the repaint call in the mouseClicked method, but don't increment i. That way the next mouseclick will overwrite the unwanted point.

3. All the testPass method needs to do is increment i if the boolean is true. That way the next mouse click will add another point instead of overwriting the old one. (I'm still not clear on where the boolean is coming from, so maybe you don't increment i if the boolean is false.)

Obviously, if neither of my guesses is correct, then my suggestions on changing your code won't help. I'm willing to offer more suggestions if you can help me understand what you are trying to accomplish.

I hope this helps.

Niall

Posts: 10
Nickname: niall
Registered: Feb, 2004

Re: Drawing Method Problem Posted: Feb 16, 2004 1:32 PM
Reply to this message Reply
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 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.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Drawing Method Problem Posted: Feb 16, 2004 7:20 PM
Reply to this message Reply
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.

Flat View: This topic has 6 replies on 1 page
Topic: Putting multiple images into one component Previous Topic   Next Topic Topic: sort CSV file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use