File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating an object variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an object variable." Watch "Creating an object variable." New topic
Author

Creating an object variable.

Albert Park
Greenhorn

Joined: Feb 01, 2012
Posts: 28
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!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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

Joined: Feb 01, 2012
Posts: 28
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 01, 2012
Posts: 28
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!.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Creating an object variable.
 
Similar Threads
Size of String in Pixels from JTextField
How to force the repainting of a component
[swing]highlight text on mouse over
scaling a font when resizing by dragging
can't understand JComponent