Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Understanding Graphics2d, canvas and shapes

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Understanding Graphics2d, canvas and shapes

    Hi,

    I am learning the basics of java games programming and I am confused about a few things.
    I know you use the "canvas" class to create a blank canvas and then use the paint method to create stuff.

    But what does graphics2d? I have seen people using the grahpics2d class to create a canvas for example

    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.BLACK);

    now why did they use the grahpics2d and not the canvas?

    also I have seen people creating shapes like a rectangle by using:
    Rectangle r = new Rectangle();

    but some people have created them like:

    Shape shape = new Rectangle2D.Double(value1,valu2,valu3,valu4);

    Whats the difference between these two?

    Thanks in advance.

    regards,


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Understanding Graphics2d, canvas and shapes

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    There are typically many ways to accomplish the same, or nearly the same, thing in Java. The way chosen is often personal preference, what was learned first, or a specific approach that supports the rest of the design, so therefore dependent on the requirements of a larger context than you've provided.

    You can learn about the differences in the approaches you've mentioned by studying the API for the classes being used. It's better that you do that research than to rely on someone else to do it for you.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Understanding Graphics2d, canvas and shapes

    Like Greg said, the API is your best friend: Java Platform SE 8

    You also need some familiarity with OOP. The Graphics class is an abstract class, and Graphics2D is the concrete implementation of it. The paintComponent() method takes a Graphics argument, but in actuality the argument is an instance of Graphics2D.

    Your question of "why did they use the grahpics2d and not the canvas" doesn't make any sense. You paint *with* an instance of Graphics2D (which is just a subclass of Graphics) *to* a Component such as Canvas or JPanel. You question is like asking "why did the artist paint with the brush instead of the canvas".

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Graphics2D Exception
    By JohnRawson in forum AWT / Java Swing
    Replies: 5
    Last Post: March 20th, 2014, 11:10 AM
  2. Force Sync on Graphics2D?
    By Gerp in forum Java Theory & Questions
    Replies: 2
    Last Post: April 13th, 2011, 09:13 PM
  3. question about transforming Graphics2D
    By gib65 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 7th, 2010, 09:53 PM
  4. java graphics2d issue
    By nana-j13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2010, 03:49 PM
  5. Replies: 1
    Last Post: November 14th, 2008, 03:00 PM

Tags for this Thread