I have some code that creates a full screen icon in java and sets the background color to pink and the foreground color to red. However every time i run it, it never changes the
The painting of the background is done in the paint function. So, you have to invoke super.paint(g)
at the start of the paint function.
Also, you need to override the setBackground function.
So the code becomes:
public void paint(Graphics g){
super.paint(g);
g.drawString("This is gonna be awesome", 200, 200);
}
public void setBackground(Color color){
super.setBackground(color);
getContentPane().setBackground(color);
}
public void paint(Graphics g){
g.drawString("This is gonna be awesome", 200, 200);
}
The painting of the background is done in the paint()
method. Your overrode the method and didn't invoke super.paint(g)
so the background never gets painted.
However, this is NOT the way to do custom painting. You should NOT override the paint() method of a JFrame. If you want to do custom painting then override the paintComponent()
method of a JPanel
and then add the panel to the frame.
Read the section from the Swing tutorial on Custom Painting for more information.
Edit:
Once you add the super.paint(g), child components of the frame will be painted. This means the content pane gets painted and the content pane is painted over the frame so you won't see the background of the frame, so you also need to add:
//setBackground(Color.PINK);
getContentPane().setBackground(Color.PINK);