• 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

Creating an object variable.

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me understand this.
Here is the part of the code.



so the part that confuses me is that "context" is a variable that holds an FontRenderContext object type and assigned a value(FontRenderContext type) returned from getFontRenderContext() method in Graphics2D class.
Is this different OR same concept as creating an instance of an object from a subclass?
Person person1 = new Employee();
When Employee class extends Person class.
How are these two differ?

This is quite confusing. Thanks in advance!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Albert Park wrote:
"context" is a variable that holds an FontRenderContext object



It holds a reference to a FontRenderContext object.

assigned a value(FontRenderContext type) returned from getFontRenderContext() method in Graphics2D class.
Is this different OR same concept as creating an instance of an object from a subclass?
Person person1 = new Employee();
When Employee class extends Person class.
How are these two differ?



You're mixing two totally independent concepts.

Subclass or not has nothing to do with it. One is a case of creating a new object with the new operator, and the other is a case of calling a method that returns a reference to an object. That method might itself create a new object, or it might just return a reference to an existing object.

In both the method case and the new operator case, the returned reference can point to an object of the type specified on the LHS, or any subtype.

 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the case you are showing us, there is a font rendering context object associated with the current graphics object. That method allows you to obtain a reference to it. It might be created in that method, or supplied from a field in the graphics class, and those possibilities do not make any difference to how you use it.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I think I am kind of getting it.
I need to think about this more...

This is what javadoc says
public abstract FontRenderContext getFontRenderContext()
Get the rendering context of the Font within this Graphics2D context. The FontRenderContext encapsulates application hints such as anti-aliasing and fractional metrics, as well as target device specific information such as dots-per-inch. This information should be provided by the application when using objects that perform typographical formatting, such as Font and TextLayout. This information should also be provided by applications that perform their own layout and need accurate measurements of various characteristics of glyphs such as advance and line height when various rendering hints have been applied to the text rendering.

So we can replace the last line with Rectangle2D bounds = f.getStringBounds(message, new FontRenderContext())? only if FontRenderContext constructor in FontRenderContext class was not protected?
Then isn't it pretty much same as creating a new object like the employee one?

What exactly is getFontRenderContext method returning here other than making a new object of FontRendercontext class?

Someone stop me before I confuse the heck out of myself.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Albert Park wrote:
So we can replace the last line with Rectangle2D bounds = f.getStringBounds(message, new FontRenderContext())?



If that constructor is accessible, and if all the method does is create a new FRC using that constructor, then yes.

However, if the method pulls an existing FRC from a cache, or invokes a different constructor, or does some initialization after creating it and before returning it, then, no, we can't just replace the method call with the constructor.

In fact, there's nothing special about constructors in this situation. In general, we can't replace a method call with some other code unless that code does the same thing as what's in the body of the method.

What exactly is getFontRenderContext method returning here other than making a new object of FontRendercontext class?



We don't know and we don't care, beyond what the documentation tells us. This is one of the reasons for methods to exist--so that we can separate what the operation is (get me an FRC) from how it's done (create a new one, initialize it a certain way, or return one from a cache). And then we can change the "how" without changing the "what" if there's a good reason to do so.
 
Albert Park
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Albert Park wrote:
So we can replace the last line with Rectangle2D bounds = f.getStringBounds(message, new FontRenderContext())?



If that constructor is accessible, and if all the method does is create a new FRC using that constructor, then yes.

However, if the method pulls an existing FRC from a cache, or invokes a different constructor, or does some initialization after creating it and before returning it, then, no, we can't just replace the method call with the constructor.

In fact, there's nothing special about constructors in this situation. In general, we can't replace a method call with some other code unless that code does the same thing as what's in the body of the method.

What exactly is getFontRenderContext method returning here other than making a new object of FontRendercontext class?



We don't know and we don't care, beyond what the documentation tells us. This is one of the reasons for methods to exist--so that we can separate what the operation is (get me an FRC) from how it's done (create a new one, initialize it a certain way, or return one from a cache). And then we can change the "how" without changing the "what" if there's a good reason to do so.



hmm.. so I should just memorize this whenever I need to reference to bounds related object(which requires FontRenderContext referenced object required in parameter) from Graphics.
But I do understand the difference of these two I was confused about earlier.
Really appreciate both of your help!.
 
reply
    Bookmark Topic Watch Topic
  • New Topic