Results 1 to 20 of 28
- 04-04-2012, 07:00 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
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); }
Thanks
Keir
- 04-04-2012, 07:23 PM #2
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 08:43 PM. Reason: Not sure how I missed an entire word, I hope it wasn't too confusing!
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-04-2012, 07:28 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
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(); }
Keir
EDIT2: Figured out the X and Y, for my it was X:270 and Y:360Last edited by iKeirNez; 04-04-2012 at 07:41 PM.
- 04-04-2012, 08:20 PM #4
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-04-2012, 08:21 PM #5
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Moved from New to Java
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-04-2012, 10:05 PM #6
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.
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.
- 04-04-2012, 10:35 PM #7
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
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-05-2012, 01:00 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
- 04-05-2012, 01:03 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
- 04-05-2012, 02:27 PM #10
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
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.
- 04-05-2012, 03:12 PM #11
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-05-2012, 04:00 PM #12
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
- 04-05-2012, 04:10 PM #13
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 refToCompIf you don't understand my response, don't ignore it, ask a question.
- 04-05-2012, 04:31 PM #14
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
- 04-05-2012, 05:22 PM #15
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.
- 04-05-2012, 05:31 PM #16
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
- 04-05-2012, 05:48 PM #17
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.
- 04-05-2012, 05:51 PM #18
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-05-2012, 05:53 PM #19
Member
- Join Date
- Apr 2012
- Posts
- 12
- Rep Power
- 0
- 04-05-2012, 06:06 PM #20
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
Similar Threads
-
Changing Image color
By Frecow in forum Java 2DReplies: 0Last Post: 04-04-2011, 11:16 AM -
Changing background color
By nikkka in forum New To JavaReplies: 4Last Post: 03-12-2011, 06:54 AM -
Changing text color in SWT
By ourimaler in forum SWT / JFaceReplies: 1Last Post: 06-02-2010, 02:08 PM -
Color-changing model
By higuchi in forum New To JavaReplies: 1Last Post: 03-19-2009, 08:29 AM -
Changing the color of text
By Lang in forum New To JavaReplies: 1Last Post: 11-04-2007, 10:51 AM
Bookmarks