aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes can't repaint frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "can Watch "can New topic
Author

can't repaint frame

Dario Stroob
Greenhorn

Joined: Dec 14, 2012
Posts: 10
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




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Dec 14, 2012
Posts: 10
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)
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
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

Joined: Dec 14, 2012
Posts: 10
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 07, 2007
Posts: 2289
    
  49
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

Joined: Dec 14, 2012
Posts: 10
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

Joined: Dec 14, 2012
Posts: 10
Oh and the goal is like i described on the top, that the picture moves with the mouse cursor
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
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

Joined: Dec 14, 2012
Posts: 10
Is a root panel a special sort of panels or is it just how you call the JPanel?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
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

Joined: Dec 14, 2012
Posts: 10
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.



Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

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(...).


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

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

Joined: Aug 07, 2007
Posts: 2289
    
  49
Darryl Burke wrote:Toolkint#createImage

I think that should be Toolkit#createImage
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

Tony Docherty wrote:
Darryl Burke wrote:Toolkint#createImage

I think that should be Toolkit#createImage


That's righnt!
Dario Stroob
Greenhorn

Joined: Dec 14, 2012
Posts: 10
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

Joined: Aug 07, 2007
Posts: 2289
    
  49
Congratulations on getting it working.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can't repaint frame