Hey everyone. I'm trying to create a MouseListener event so that when a Tile is clicked on my grid it will black out all tiles that are of the same color as the one I clicked, except the one I clicked.
A little background on what my code does so far: When I run the program it creates a GameBoard of JButton's and then colors them in using my colorTiles() method. After that it's supposed to shuffle them up and distribute them randomly across the board but I haven't gotten that far yet.
here is my code so far
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Arrays; import java.util.Collections; import java.util.Random; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; @SuppressWarnings("serial") public class GameBoard extends JFrame{ private int numRows = 16, numCols = 16; Tile[][] myTiles; Tile[] t = new Tile[256]; JButton board; private MyEventHandler eh = new MyEventHandler(); public static void main(String[] args) { new GameBoard(); } public GameBoard() { setSize(800,800); Container c = getContentPane(); c.setLayout(new BorderLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); setTitle("Random Colors"); JButton tilePanel = buildTiles(); c.add(tilePanel, BorderLayout.CENTER); setResizable(false); setVisible(true); } private JButton buildTiles() { board = new JButton(); board.setBackground(Color.BLACK); board.setPreferredSize(new Dimension(800, 800)); board.setLayout(new GridLayout(numRows, numCols)); colorTiles(); return board; } public void colorTiles() { for(int i = 0; i < t.length; i++){ if(i < 39){ t[i] = new Tile(Color.YELLOW); t[i].setColor(); board.add(t[i]); } else if(i >= 39 && i < 85){ t[i] = new Tile(Color.RED); t[i].setColor(); board.add(t[i]); } else if(i >= 57 && i < 119){ t[i] = new Tile(Color.BLUE); t[i].setColor(); board.add(t[i]); } else if(i >= 119 && i < 171){ t[i] = new Tile(Color.GREEN); t[i].setColor(); board.add(t[i]); } else if(i >=171 && i < 212){ t[i] = new Tile(Color.MAGENTA); t[i].setColor(); board.add(t[i]); } else if(i >= 212 && i < 256){ t[i] = new Tile(Color.CYAN); t[i].setColor(); board.add(t[i]); } } } public void shuffle() { Collections.shuffle(Arrays.asList(t)); } private class MyEventHandler implements MouseListener { @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } } }
import java.awt.Color; import java.util.Arrays; import java.util.Collections; import java.util.Random; import javax.swing.JButton; @SuppressWarnings("serial") public class Tile extends JButton{ Color color; Tile[] colors = new Tile[256]; Tile[][] myTiles; public Tile(Color c) { color = c; } public Tile() { } public void setColor() { for(int i = 0; i < colors.length; i++){ setBackground(color); paintImmediately(0, 0, getWidth(), getHeight()); } } }
I guess my question is, how would I go about doing this and where do I start? do I need to make a new method or can I write some code in the mouseClicked method and how do I give my tiles the ability to detect a mouse click?