-
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:
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
-
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().
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
KevinWorkman
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.
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
-
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Moved from New to Java
db
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
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.
-
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
DarrylBurke
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
Norm
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
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
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.
-
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
DarrylBurke
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
-
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
Norm
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.
-
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);
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
Norm
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.
-
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.
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
iKeirNez
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
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
DarrylBurke
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?
-
Re: Drawing a String, Changing it's Color and Displaying it at the Bottom Right
Quote:
Originally Posted by
iKeirNez
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?