Change button color
/* I am trying to add a thread to my code that will change the button based on the color pressed for 8 seconds and then fade the color back to white and when the user click
* on another button it will do the same thing... The user can click(the button) at least 10 times. I have done majority of the code, I just not sure where I can add the thread * and get it to change colors.......... Please help!!!
*/
// CODE:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicBorders.RolloverButtonBorder;
public class ColorThread extends JFrame implements ActionListener {
// Global variables
Thread th = new Thread();
public static JButton[] gridRef;
// size of the frame
public static ColorThread window;
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
// Creating the JFrame
public ColorThread(String title) {
super(title);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// // Creating the Thread
// Runnable changer = new Runnable(){
// public void run(){
// };
// Create JFrame layout
window = new ColorThread(" CS 302 Lab HW 7");
window.setLayout(new BorderLayout());
// 8 x 8 Gridlayout
int rows = 8;
int cols = 8;
// North(top) JPanel added to the JFrame
JPanel text = createTracking(); // calling creatTracking
window.add(text, BorderLayout.NORTH);
// Center(middle) JPanel added to the JFrame
JPanel grid = createGrid(rows, cols); // calling createGrid
window.add(grid, BorderLayout.CENTER);
// South(bottom) JPanel added to the JFrame
JPanel button = createButtons(); // calling createButton
window.add(button, BorderLayout.SOUTH);
// setting the JFrame visible
window.setVisible(true);
}
// createCrid method - will be used to go to the center of the BorderLayout
public static JPanel createGrid(int rows, int cols) {
// panel to hold grid
JPanel grid = new JPanel();
// grid's buttons 8x*
gridRef = new JButton[rows * cols];
// grid layout
grid.setLayout(new GridLayout(rows, cols));
// background of the grid
grid.setBackground(Color.WHITE);
// loop to add buttons 0 -63, with buttons label 0-63
for (int i = 0; i < 64; i++) {
JButton button = new JButton(Integer.toString(i));
// add buttons to the grid
grid.add(button);
// gridRef to store the array of buttons
gridRef[i] = button;
}
// return the grid that will go to the center panel
return grid;
}
// createButtons - will be used to go to the bottom of the BorderLayout
public static JPanel createButtons() {
// panel to hold bottons
JPanel button = new JPanel();
// panel layout
button.setLayout(new GridLayout(1, 3));
// create buttons for bottom panel
JButton red = new JButton("Red");
JButton green = new JButton("Green");
JButton blue = new JButton("Blue");
// bottom panel button listeners
red.addActionListener(window);
green.addActionListener(window);
blue.addActionListener(window);
// adding buttons to the bottom panel
button.add(red);
button.add(green);
button.add(blue);
// return the buttons that will go to the bottom panel
return button;
}
// creatTracking method - will be used to go to the top of the BorderLayout
public static JPanel createTracking() {
// panel to hold text field
JPanel text = new JPanel();
// layout of top panel
text.setLayout(new GridLayout(1, 3));
// text field for tRed - set text field disable
JTextField tRed = new JTextField();
tRed.setEnabled(false);
// text field for tGreen - set text field disable
JTextField tGreen = new JTextField();
tGreen.setEnabled(false);
// text field for tBlue - set text field disable
JTextField tBlue = new JTextField();
tBlue.setEnabled(false);
// adding text field to top panel
text.add(tRed);
text.add(tGreen);
text.add(tBlue);
// return the text field that will go to the top panel
return text;
}
@Override
public void actionPerformed(ActionEvent e) {
String currentColor = e.getActionCommand();
Color color;// (0,255,0);
/*
* conditions for what button is pressed on the bottom panel("Red", "Green", "Blue"),
* then that will color the button that color will correspond to buttons on the grid
*/
if (currentColor.equals("Red"))
color = Color.RED;
else if (currentColor.equals("Green"))
color = Color.GREEN;
else
color = Color.BLUE;
// setting the buttons to be randomly chosen when clicked by the bottom panel color buttons
int random = (int) (Math.random() * 64);
// will hold the color of the button in the grid using an index array [random]
gridRef[random].setBackground(color); // JButton Object
}
}