Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GridLayout/JButton question

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GridLayout/JButton/MouseListener question

    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?
    Last edited by captain; February 23rd, 2012 at 10:42 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: GridLayout/JButton question

    how do I give my tiles the ability to detect a mouse click
    You can add an action listener to the Tile class objects because it extends the JButton class.

Similar Threads

  1. [SOLVED] JPanel GridLayout isn't working
    By mzamboen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 14th, 2012, 01:10 PM
  2. GridLayout help
    By sambar89 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2011, 10:00 PM
  3. GridLayout and size problems...
    By sambar89 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 6th, 2011, 12:06 PM
  4. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM
  5. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 06:39 PM