Good day,
Know that in Java Graphics object, we can user the setColor()
method to set the object color. But this is only apply to the object border. Is it anyway to set color for the whole object? I means the background of the Graphics object.
void draw(Graphics g)
{
g.setColor(color);
g.drawRect(left, right, width, height);
}
Kindly advise.
We use the following code for the creation of a JFrame: JFrame f = new JFrame(); // Creation of a JFrame object named as f f. setTitle("Change Background Color"); //To set the title of the frame f. setVisible(true); // To present the frame on the screen f.
By default, the applet will usually have a grey background when displayed in a web browser. If you want to change it then you can call the setBackground(java. awt. Color) method and choose the color you want.
Normally with Java Swing you can set the background color of a button with: myJButton. setBackground(Color. RED);
How to change JFrame background color in Java. Java 8 Object Oriented Programming Programming. At first, create a JFrame −. JFrame frame = new JFrame (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setPreferredSize (new Dimension (550, 300)); Now, change the background color of the JFrame −.
Know that in Java Graphics object, we can user the setColor () method to set the object color. But this is only apply to the object border. Is it anyway to set color for the whole object? I means the background of the Graphics object. Kindly advise. Show activity on this post.
The all-capital version depicts a constant value. But lowercase version also works fine. The basic colors of color system are red, green, and blue. Java provides the Color class constructor with different RGB color codes as arguments. Many developer tools are available that helps in picking up the correct RGB value.
Set background color in java Graphics object. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
use fillRect()
method .
g.fillRect(left, right, width, height);
from javadoc
drawRect()
Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.
fillRect()
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
" this is only apply to the object border " because drawRect
draw the outlines only.
" Is it anyway to set color for the whole object? " well you misunderstand . and setColor()
set the color to what you draw if you draw a outline then you can see outline only and it's not because of .setColor()
set colors to border
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With