Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree1Likes

Thread: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

  1. #1
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    So how can I do this? I have the following code:

    Java Code:
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		this.setBackground(Color.BLACK);
    		g.setColor(Color.RED);
    		g.fillRect(character.x, character.y, character.width, character.height);
    		
    		if(right){
    			character.x += 1;
    		}
    		
    		if (left){
    			character.x -=1;
    		}
    		
    		if (forward){
    			character.y -= 1;
    		}
    		
    		if (backward){
    			character.y += 1;
    		}
    		repaint();
    		g.drawString(("iKeirNez's First Game Version " + Main.version) , 180, 180);
    	}
    Notice the last line, how can I change its color and get it to display in the bottom left? Just now with 180, 180 it displays in the center of the screen.

    Thanks
    Keir

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    You probably SHOULDN'T set the background inside paintComponent(). You set the color by calling g.setColor(). You change its position by changing the x and y values passed into g.drawString().
    Last edited by KevinWorkman; 04-04-2012 at 07:43 PM. Reason: Not sure how I missed an entire word, I hope it wasn't too confusing!
    iKeirNez likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by KevinWorkman View Post
    You probably set the background inside paintComponent(). You set the color by calling g.setColor(). You change its position by changing the x and y values passed into g.drawString().
    I already did g.setColor but I only wanted it to do that for the rectangle, do I have to create another paintComponent for the version? Also I know I can change the location by changing the X and the Y but what would I type for the X and the Y if I wanted it in the bottom right corner?

    Keir

    EDIT: I figured out the first bit on how to set a different color.

    Java Code:
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		this.setBackground(Color.BLACK);
    		g.setColor(Color.RED);
    		g.fillRect(character.x, character.y, character.width, character.height);
    		
    		if(right){
    			character.x += 1;
    		}
    		
    		if (left){
    			character.x -=1;
    		}
    		
    		if (forward){
    			character.y -= 1;
    		}
    		
    		if (backward){
    			character.y += 1;
    		}
    		
    		g.setColor(Color.WHITE);
    		g.drawString(("iKeirNez's First Game Version " + Main.version) , 180, 180);
    		repaint();
    	}
    But I still don't know what numbers to change the X and Y to for it to be a the bottom right.

    Keir

    EDIT2: Figured out the X and Y, for my it was X:270 and Y:360
    Last edited by iKeirNez; 04-04-2012 at 06:41 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Do NOT set the background or in any other way change the state of a component in a painting method override.

    Do NOT call repaint() from inside a painting method.

    That code you posted probably pins your CPU.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    what numbers to change the X and Y to for it to be a the bottom right.
    That would be the max values of x (minus length of String) and y.

    Experiment with larger values. Get the size of the component and subtract from the lower righthand corner's x,y position to leave room to draw the String.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    And to find how much space the String will occupy, use the methods of FontMetrics or TextLayout

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by DarrylBurke View Post
    Do NOT set the background or in any other way change the state of a component in a painting method override.

    Do NOT call repaint() from inside a painting method.

    That code you posted probably pins your CPU.

    db
    I am new to Java so where else would I change the state of the component? And where else could I call the repaint() function?

    Thanks
    Keir

  9. #9
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by Norm View Post
    That would be the max values of x (minus length of String) and y.

    Experiment with larger values. Get the size of the component and subtract from the lower righthand corner's x,y position to leave room to draw the String.
    Quote Originally Posted by DarrylBurke View Post
    And to find how much space the String will occupy, use the methods of FontMetrics or TextLayout

    db
    Again I am new to Java so a code example would be nice. Also if I use the values X: 266 and Y:360 it works fine, until I put it into fullscreen mode.

    Keir

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    where else would I change the state of the component
    In the program logic. The paint method draws the state of the component determined by the program's logic.
    You call repaint after the logic has finished changing the state of the component and are ready to have it drawn in the paint method.

    If the size of the area being drawn on changes, then you will need to get the size of the area and change where you draw to have the output placed where you want it. For example if the output is 50 pixels long then the x location would be the width - 50. If the height of the output is 12 then the y would be the height - 12.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    You can query the state of the component in a painting method. Just don't change it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by DarrylBurke View Post
    You can query the state of the component in a painting method. Just don't change it.

    db
    How can I change the states of the component outside of the painting method then? Code example? Sorry I am very new to Java.

    keir

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    What state do you want to change? Look at the API doc for the component classes you are using. The method names that begin with "set" will change the state. An example for some component:
    refToComp.setProperty(<new value>); // change a value for the component pointed to by refToComp
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by Norm View Post
    What state do you want to change? Look at the API doc for the component classes you are using. The method names that begin with "set" will change the state. An example for some component:
    refToComp.setProperty(<new value>); // change a value for the component pointed to by refToComp
    I'm going to be setting the color, background and font.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    I'm sure there are set methods in the classes you are using to do that.

    The one setting you would do in the paint method is to set the color for the Graphics object passed to the paint method so that the next shape you draw would be the desired color: g.setColor(Color.red);
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by Norm View Post
    I'm sure there are set methods in the classes you are using to do that.

    The one setting you would do in the paint method is to set the color for the Graphics object passed to the paint method so that the next shape you draw would be the desired color: g.setColor(Color.red);
    I have done that in the code but apparently that eats CPU.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    No, the setColor method will not eat cpu. That is the only way to change the color when using a Graphics object to draw shapes in a paint method.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by iKeirNez View Post
    I have done that in the code but apparently that eats CPU.
    It's the setBackground(...) and repaint(...) that are pinning the CPU. Not any method you invoke on the Graphics reference.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    iKeirNez is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by DarrylBurke View Post
    It's the setBackground(...) and repaint(...) that are pinning the CPU. Not any method you invoke on the Graphics reference.

    db
    Ah ok, how else can I do this then?

  20. #20
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right

    Quote Originally Posted by iKeirNez View Post
    Ah ok, how else can I do this then?
    Don't call them from inside paintComponent(). Why would you have to call them in the middle of painting?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 1 of 2 12 LastLast

Similar Threads

  1. Changing Image color
    By Frecow in forum Java 2D
    Replies: 0
    Last Post: 04-04-2011, 10:16 AM
  2. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 05:54 AM
  3. Changing text color in SWT
    By ourimaler in forum SWT / JFace
    Replies: 1
    Last Post: 06-02-2010, 01:08 PM
  4. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 07:29 AM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 09:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •