• 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

can't repaint frame

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
i'm doing a little application, where an image has to follow the mousecursor in a JFrame. To get the mousecoordinates i use a MouseMotionListener. I call the repaint() method in the listener void, but it never draws the image...
Thanks a lot for your help




 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't work? How do you know that? (Read our FAQ entry ItDoesntWorkIsUseless which explains what you should tell us about your problem.)
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the bad explanation.
I meant that it actually never "enters" (sorry for the oversimplyfication, but i don't know the technical term, because i'm a beginner) the paintComponent method. Although it should get to the textposition, where the repaint() method is called (i checked that)
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I meant that it actually never "enters" (sorry for the oversimplyfication, but i don't know the technical term, because i'm a beginner) the paintComponent method.


How do you know this, have you put a System.out.println() type statement in paintComponent?
Why are you loading the same image each time paintComponent() is called, can't you load it once eg in the constructor.
Have you checked the image is actually loaded, it may be that the image file can't be found?
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i checked it witch a System.out.println() and it never appears. But it gets into the listener class and it gets the right coordinates, i checked that to. And the image can be loaded, everything worked at the beginning, until i gave posX and posY as its coordinates and tried to refresh it in the listener class.
Thanks for the idea to load the picture earlier, i'll do it
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dario Stroob wrote:everything worked at the beginning, until i gave posX and posY as its coordinates and tried to refresh it in the listener class.



Didn't you just have another post in which your problem was that you declared variables in two different places and were confused about the scope of those variables? Well, you're doing that again. Your posX and posY variables in the MeinZeichenPanel class aren't null, so you don't get a NullPointerException like you did in the other post, but they are zero.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The posX and posY co-ordinates are not the same variables in the two classes.
The JPanel is being overwritten by the JLabel as they are both being added to the same location in the JFrame, hence it's paintComponent method isn't called.

Can you explain what you are trying to achieve.
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everybody for your replies.
Now I put the MouseMotionListener directly on the JFrame instead of creating a JLabel first. Now it gets into the paintComponent method, however the position of the picture doesn't change. I suppose this has to do with the variables posX and posY that you mentioned. I'm sorry but I actually don't know how to make them equal everywhere... Do you know how i could solve this?

I'm sorry if I'm a bit slow and I thank you for your patience
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh and the goal is like i described on the top, that the picture moves with the mouse cursor
 
Tony Docherty
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I'm confused about why you are using a JLabel.

I would advise against adding mouse listeners to the JFrame. You should add a JPanel to the JFrame as your root application panel and add stuff to that.

Ok so add a JPanel to the frame and add a MouseListener to the root panel.
Set the root panels layout manager to null.
Add the MeinZeichenPanel to the root panel.
In the mouse listener call the MeinZeichenPanel object's setLocation() method with the x, y position of the mouse and call repaint on the root panel.
You will also need to set the size of the MeinZeichenPanel to the size of the image which you can do once you have loaded it.
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is a root panel a special sort of panels or is it just how you call the JPanel?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just a JPanel, I just called it root to give it a name as it's the root panel of your application
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thanks. So I tried to do what you told me and I hope i did it correctly ^^
I still have the Image loading process in the paintComponent method, but i'll fix this later.
However like before it doesnt enter the paintComponent method anymore. I put two System.out.println() in the code. The one in the Listener class reports correctly the mousecoordinates, but the one in the paintComponent class doesn't appear.
I show the new code again, that may help.



 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dario Stroob wrote:I still have the Image loading process in the paintComponent method, but i'll fix this later.


That's something you should fix earlier rather than later. Also, when you need an Image why are you constructing an ImageIcon? You should load any non-animated image via ImageIO#read(...) or, for an animated GIF, Toolkint#createImage(...).
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your MeinZeichenPanel doesn't contain any components, and doesn't have a preferredSize set, so it will display at its default size in the root JPanel's default FlowLayout null layout.

A question for you, which you can answer by adding a System.out.println(...) in your code: what's the default size of an empty JPanel?

Also, all Swing methods and constructors should be invoked on the EDt, not on the Main Thread. Learning resource: Concurrency in Swing.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Toolkint#createImage


I think that should be Toolkit#createImage
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

Darryl Burke wrote:Toolkint#createImage


I think that should be Toolkit#createImage



That's righnt!
 
Dario Stroob
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys soooo much! now it finally works ^^
in the end i just needed to give the zeichenPanel the right size and I fixed the imageloading process to.
Thanks again for your patience, I know it's not easy to teach a noobie like me :P
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations on getting it working.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic