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 9 of 9

Thread: odd message from Component.getGraphics()

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default odd message from Component.getGraphics()

    I used this code:

    System.out.println(comp.getGraphics());

    Result:
     sun.java2d.SunGraphics2D[font=javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=plain,size=12],color=sun.swing.PrintColorUIResource[r=51,g=51,b=51]]

    Can someone decode that for me? I'm not sure I understand it right.

    Thanks.
    -Mel
    Last edited by Melawe; May 26th, 2012 at 08:49 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: odd message from Component.getGraphics()

    That is what the toString() method returns for the Graphics object returned by that method, and it describes the properties of the object.

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: odd message from Component.getGraphics()

    I was trying to get the Graphics object associated with a Canvas object, then use it's drawString method. I'm making a program that handles key events and then draws them on a Canvas object. One class takes care of the input, and the other the GUI. The class which takes care of the input should also draw on the canvas. I don't think I need to do it that way, but thought I'd see if its possible.

    My problem is I can't understand the string the method returned, so I can't call its drawString method.


    Thanks.
    -Mel

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: odd message from Component.getGraphics()

    My problem is I can't understand the string the method returned, so I can't call its drawString method.
    I'm a bit confused, I'm not sure what the value toString() returns has to do with your code using drawString().

  5. #5
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: odd message from Component.getGraphics()

    I want to get the graphics object associated with the canvas object, then call its drawString() method. So if its named graph, I can call graph.drawString().

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: odd message from Component.getGraphics()

    Quote Originally Posted by Melawe View Post
    I want to get the graphics object associated with the canvas object, then call its drawString() method. So if its named graph, I can call graph.drawString().
    Again, I'm still confused. What does the toString() method of Graphics have to do with calling its drawString() method?

    (and FYI if you wish to draw to a component you should do so using the paintComponent, not by calling getGraphics())

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: odd message from Component.getGraphics()

    The thing about variables is that they are names that *your* code gives to things. The things themselves don't have names intrinsically. So if you want that graphics context to be called graph then you go right ahead and declare such a variable and assign (a reference) to it.

    Graphics2D graph = (Graphics2D)comp.getGraphics();
    // later...
    graph.drawString(/*etc*/);

    But, as copeg noted, this is altogether the wrong way to draw a string: use the component's paintComponent() method.

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: odd message from Component.getGraphics()

    The problem with the paintComponent() is I can't use it in an if-else statement. The if-else statment checks what key was pressed, and ignores it if it is a certain key.

  9. #9
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: odd message from Component.getGraphics()

    I think I found a workaround, might be messy code, but worth a test.

Similar Threads

  1. component classes
    By casperl90 in forum AWT / Java Swing
    Replies: 2
    Last Post: August 8th, 2011, 03:17 AM
  2. [SOLVED] Painted component won't show up
    By Fermen in forum Object Oriented Programming
    Replies: 2
    Last Post: June 19th, 2011, 04:24 PM
  3. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 06:39 PM
  4. component
    By nasi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 4th, 2010, 07:40 PM
  5. Replies: 3
    Last Post: February 1st, 2010, 12:24 AM