Reputation: 2330
I'm trying to change the background of a JPanel inside of a JFrame. JFrame consist of JPanels much like a grid. I"m trying to change a random JPanel inside of the JFrame and see the color change on each pass through the loop. Here is what I've got so far.
private static JPanel panel;
private static int index;
public static void main(String[] args)
{
JFrame window = new JFrame();
window. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
window.setSize(600, 600);
GridLayout layout = new GridLayout(50, 50, 3, 3);
panel = new JPanel(layout);
panel.setSize(600, 600);
//Initialize the JFrame with JPanels that
//are all white.
InitializeGrid(panel);
window.add(panel);
window.validate();
window.setVisible(true);
MainLoop();
}
private static void MainLoop()
{
Component[] componentArrayTwo = panel.getComponents();
int index = 0;
while(true)
{
JPanel currentPanel = (JPanel) componentArrayTwo[index];
currentPanel.setBackground(Color.GREEN);
index++;
if(index == 1600)
{
//Restart again
index = 0;
}
//Colors everything back to white.
Reset();
}
}
private static void InitializeGrid(JPanel panel)
{
//40 x 40 = 1600
for(int i = 0; i < 1600; i++)
{
JPanel cellPanel = new JPanel();
cellPanel.setMinimumSize(new Dimension(3, 3));
cellPanel.setMaximumSize(new Dimension(3, 3));
cellPanel.setPreferredSize(new Dimension(3, 3));
cellPanel.setBackground(Color.WHITE);
panel.add(cellPanel);
}
}
/*************************************************************************/
//NAME: Reset
//DESCRIPTION: Resets all the cells back to white which is executed on
//each iteration through the loop.
/*************************************************************************/
private static void Reset()
{
Component[] componentArrayTwo = panel.getComponents();
for(Component individualComponent : componentArrayTwo )
{
JPanel panelToColor = (JPanel) individualComponent;
panelToColor.setBackground(Color.WHITE);
}
}
If I uncomment line the panel.add(individualPanel) this will show the color change, but it keeps adding more and more JPanels to the JFrame. However, commenting this line lets me change the color but doesn't show any changes in the JFrame. I've tried changing various pieces in this code but so far I've been unsuccessful. What should happen is that on each pass through the loop I should see a Green JPanel show up in random spots in the JFrame. If anyone could help I'd appreciate it.
Upvotes: 0
Views: 10859
Reputation: 208984
Use a javax.swing.Timer
if you want to animate with swing. See How to use Swing Timers
See more examples here and here and here and here.
Test this one out. It changes the background of just one panel, instead of trying to use multiple ones like you are trying to do.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ColorChange extends JPanel {
private static final int D_W = 300;
private static final int D_H = 300;
private final List<Color> colors;
private final Random random;
private Color bgColor = Color.BLUE;
public ColorChange() {
colors = createColorList();
random = new Random();
Timer timer = new Timer(500, new ActionListener(){
public void actionPerformed(ActionEvent e) {
int index = random.nextInt(colors.size());
bgColor = colors.get(index);
repaint();
}
});
timer.start();
}
private List<Color> createColorList() {
List<Color> list = new ArrayList<>();
list.add(Color.BLUE);
list.add(Color.CYAN);
list.add(Color.PINK);
list.add(Color.ORANGE);
list.add(Color.MAGENTA);
list.add(Color.GREEN);
list.add(Color.YELLOW);
list.add(Color.RED);
list.add(Color.GRAY);
return list;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(bgColor);
g.fillRect(0, 0, getWidth(), getHeight());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new ColorChange());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Upvotes: 4