Jump to content
  • Advertisement
Sign in to follow this  

How Is This Side Effect Happening ?

This topic is 2057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have run into an interesting side effect in my code. It seems like turning on RenderingHints for my Graphics g1 object also is affecting my Graphics g object.


 As it is written, the Graphics g object should not be anti-aliased, only g1 object .... am I not "getting" something here ?


public void paint(Graphics g){
     Graphics2D g1 = (Graphics2D) g;
     RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
     g.fillRect(0, 0, WIDTH + 20, HEIGHT + 20);
     g.drawString("\u263a \u263b",20,20);
Edited by Shippou

Share this post

Link to post
Share on other sites

lol Yeah, you're not getting Java :D Its the same object dude, you're just casting it to another type.


Its common to cast the incoming Graphics object to Graphics2D inside the method to do more interesting stuff with it (drawImage for example).

Share this post

Link to post
Share on other sites

yah what they are saying, you are just creating a new reference to the g object and calling it g1, your also casting it to a graphics2d so that reference treats it as a graphics2d object, which means you can use graphics2d methods on the object but it is still the same object your are performing the methods on.


If you really want to create a new object I believe there is a graphics.create() method that returns a copy.

Graphics2D g1 = (Graphics2D) g.create();

That should give you the desired effects I believe, now g1 is a COPY of the g object and it is still being cast to a Graphics2D

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!