Java Graphics vs Graphics2D

Discussion in 'Mac Programming' started by jsmwoolf, Sep 1, 2011.

  1. jsmwoolf macrumors regular

    Joined:
    Aug 17, 2011
    #1
    Sorry if I'm asking too many questions in a short amount of time, but I was wondering if its best to use the Graphics class or Graphics2D class?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Yes, it's definitely better to use one of those. If you want an opinion on which one, you'll have to be more specific.

    -Lee
     
  3. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #3
    How about if the shape always changes? The graphing part of my Calculator app uses the Graphics class for drawing the coordinate plane. However, I plan on using Graphics2D for drawing the shapes because it can deal with complex shapes.

    Even though this question doesn't necessarily relate to this post, but it is possible with either class to restrict drawing within a Dimension object so that once it passes a point, it would no longer draw?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Do you realize that Graphics2D is a subclass of Graphics? So any method Graphics provides is also provided by Graphics2D.

    Have you looked at any examples using Graphics2D to draw shapes? Such as one of the chart/graph packages I mentioned earlier.


    Do you mean clipping? Like with the Graphics2D methods clip(), clipRect(), or setClip()?

    What reference docs are you consulting for drawing with a Graphics or Graphics2D? What books or tutorials have you used up to this point?
     
  5. jsmwoolf thread starter macrumors regular

    Joined:
    Aug 17, 2011
    #5
    Yes, I have looked on Oracle Java section, on their online documentation for Java, and on online forums. And yes, I also know that Graphics2D is a child class of Graphics. However, aren't there times when it's appropriate to use Graphics instead of Graphics2D?

    Did some research and yes I meant clipping. I drew a rectangle based off of a point(which is the starting point for the rectangle, not the origin) and set the size based off a Dimension object. I declared the object like this using this Constructor:

    Code:
    Rectangle(Point p, Dimension d) 
    I suppose clipRect would be my best option as I tied the dimension with the Rectangle object?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Think it through. Graphics2D can do everything Graphics does, and more. This isn't just a marketing pitch, it's an actual API guarantee because of the subclass relationship. So think of a reason why you'd use a Graphics with fewer features, instead of a Graphics2D.

    The only reason I can think of is if you're writing retro-code, which must run on a JVM version before Graphics2D was introduced. In that case, the reason for not using Graphics2D is because it doesn't exist on the target JVM.
     

Share This Page