Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does canvas, an exteded JPanel obj have methods getSize(), createImage(size.width, size.height)?

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below code is from the book Objects First With Java by Michael Kolling and David J. Barnes

Why is the class CanvasPane not included in the Java API?.How does canvas have methods getSize(), createImage(size.width, size.height).canvas is an object of CanvasPane, CanvasPane is an extended object Of Jpanel(a paint() method is added to CanvasPane).Jpanel does not have any of the above mentioned methods




 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varuna Seneviratna wrote:
Why is the class CanvasPane not included in the Java API?


Do you mean not in the online API documentation? The online JavaDocs are only for the classes that are part of the core Java Runtime (which CanvasPane is not).

Or do you mean why isn't CanvasPane part of the core classes that are part of the Java Runtime Environment? In this case, the API can't have every imaginable type built into it. that is not its purpose. The Java API is designed to provide the building blocks on which other applications can be built. The CanvasPane would be too narrow in its usage to be included in the API.


.How does canvas have methods getSize(), createImage(size.width, size.height).canvas is an object of CanvasPane, CanvasPane is an extended object Of Jpanel(a paint() method is added to CanvasPane).Jpanel does not have any of the above mentioned methods



CanvasPane IS-A javax.swing.JPanel. JPanel IS-A javax.swing.JComponent. JComponent has a getSize() method, which means so does JPanel, and so does CanvasPane.

JComponent IS-A java.awt.Container, and Container IS-A java.awt.Component. Component has a (actually a few) creatImage() methods, which naturally means that any of its subclasses (Container, JComponent, JPanel, and CanvasPane to name a few) inherit these methods.

Take a look at the API for javax.swing.JPanel. Scroll down and you will see a bunch of Methods inherited from... sections which details which methods JPanel has by nature of inheriting them from its parent class even if JPanel doesn't specifically define them itself. As an extension of JPanel, CanvasPane gets them all too.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:CanvasPane IS-A javax.swing.JPanel. JPanel IS-A javax.swing.JContainer. JContainer has a getSize() method, which means so does JPanel, and so does CanvasPane.


I think you meant JComponent here (not JContainer). JComponent does have a getSize() method, but it takes a parameter so is not the one being used here. getSize() without a parameter comes from the java.awt.Component class.
 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve

I can understand most of your answer but I am not able to find JContainer class is it JComponent that you meant as JContainer.In the API which describes JPanel the following is included


java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by javax.swing.JComponent
extended by javax.swing.JPanel




Regards Varuna
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varuna Seneviratna wrote:I can understand most of your answer but I am not able to find JContainer class is it JComponent that you meant as JContainer.In the API which describes JPanel the following is included


Did you read my correction to Steve's post ?
 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I saw But after I have postedIt took me some time to post going through the API and so on Thank you for the help!



Regards Varuna
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bummer. Sorry for the misleading. I will edit my post so as not to lead others astray. Again, sorry for the mistake, and thanks Joanne for the correction.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic