Reputation: 19
I'm trying to create a reset button for a grid of rectangles. When running it, you'll be able to click rectangles and turn them blue - the reset button is supposed to turn them all back to white. I'm stuck at what to put in the reset method in Lifeform. It currently does nothing. I appreciate any help!
Class Grid:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Grid extends JPanel{
private int a = 50;
private int b = 50;
private Lifeform[][] Life;
private Lifeform ife;
private boolean[][] life = new boolean[a][b];
private Timer t;
private JButton reset;
private JButton run;
private JPanel panel;
Grid(){
ife = new Lifeform();
run = new JButton("Run");
reset = new JButton("Reset");
reset.addActionListener(new ResetListener());
//creates grid of rectangles
Life = new Lifeform[a][b];
int ypos = 0;
for(int i = 0; i < Life.length; i++){
int xpos = 0;
for(int j = 0; j < Life[0].length; j++){
Rectangle r = new Lifeform();
r.setBounds(xpos, ypos, 50, 50);
Life[i][j] = (Lifeform) r;
xpos += 50;
}
ypos += 50;
}
t = new Timer(64, new Movement());
this.addMouseListener(new mouse());
}
public void paintComponent(Graphics g){
for(Lifeform[] n : Life){
for(Lifeform lf : n){
g.setColor(lf.getColor());
g.fillRect((int)lf.getX(), (int)lf.getY(), (int)lf.getWidth(), (int)lf.getHeight());
}
}
for (int i = 0; i <= 25; i++){
g.drawLine(0, 50*i, 1500, 50*i);
g.setColor(Color.black);
}
for (int i = 0; i <= 25; i++){
g.drawLine(50*i, 0, 50*i, 750);
g.setColor(Color.black);
}
}
private JFrame createGrid(){
JPanel panel = new JPanel();
JFrame frame = new JFrame("Alveolate");
frame.add(run, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.add(reset, BorderLayout.SOUTH);
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,700);
frame.setVisible(true);
return frame;
}
public class mouse implements MouseListener{
//turns rectangles blue
public void mouseClicked(MouseEvent e) {
for(int i = 0; i < Life.length; i++){
for(int j = 0; j < Life[i].length; j++){
Lifeform spot = Life[i][j];
if (spot.contains(e.getPoint())) {
Color b = Color.blue;
if( spot.getColor().equals( Color.blue ) ) {
b = Color.white;
}
spot.setColor(b);
}
}
}
repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public class Movement implements ActionListener{
public void actionPerformed ( ActionEvent e ){
}
updateUI();
}
repaint();
}
}
public void startTimer(){
t.start();
}
public void stopTimer(){
t.stop();
}
private class ResetListener implements ActionListener{
public void actionPerformed( ActionEvent e ) {
ife.reset();
updateUI();
}
}
public static void main(String[] args) {
Grid ABC = new Grid();
ABC.createGrid();
ABC.startTimer();
}
}
Class Lifeform:
import java.awt.Color;
import java.awt.Rectangle;
public class Lifeform extends Rectangle {
private Color c;
public Lifeform() {
c = Color.WHITE;
}
public Lifeform(int width){
reset();
}
public Color getColor() {
return c;
}
public boolean setColor( Color c ) {
boolean rtn = false;
if( c != null ) {
this.c = c;
rtn = true;
}
return rtn;
}
public void reset() {
}
}
Upvotes: 0
Views: 1865
Reputation: 51565
In the reset method of your Lifeform class, you code this:
public void reset() {
c = Color.WHITE;
}
In the actionPerformed method of your ResetListener class, you code this:
public void actionPerformed( ActionEvent e ) {
for(Lifeform[] n : Life) {
for(Lifeform lf : n) {
lf.reset();
}
}
updateUI();
}
And that's how you reset all of the Lifeform instances.
In your Grid class, you've coded:
private Lifeform[][] Life;
and
private boolean[][] life = new boolean[a][b];
Don't ever give two fields the same name like this. It's confusing to the reader of your code.
All field names in Java should start with a lowercase letter. This helps you see the difference between a class name Lifeform and a class instance lf.
In this case, the life boolean probably should be a field in the Lifeform class. A more descriptive name would be isLife.
Upvotes: 1
Reputation: 49921
It doesn't do anything when you hit the Reset button because reset (presumably the reset() method of Lifeform) is defined as doing nothing: there is no code in that method.
Upvotes: 4