1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
//I ended up not using the online example at all, it was only making it more difficult
//this is my doing from top to bottom
package isay;
import javax.swing.*;//using this for the delay on the gameplay
import java.awt.*;//used for grid
import java.awt.event.*;//used for event listener
import java.util.Random;//using this for the random button selector
/**
*
* @author Sir Rudy Typealot
*/
public class ISay extends JFrame
{
private final int windowTall=700;//height of window in pixels
private final int windowWide=700;//width of window in pixels
private boolean Blue=(false);
private boolean Red=(false);
private boolean Green=(false);
private boolean Yellow=(false);
private JButton[] ColorButton;
private JPanel[] BtnLayout;
private JButton sequencer=new JButton();
Dimension btnSize= new Dimension (125, 125);
public ISay()
{
setTitle("I Say Let's Play!!!");//sets the title of the game
setSize(windowWide, windowTall);//sets the dimensions of window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes the window when x button is pressed
//this for section of code will create and add the 4 color buttons and it will also create the action listener for the corresponding buttons
ColorButton=new JButton[4];
BtnLayout= new JPanel[5];
for(int w=0; w<5; w++)
{
BtnLayout[w]=new JPanel();
}
sequencer=new JButton("Start/Reset");
for (int x=0; x<4; x++)
{
ColorButton[x]=new JButton();
ColorButton[x].setPreferredSize(btnSize);
ColorButton[x].addActionListener(new ColorButtonListener());
}
BtnLayout[0].add(ColorButton[0]);
BtnLayout[1].add(ColorButton[1]);
BtnLayout[2].add(ColorButton[2]);
BtnLayout[3].add(ColorButton[3]);
BtnLayout[4].add(sequencer);
add(BtnLayout[0], BorderLayout.NORTH);
add(BtnLayout[1], BorderLayout.WEST);
add(BtnLayout[2], BorderLayout.SOUTH);
add(BtnLayout[3], BorderLayout.EAST);
add(BtnLayout[4], BorderLayout.CENTER);
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
sequencer.addActionListener(new sequencerListener());//action listener for the sequence creator
pack();
setVisible(true);//displays the window
setResizable(false);
}
private class sequencerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int sequence[]=new int[10];
Random rand=new Random();
for (int r = 0; r < 10; r++)
{
sequence[r] = rand.nextInt(4)+1;
if(sequence[r]==1)
{
ColorButton[0].setBackground(Color.black);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
System.out.print("Blue, ");
}
if(sequence[r]==2)
{
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.black);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
System.out.print("Red, ");
}
if(sequence[r]==3)
{
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.black);
ColorButton[3].setBackground(Color.yellow);
System.out.print("Green, ");
}
if (sequence[r]==4)
{
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.black);
System.out.print("Yellow, ");
}
}
}
}
private class ColorButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int x=0; x<10;x++)
{
if(e.getSource() == ColorButton[0])
{
Blue=true;
ColorButton[0].setBackground(Color.black);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
e.setSource(0);
Blue=false;
}
else if(e.getSource() == ColorButton[1])
{
Red=true;
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.black);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.yellow);
e.setSource(0);
Red = false;
}
else if(e.getSource() == ColorButton[2])
{
Green=true;
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.black);
ColorButton[3].setBackground(Color.yellow);
e.setSource(0);
Green = false;
}
else if(e.getSource() == ColorButton[3])
{
Yellow=true;
ColorButton[0].setBackground(Color.blue);
ColorButton[1].setBackground(Color.red);
ColorButton[2].setBackground(Color.green);
ColorButton[3].setBackground(Color.black);
e.setSource(0);
Yellow = false;
}
}
}
}
public static void main(String[] args)
{
new ISay();
}
}
|