• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having two problem on paint

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I ought to move this discussion to our GUIs forum.
 
ilumi kinoko
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you can just use a List<ImageIcon>.
 
ilumi kinoko
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first parameter should be an Image, not an ImageIcon. So use images.get(i).getImage() instead.
 
ilumi kinoko
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably the CPU fan trying to keep your processor cool, with it running at full load
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic