Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Graphics2D g2 = (Graphics2D)g;

 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the code. it works.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Darrly
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic