How can I change the background color of a JFrame?
I'm building a "GUI from Hell" and I've been trying to render a JFrame with flash colors (change background quickly) for long enough to be annoying. This is what I have:
int changes = gen.nextInt(2000) + 5000;
int red;
int green;
int blue;
Color color;
for (int i = 0; i < changes; i++)
{
color = new Color(gen.nextInt(256), gen.nextInt(256),
gen.nextInt(256));
// I first tried this...
frameMain.getContentPane().setBackground(color);
// Then I tried this, which only
// appeared to change the color once and then proclaim
// that it was done:
panel1.setBackground(color);
panel2.setBackground(color);
panel3.setBackground(color);
}
Note. ... If you know how easy it is to make the entire JFrame and all of its contents change color (not just the background), that would be crazy and awesome, so let's take care of that.
Any guidance is appreciated! Hope I'm not just missing something stupid ...
... and if you have an idea or two for a funny GUI effect, feel free to share them! :)
source to share
Here's my take on your GUI from hell that seems to work fine. It's pretty intense. How do you update? To another stream?
final JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.getContentPane().setLayout(new GridLayout(3, 1, 20, 20));
final JPanel[] panels = new JPanel[3];
for (int i = 0; i < panels.length; i++) {
panels[i] = new JPanel();
panels[i].setOpaque(true);
frame.getContentPane().add(panels[i]);
}
frame.setVisible(true);
ActionListener action = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Random gen = new Random();
Color color = new Color(gen.nextInt(256), gen.nextInt(256),
gen.nextInt(256));
frame.getContentPane().setBackground(color);
for (int i = 0; i < panels.length; i++) {
color = new Color(gen.nextInt(256), gen.nextInt(256),
gen.nextInt(256));
panels[i].setBackground(color);
}
}
};
Timer t = new Timer(100, action);
t.setRepeats(true);
t.start();
source to share
I suggest using Swing Timer for multiple events in Swing GUI, maybe this example can help you with your other dreams
JFrame
impossible to paint but works for ContentPane
geJFrame.getContentPane.setColor(Color.red)
source to share
Finally, something for fun ... Try it. Use it like any JFrame.
class JFrameWild extends JFrame {
private static final long serialVersionUID = 666L;
public JFrameWild(String string) {
super(string);
Thread thread = new Thread(new Runnable() {
public void run() {
while (true) {
yoyoMama(JFrameWild.this);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.setDaemon(true);
thread.start();
}
private void yoyoMama(Object object) {
if (object instanceof Container) {
Container c = (Container) object;
Component[] components = c.getComponents();
for (Component component : components) {
yoyoMama(component);
// put extra "wild" stuff here
component.setBackground((new Color((int) (Math.random() * (double) (0xFFFFFF)))));
}
} else {
if (object instanceof Component) {
Component component = (Component) object;
// put extra "wild" stuff here
component.setBackground((new Color((int) (Math.random() * (double) (0xFFFFFF)))));
}
}
}
}
source to share