Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background color in java Graphics object

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.

like image 380
Panadol Chong Avatar asked Aug 19 '15 16:08

Panadol Chong


People also ask

How do you change the background color in Java?

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.

How do I change the background color in an applet?

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.

How do I change the color of a Jbutton?

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 8?

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 −.

Is it possible to set color for the whole graphics object?

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.

What is RGB color code in Java?

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.

How do you set the background color of a rectangle?

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.


1 Answers

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 .

like image 68
Madhawa Priyashantha Avatar answered Oct 03 '22 01:10

Madhawa Priyashantha