aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Can someone please help me understand this code? 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 someone please help me understand this code?" Watch "Can someone please help me understand this code?" New topic
Author

Can someone please help me understand this code?

Ron Lavery
Greenhorn

Joined: Oct 01, 2011
Posts: 6
I'm trying to learn basic Java on my own. This program I'm trying to do from my book confuses me - - it's probably a bit over my head at this point - - but I would like to try and work my way through it.

I have some specific questions, but also any help walking me through the code would be greatly appreciated.

line 11: what class does this add(p) function come from?
line 33: what's going on here - - where did the function come from, and what is this parameter?
line 35: what's going on here - - where did the function come from, and what is this parameter?
line 39: what class does this repaint(p) function come from?
line 44: how does this paintComponent method get executed?
line 46: what does this do?



Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
Well, I'm pretty new myself, but the classes you have extend JFrame and JPanel, so they inheret a bunch of methods. That's where the methods come from. PaintComponent is an overridden method, you gotta over ride it if you want to draw graphics on a JPanel, I think it ends up getting called when you call repaint();. JPanels are things that go on a JFrame. When you add(p) in the MoveMessageDemo constructor, you are adding the JPanel to the JFrame. Anyway I haven't seen a MouseMotionAdapter before, or seen it done exactly this way, but MoveableMessagePanel is an inner class, and you usually use one of those if you are dealing with events, they are classes that listen for events like a mouse click buton push or something, and I would guess that MouseMotionAdapter is a special class included in the event or swing package that has the purpose of listening for mouse movements. When you override mouseDragged, that's where you put the code of what to do when the mouse gets moved. So, I hope that helped!
Ron Lavery
Greenhorn

Joined: Oct 01, 2011
Posts: 6
Benjamin Thvedt wrote:Well, I'm pretty new myself, but the classes you have extend JFrame and JPanel, so they inheret a bunch of methods. That's where the methods come from. Overall I understand this - - but could not find the specifics of which class they come from. PaintComponent is an overridden method, you gotta over ride it if you want to draw graphics on a JPanel, I think it ends up getting called when you call repaint();. I almost understand this - - but how does calling repaint() invode paintComponent(), especially when paintComponent has a parm? - - that is, what's the link so that calling one name invokes the other?? JPanels are things that go on a JFrame. When you add(p) in the MoveMessageDemo constructor, you are adding the JPanel to the JFrame. Got it, thanks. Anyway I haven't seen a MouseMotionAdapter before, or seen it done exactly this way, but MoveableMessagePanel is an inner class, and you usually use one of those if you are dealing with events, they are classes that listen for events like a mouse click buton push or something, and I would guess that MouseMotionAdapter is a special class included in the event or swing package that has the purpose of listening for mouse movements. When you override mouseDragged, that's where you put the code of what to do when the mouse gets moved. So, I hope that helped! Yeah - this is where I'm especially confused: the addMouseMotionListener method must be inherited from JPanel? And we're creating a new MouseMotionAdapter() object while overriding the mouseDragged method in that object? Who passes the "MouseEvent e" parm to this method?


Thanks a lot! - - please see my follow-up above. I thought I understood each of these concepts individually, but all put together like this in one class has me a bit confused. . .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

If you open the Javadoc pages of a class like JFrame or JPanel, you will first see all of the methods that are declared in the class itself. Below that you will see the methods it inherited from its super class, then the methods it inherited from the super class' super class, etc. You can use this to find out exactly in what class a method is declared.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please avoid coloured text; many people find it difficult to read.
Benjamin Thvedt
Ranch Hand

Joined: Jul 29, 2011
Posts: 31
I'd be happy to elaborate, but that's really almost the extent of my current knowledge to be honest. Not sure how MouseEvent gets passed to the method, or what exactly happens in reapaint() other than calling the paintComponent method which always takes a graphics g reference (which can be cast to a graphics2D object by the way, giving you more functionality). Yeah, I don't really know what's going on underneath the surface. But I'm just a beginner myself too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone please help me understand this code?