Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone please help me understand this code?

 
Ron Lavery
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20551
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49443
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid coloured text; many people find it difficult to read.
 
Benjamin Thvedt
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic