Reputation: 1154
I'm making a "GUI from Hell", and I'm trying to make the JFrame flash colors (change background rapidly) for a time long enough to be annoying. This is what I've got:
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 to easily make the entire JFrame and all of its contents change color (not just the background), that would be crazy and awesome, so let us in on that.
Any guidance is appreciated! Hopefully I haven't just missed something silly...
...and if you have an idea or two for a ridiculous GUI effect, feel free to share! :)
Upvotes: 3
Views: 8273
Reputation: 7706
Finally, something for fun...Give this a try. 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)))));
}
}
}
}
Upvotes: 2
Reputation: 36703
Here's my take on your GUI from hell which seems to work OK. It's pretty intense. How are you performing the updates? On another thread?
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();
Upvotes: 3
Reputation: 109813
I'd suggest to use Swing Timer for repeatedly events to the Swing GUI, maybe this example can help you with another yours dreams
JFrame
is not possible to be colored, but works for ContentPane
g.e.JFrame.getContentPane.setColor(Color.red)
Upvotes: 2