aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Graphics2D g2 = (Graphics2D)g; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Graphics2D g2 = (Graphics2D)g;" Watch "Graphics2D g2 = (Graphics2D)g;" New topic
Author

Graphics2D g2 = (Graphics2D)g;

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

when and why should i do this?


or this


i read part of the tutorial and (at least some of) the stuff they said i needed Graphics2D for i can do just using the Graphics g that i get passed to me.
i set the Stroke no problem.


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
You need to look which methods are specific to the Graphics2D class. I think what is passed is an instance of a subclass of Graphics2D, but it is declared as Graphics. Graphics and Graphics2D are abstract classes, subclasses of each other. You might do well to use the create() method to create a copy of the object if you do anything complicated, eg shear().
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3527
    
  15
Randall Twede wrote:(at least some of) the stuff they said i needed Graphics2D for i can do just using the Graphics g that i get passed to me.
i set the Stroke no problem.

Are you sure ? Graphics doesn't have a setStroke method.


Joanne
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

here is the code. it works.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3527
    
  15
Once I'd fixed the missing . error on line 32, I get this if i try to compile that code
"Shape.java": B:\Greenwich\src\com\tests\Shape.java:31: cannot find symbol at line 31, column 0
symbol : method setStroke(java.awt.BasicStroke)
location: class java.awt.Graphics
g.setStroke(new BasicStroke(stroke));

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i don't know it works for me. as you figured out there are other classes. maybe i'll try compiling each separately again and see if there an error.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

nope. all i get is a warning from one class about using unchecked or unsafe operations. i first wrote it in 2004. it compiles and runs for me using java7.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3527
    
  15
Randall Twede wrote:as you figured out there are other classes

i didn't have any problems with missing classes - it was just a missing . on line 32 between this and color. If you copy and pasted this code into the post above, then this is not the code you are compiling.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

oh you are right.
now i am even more confused because it changes color fine also.
that is the code and it compiles.
i was in the wrong folder(previous version).
i get the error now.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

what about
g2d.dispose();
is that something you should always do?

wow. that class gave a compile error, but i can run the program and it all works.
oh, i get it. it was never compiled so the program uses the older(preexisting) .class file
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

I think what is passed is an instance of a subclass of Graphics2D, but it is declared as Graphics.

yes, this is my thinking also
I think what is passed is an instance of a subclass of Graphics2D, but it is declared as Graphics.
i could maybe say public void draw(Graphics2D g)
instead of casting.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Randall Twede wrote:what about
g2d.dispose();
is that something you should always do?

If you have created the Graphics object using either overload of create(...) then you are responsible for dispose()ing it. Failure to do so can prevent system resources form being released.

Never call dispose() on the passed-in Graphics reference. The Graphics object it refers to may be used after your painting method returns.


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Randall Twede wrote:i could maybe say public void draw(Graphics2D g)
instead of casting.

You would still have to cast in order to call your method from a Swing painting method e.g.Anywhere you need to treat a more general type -- a superclass -- as a more specific type -- a subclass -- you can't avoid casting (someone correct me if that's wrong).
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

thanks Darrly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Graphics2D g2 = (Graphics2D)g;