GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Having two problem on paint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Having two problem on paint" Watch "Having two problem on paint" New topic
Author

Having two problem on paint

ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
hi there,

I have some problem on using paint

below is my code:



my first question is why cant i move the icon after i press the button?
it seems that it has already move to x =100 y =100 before i press
and when i press the button, it didn't work.


I also search some previous topic and find

Yes, generally, in Java, the reference to the screen's Graphics object (called "g" in this case) is not reliable outside of the paint() method.

Standard procedure is to do all of your drawing in the paint() method, or in methods that are called only by paint(). That drawing code should look at the state of your application to know what to draw.

For example, you could have your key press set a boolean variable. Then, when paint() did its thing, it would look at the variable to know whether or not to draw the image.

And you don't normally call paint() directly. As Svend said, you would usually call repaint().

- Jeff


my second question is how and where should i set the variable to control paint in my code
I tried and it just cant pass into paint method.

thank you
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

First off, you're using Swing, so stay away from the paint(Graphics) method. You should override paintComponent(Graphics) instead.

Never put your program logic in paintComponent(). It gets called at moments you can't predict, so your logic will execute less or more often than you expect. In your case, you shouldn't update x and y in the painting method. paint/paintComponent are only for drawing.

Another problem is that your button will call repaint() on the ButtonPanel. So when you press it, the only thing that gets repainting is the ButtonPanel. Nothing else.

Take a good look at the changes I made and let me know if you understand:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
I think I ought to move this discussion to our GUIs forum.
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
Thank you Stephan,

you are awesome!!

I would never solve the problem without your help even after ages.

can i ask you two more question?

if I want to use another button to show another image,
I have to create new class like ButtonPanel2 and MyCanvas2, right?
Or I just add button2 in ButtonPanel and let MyCanvas class to decide which one to draw?
if the former case is correct, what should i do if i want to share coordinate x and y?
if the latter case is correct, how to set condition to let paintComponent choose which one to draw?

the second question is if I want the previous images remain,
what should i do?
Should I use repaint() or use other function?

Thank you
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Actually I think it's better not to separate the buttons from your frame in the first place. Here's how I usually do something like this:
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
Sorry, i don't get it....

where should I put my MyCanvas class, the graph just won't show up.



I just can't find it
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Sorry, I didn't understand your question. Can you be more clear?

By the way, there is a typo in your paintComponent signature. You wrote paintCompinent.
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
Thank you Stephan
The typo you point out annoys me all day

and Thank you again for let me really know how it works.

YOU ROCK!!

below is the code:



My last question is how can I let the image which i had printed remain?
Every time i use repaint(), the images just gone.

Please tell me thank you.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

You mean when you call move()?

You should stop moving them then. Eventually they go off the edge of your panel.
If you only want to change the image, you should call repaint() instead of move() in your buttons' listeners.
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
Sorry for bad description

What i mean is when i press button1 it prints logo1
and shows:

please see the "1" as image icon.
then i press button 2,
then it shows :

where "icon 2"will get the previous x,y position.
and if I press 1 again
it shows :

and so on...

like

<----1
<-----12
<------121


What I want is preserve the previous icons while showing present icon.
but I dont know which function is proper to call

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

I see. Well, what you need to do is maintain some state information. You have to keep track of the order in which you pressed the buttons, and then draw the images accordingly.

What you could do is maintain a list of ImageIcons, and when you press one button you add the one icon to it, and when you press the other button, you add the other icon to it.
Then in your paintComponent() method, you just iterate over all the elements in the list, and draw each of them in the location you want:
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19

Thank you Stephan

I know what you mean,
But can you tell me the type of "images"?
I thought it's arraylist at first.
But in drawimage it needs ....icon?
I am confused



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Yeah, you can just use a List<ImageIcon>.
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
But the compiler told me this line is incorrect

can not find symbol:
drawImage(javax.swing.ImageIcon,int,int,MyCanvas)

I don't know what's wrong
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The first parameter should be an Image, not an ImageIcon. So use images.get(i).getImage() instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
Thank you Stephan!!
Thank you Rob!!

I would never get this done without your help.
I love this place!!!

Thank you again Stephan.
Thank you for your time and help

I feel Very best for completing the code, and understand how it works at the same time

Below is the finished code :
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Please remove that call to repaint() from your paintComponent method. This causes the paintComponent method to be called, which calls repaint() which causes paintComponent to be called, etc. Your CPU usage will be quite high, with the continuous repainting.
ilumi kinoko
Greenhorn

Joined: Jan 07, 2011
Posts: 19
Thank you Rob,
I changed it.

That's why my computer make the noise so loud
I thought that's my GPU's sound
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Probably the CPU fan trying to keep your processor cool, with it running at full load
 
GeeCON Prague 2014
 
subject: Having two problem on paint