10 Replies - 1070 Views - Last Post: 29 April 2012 - 01:48 AM Rate Topic: -----

#1 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Shading Every Other Grid

Posted 27 April 2012 - 02:27 PM

I am working through a Sudoku game i am trying to get setup. The problem i am having currently is i wish to color every other region. I have a inner class that creates my cells for the region, this is called in a outer for loop that creates each region. I can get them either all colored or none. I have tried an inner for loop, and a boolean flag, but i think it keeps setting it to true before i need it to...so it always colors them.

How do i get every other region to be shaded grey to indicate which region it is? i can attach my base and board if needed, but for this issue it is all in this class.

Here is my code so far.

import java.awt.Graphics;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;

public class SudokuViewWorks extends JPanel  {
  

  private JPanel centerBoard;
  private int cellSize = 48;
  private int Rows;
  private int Cols;
  private int boardSize;
  private MyPanel[][] region;
  private SudokuBase board;
  private Boolean on = false;
  
  public SudokuViewWorks(SudokuBase sb){
    super(new GridLayout(3,3,3,3));
    setVisible(true);
    
    board = sb;
    Rows = board.rows;
    Cols = board.columns;
    boardSize = Rows * Cols;
    setBorder(BorderFactory.createLineBorder(Color.black));
    region= new MyPanel[3][3];
    setPreferredSize(new Dimension(boardSize*cellSize + (boardSize*2 + 11), boardSize*cellSize +(boardSize*2 + 11)));
    for (int i =0; i < Rows; i++){
      for(int j =0; j<Cols; j++) {
        
        region[i][j] = new MyPanel();
        add(region[i][j]);
        if( on = true );  {
          colorPanel(region[i][j]);
              //on = false;
        }
        on = true;
     
      }
    }
  }
  //region[i][j].setBackGround(Color.GRAY);
          //repaint();
    
    
  public void colorPanel(MyPanel region) {
    MyPanel colored = region;
    for( int row =0; row < Rows; row++ ){
            for (int col = 0; col<Cols; col++) {
              colored.label[row][col].setBackground(java.awt.Color.GRAY);
            }
    }
    on = false;
  }
    

  
    
  
    
    
    class MyPanel extends JPanel {
      public int num;
      public JButton[][] label = new JButton[Rows][Cols];
      //Cell[][] label = new Cell[Rows][Cols];
      
      public MyPanel() {
       
        super(new GridLayout(3,3,1,1));
         //num = 1;
        for(int i=0; i<Rows; i++) {
          for (int j = 0; j<Cols; j++) {
            num = board.getValue(i,j);
            Icon center = new ImageIcon(drawSymbol(num));
            //num++;
            label[i][j] = new JButton(center);
            //label[i][j].setOpaque(true);
            
            //label[i][j] = new Cell(i,j);
            
            //label[i][j] = new Cell(boardSize,boardSize);
            add(label[i][j]);
            
            setBorder(BorderFactory.createLineBorder(Color.black));
            //setVisible(true);
           
          }
        }
         repaint();
      }
      
      
      
      
      /** here is the getPrefferredSize method
        */
      public Dimension getPreferredSize() {
        return new Dimension(50,50);
      }
      
      /**
       * this is my bufferedImage method, we will use this to draw the
       * different images onto the buttons
       * @param the number to choose
       */
      public BufferedImage drawSymbol( int symbolNum){
       
     BufferedImage image = new BufferedImage(100,100,
          BufferedImage.TYPE_INT_ARGB);
          Graphics g = image.getGraphics();
          String str = "HEY";
       
      g.setColor(Color.BLACK);
      switch(symbolNum) {
        case 0: g.drawString(str,25,50);
         break;
      case 1: g.drawRect(15, 15, 25, 50);
              break;
      
      case 2: g.drawOval(67, 67, 25, 25);
              g.drawArc(250,225,10,15,22,42);
              break;
      case 3: g.drawLine(119, 119, 144, 144);
              g.drawLine(144, 119, 119, 144);
              break;
      case 4: g.setColor(Color.YELLOW);
           g.fillRect(10, 30, 25, 70);
           g.setColor(Color.RED);
           break;
     
      case 5: g.fillRect(50, 50, 25, 25); 
      break;
      case 6: g.drawOval(30, 25, 25, 45);
      break;
      case 7: g.fillRect(50, 25, 25, 25);
      break;
      case 8: g.fillOval(50, 25, 25, 25);
      break;
      case 9: g.fillOval(50, 25, 25, 25);
      break;
      }
      repaint();
      return image;
   
    }
      
      
      
      
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
      
     
       //repaint();
        
      }
    }
    

/**
 * this is going to be my class for handeling Focus
 */
private class FocusHandler implements FocusListener {
  
  private Color background = getBackground();
  
  //@ovverride
  public void focusGained(FocusEvent e) {
    setBackground(background.brighter());
  }
  
  //@ovverride
  public void focusLost(FocusEvent e ) {
    setBackground(background);
  }
}



    
    
    
    
  /**
   * here is the method we are testing against
   */
  public static SudokuBase makeBoard() {
      SudokuBase board = new SudokuBoard(2, 3);
      board.setValue(0, 3, 6);
      board.setValue(0, 5, 1);
      board.setValue(1, 2, 4);
      board.setValue(1, 4, 5);
      board.setValue(1, 5, 3);
      board.setValue(2, 3, 3);
      board.setValue(3, 2, 6);
      board.setValue(4, 0, 2);
      board.setValue(4, 1, 3);
      board.setValue(4, 3, 1);
      board.setValue(5, 0, 6);
      board.setValue(5, 2, 1);
      board.fixGivens();
      board.setValue(1, 0, 6);
      board.setValue(3, 1, 5);
      return board;
   }
  
  /**public void setSelected(int row, int col){
  }
  public int getSelectedRow(){
  }
  public int getSelectedColumn(){
  }*/
  
  public static void main(String[] args) {
      JFrame win = new JFrame("Test board");
      win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
      SudokuViewWorks view = new SudokuViewWorks(makeBoard());
     
     //win.add(ggo);
      win.add(view);
      win.setVisible(true);
      win.pack();
   }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Shading Every Other Grid

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Shading Every Other Grid

Posted 27 April 2012 - 04:03 PM

This code has multiple errors
        if( on = true );  {
          colorPanel(region[i][j]);
        }


first the ; after the if. It finishes it.
this code is equivalent to
        if( on = true );  // <--- if finishes here  
        {
          colorPanel(region[i][j]);
        }

or if you prefer

        if( on = true );
        colorPanel(region[i][j]);


So you basically call colorPanel() in all cases :)

Now the if itself is crooked

if(on == true) { // with the == would make sense or more simply
if(on) {

if(on = true) is (with the assigment operator in is) equivalent to:

on = true;
if(on == true) {

or

on = true;
if(on) {



Also
colored.label[row][col].setBackground(java.awt.Color.GRAY);
for the background of a JLabel to appear, you have to setOpaque(true) on the JLabel

Happy coding
Was This Post Helpful? 1
  • +
  • -

#3 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Re: Shading Every Other Grid

Posted 27 April 2012 - 04:46 PM

So how do i change the flag back to off...i put the on = off; at the bottom of my constructor for my panel, that allows one region to be shaded but then none of the others....would i start it with on== true.... thanks for the help..i keep trying different variations..
Was This Post Helpful? 0
  • +
  • -

#4 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Re: Shading Every Other Grid

Posted 27 April 2012 - 06:27 PM

Thank you for pointing out the things i am missing...I figured out where i needed to put the if on turn off finally...took me a while to get that small part....now on to the next part.
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Shading Every Other Grid

Posted 27 April 2012 - 07:19 PM

View Postdj-wires, on 27 April 2012 - 09:27 PM, said:

....now on to the next part.

which is ?
Was This Post Helpful? 0
  • +
  • -

#6 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Re: Shading Every Other Grid

Posted 28 April 2012 - 11:33 AM

I need to be able to set up the setSelected method so that you can set the images i have drawn in the different circles and i need to setup some mouse listeners to be able to show when a cell is selected. Currently with the mouse listener i can attach it to a region but not to a single button. I thought that if i put the mouse listener on the JButtons that it would allow me to select each one differently. I think i might need to build another inner class with the mouse listeners for each button. That is what i am reading... Do you feel that is the way to go with this?

import java.awt.Graphics;
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;

public class SudokuViewWorks extends JPanel  {
  

  private JPanel centerBoard;
  private int cellSize = 48;
  private int Rows;
  private int Cols;
  private int boardSize;
  private MyPanel[][] region;
  private SudokuBase board;
  private Boolean on = true;
  
  public SudokuViewWorks(SudokuBase sb){
    super(new GridLayout(3,3,3,3));
    setVisible(true);
    
    board = sb;
    Rows = board.rows;
    Cols = board.columns;
    boardSize = Rows * Cols;
    setBorder(BorderFactory.createLineBorder(Color.black));
    region= new MyPanel[3][3];
    setPreferredSize(new Dimension(boardSize*cellSize + (boardSize*2 + 11), boardSize*cellSize +(boardSize*2 + 11)));
    for (int i =0; i < Rows; i++){
      for(int j =0; j<Cols; j++) {
        
        region[i][j] = new MyPanel();
        add(region[i][j]);
        
          colorPanel(region[i][j]);
              //on = false;
        
      }
    }
  }
  //region[i][j].setBackGround(Color.GRAY);
          //repaint();
    
    
  public void colorPanel(MyPanel region) {
    MyPanel colored = region;
    
    if( on = false) {
      on = true;}
    else if ( on = true);{
        for( int row =0; row < Rows; row++ ){
            for (int col = 0; col<Cols; col++) {
              colored.label[row][col].setBackground(java.awt.Color.GRAY);
            }
    }
  }
  }
    

  
    
  
    
    
    class MyPanel extends JPanel {
      public int num;
      public JButton[][] label = new JButton[Rows][Cols];
      //Cell[][] label = new Cell[Rows][Cols];
      
      public MyPanel() {
       
        super(new GridLayout(3,3,1,1));
         //num = 1;
        for(int i=0; i<Rows; i++) {
          for (int j = 0; j<Cols; j++) {
            num = board.getValue(i,j);
            Icon center = new ImageIcon(drawSymbol(num));
            //num++;
            label[i][j] = new JButton(center);
            //label[i][j].setOpaque(true);
            
            //label[i][j] = new Cell(i,j);
            
            //label[i][j] = new Cell(boardSize,boardSize);
            add(label[i][j]);
            setOpaque(true);
            setBorder(BorderFactory.createLineBorder(Color.black));
            //setVisible(true);
           
          }
        }
         repaint();
      }
      
      
      
      
      /** here is the getPrefferredSize method
        */
      public Dimension getPreferredSize() {
        return new Dimension(50,50);
      }
      
      /**
       * this is my bufferedImage method, we will use this to draw the
       * different images onto the buttons
       * @param the number to choose
       */
      public BufferedImage drawSymbol( int symbolNum){
       
     BufferedImage image = new BufferedImage(100,100,
          BufferedImage.TYPE_INT_ARGB);
          Graphics g = image.getGraphics();
          String str = "HEY";
       
      g.setColor(Color.BLACK);
      switch(symbolNum) {
        case 0: g.drawString(str,25,50);
         break;
      case 1: g.drawRect(15, 15, 25, 50);
              break;
      
      case 2: g.drawOval(67, 67, 25, 25);
              g.drawArc(250,225,10,15,22,42);
              break;
      case 3: g.drawLine(119, 119, 144, 144);
              g.drawLine(144, 119, 119, 144);
              break;
      case 4: g.setColor(Color.YELLOW);
           g.fillRect(10, 30, 25, 70);
           g.setColor(Color.RED);
           break;
     
      case 5: g.fillRect(50, 50, 25, 25); 
      break;
      case 6: g.drawOval(30, 25, 25, 45);
      break;
      case 7: g.fillRect(50, 25, 25, 25);
      break;
      case 8: g.fillOval(50, 25, 25, 25);
      break;
      case 9: g.fillOval(50, 25, 25, 25);
      break;
      }
      repaint();
      return image;
   
    }
      
      
      
      
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
      
     
       //repaint();
        
      }
    }
    

/**
 * this is going to be my class for handeling Focus
 */
private class FocusHandler implements FocusListener {
  
  private Color background = getBackground();
  
  //@ovverride
  public void focusGained(FocusEvent e) {
    setBackground(background.brighter());
  }
  
  //@ovverride
  public void focusLost(FocusEvent e ) {
    setBackground(background);
  }
}



    
    
    
    
  /**
   * here is the method we are testing against
   */
  public static SudokuBase makeBoard() {
      SudokuBase board = new SudokuBoard(2, 3);
      board.setValue(0, 3, 6);
      board.setValue(0, 5, 1);
      board.setValue(1, 2, 4);
      board.setValue(1, 4, 5);
      board.setValue(1, 5, 3);
      board.setValue(2, 3, 3);
      board.setValue(3, 2, 6);
      board.setValue(4, 0, 2);
      board.setValue(4, 1, 3);
      board.setValue(4, 3, 1);
      board.setValue(5, 0, 6);
      board.setValue(5, 2, 1);
      board.fixGivens();
      board.setValue(1, 0, 6);
      board.setValue(3, 1, 5);
      return board;
   }
  
  /**public void setSelected(int row, int col){
  }
  public int getSelectedRow(){
  }
  public int getSelectedColumn(){
  }*/
  
  public static void main(String[] args) {
      JFrame win = new JFrame("Test board");
      win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
      SudokuViewWorks view = new SudokuViewWorks(makeBoard());
     
     //win.add(ggo);
      win.add(view);
      win.setVisible(true);
      win.pack();
   }

}


Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Shading Every Other Grid

Posted 28 April 2012 - 11:45 AM

http://www.dreaminco...snippet1891.htm
Was This Post Helpful? 1
  • +
  • -

#8 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Re: Shading Every Other Grid

Posted 28 April 2012 - 01:05 PM

Ok i have built a seperate button class and i can get the mouse listeners to work on the button. i create a new Button class for every button and it works, but the click part is small...if i click the whole button it does not change it... there is just a small part that allows for the clicking, or entering and exiting of the mouse. I tried to set size as the size of the button.

I figured when i would add the mouse listener to the JButton as a seperate class, and then used that to create each of the grid squares that it would fill the entire space. Even setting size doesnt change the area that is needed to be clicked. i will attach my button class as well...

my Button class
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Button extends JButton implements MouseListener {

	// count the number of time the mouse entered the JButton
	// and the number of times a click was done over it
	int nbOver = 0, nbClick = 0;
	JButton label;
	private boolean on = true;
	
	Button() {
		super();
		// create the JButton
	    label = new JButton();
	    label.setSize(50, 50);
	    // add the mouseListener
	    label.addMouseListener(this);
	    // put the JButton in the frame
	    add(label);
	}

	// the following 5 methods must be defined if you
	// implements MouseListener
	public void mouseClicked(MouseEvent arg0) {
	}
	// mouse entered the JLabel increment count and display it
	public void mouseEntered(MouseEvent arg0) {
		//nbOver++;
		//label.setText("Over: " + nbOver +  " WORD UP!!");
	}
	public void mouseExited(MouseEvent arg0) {
	}
	// mouse was presssed (cliked and released)
	// increment counter and display it
	public void mousePressed(MouseEvent arg0) {
		if(on == true){
		label.setBackground(getBackground().darker());
		
	} else {
		label.setBackground(getBackground().brighter());
		on = true; }
	on = false;
	}
	public void mouseReleased(MouseEvent arg0) {
	}
	
	}




and my updated grid

import java.awt.*;
import javax.swing.*;

import java.awt.image.*;
import java.awt.event.*;

public class SudokuViewWorks extends JPanel  {
  

  private JPanel centerBoard;
  private int cellSize = 48;
  private int Rows;
  private int Cols;
  private int boardSize;
  private MyPanel[][] region;
  private SudokuBase board;
  private Boolean on = true;
  
  public SudokuViewWorks(SudokuBase sb){
    super(new GridLayout(3,3,3,3));
    setVisible(true);
    
    board = sb;
    Rows = board.rows;
    Cols = board.columns;
    boardSize = Rows * Cols;
    setBorder(BorderFactory.createLineBorder(Color.black));
    region= new MyPanel[3][3];
    setPreferredSize(new Dimension(boardSize*cellSize + (boardSize*2 + 11), boardSize*cellSize +(boardSize*2 + 11)));
    for (int i =0; i < Rows; i++){
      for(int j =0; j<Cols; j++) {
        
        region[i][j] = new MyPanel();
        add(region[i][j]);
        //on = false;
      
    }
      //on = true;
  }
    
  }
  //region[i][j].setBackGround(Color.GRAY);
          //repaint();
    

    
    /**
     * here is the Inner class that will construct a single panel
     * THis is a JPanel- it will add JButtons according to the Rows and Cols that are set
     * will also shade the buttons
     */
    class MyPanel extends JPanel {
    	
    	// Here are the fields for this class
    	// num is going to be the number to be called for drawing on the JPanel
    	// Label is going to be the 2d array to hold all of the JButtons
      private int num;
      private Button[][] label = new Button[Rows][Cols];
      //Cell[][] label = new Cell[Rows][Cols];
      
      
      /*
       * This is my constructor. This will start with a call to the super class
       * which is a jpanel to enact all of its fields and methods, also setting
       * the Jpanel to a new grid layout. which will accomodate the rows and columns
       */
      public MyPanel() {
    	  
       // Starts the JPanel with a new Grid Layout
        super(new GridLayout(3,3,1,1));
       
         //num = 1;
        for(int i=0; i<Rows; i++) {
          for (int j = 0; j<Cols; j++) {
            num = board.getValue(i,j);
            
            Icon center = new ImageIcon(drawSymbol(4));
            //num++;
            label[i][j] = new Button();
            label[i][j].setSize(cellSize, cellSize);
            label[i][j].setIcon(center);
            
            //This is my action listener portion of the method...
            // currently this attaches a click to a value to an entire region
            // i should be able to put this in a method and assign each button
            // seperatly
            
             // here we are going to check if flag is True
            // if Flag is true then we will color the label
            // due to the location of this, it will stay on
            // until the loop is exited. This will allow for every button 
            // in a region to be colored Gray.
            if(on){
            colorPanel(label[i][j]);
            }
            	
            //label[i][j].setOpaque(true);
            //label[i][j] = new Cell(i,j);
            
            //label[i][j] = new Cell(boardSize,boardSize);
            add(label[i][j]);
            label[i][j].setOpaque(true);
            setBorder(BorderFactory.createLineBorder(Color.black));
            //setVisible(true);
           
          }
        }
        // here we should use the IsGivens method to check...if so we can change it to
        // the color we want as well.
        if (on==true) {
        on = false;
        } else {
        	on = true;
        }
        
         repaint();
      }
      
      /**
       * here is the method that will color the panels grey based
       * it will be called if the on == true. This will mean that 
       * every other panel will get colored. 
       * @param region
       */
      public void colorPanel(Button colored) {
    	    //JButton colored = region;
    	
    	        colored.setOpaque(true);
    	              colored.setBackground(java.awt.Color.GRAY);
    	            }
    	    
      
      
      /** here is the getPrefferredSize method
        */
      public Dimension getPreferredSize() {
        return new Dimension(50,50);
      }
      
      /**
       * this is my bufferedImage method, we will use this to draw the
       * different images onto the buttons
       * @param the number to choose
       */
      public BufferedImage drawSymbol( int symbolNum){
       
     BufferedImage image = new BufferedImage(100,100,
          BufferedImage.TYPE_INT_ARGB);
          Graphics g = image.getGraphics();
          String str = "ZERO";
       
      g.setColor(Color.BLACK);
      switch(symbolNum) {
        case 0: g.drawString(str,25,50);
         break;
      case 1: g.drawLine(50,30, 50, 100);
              g.drawLine(51, 31, 51, 101);
              g.drawLine(100,50, 0, 50);
              g.drawLine(101, 51, 1, 51);
              break;
      
      case 2: g.fillOval(35, 35, 25, 25);
              g.setColor(Color.GRAY);
              g.fillOval(37,37,20,20);
              break;
      case 3: g.setColor(Color.GREEN); 
    	      g.drawLine(100, 30, 0, 70);
              g.drawLine(0, 30, 100, 70);
              break;
              
      case 4: g.setColor(Color.YELLOW);
            g.fillRect(30, 40, 45, 30);
            g.setColor(Color.GRAY);
            g.fillRect(35, 45, 35, 25);
            g.setColor(Color.RED);
           break;
     
      case 5: g.fillRect(50, 50, 25, 25); 
      break;
      
      case 6: g.drawOval(30, 25, 25, 45);
      break;
      
      case 7: g.fillRect(50, 25, 25, 25);
      break;
      
      case 8: g.fillOval(50, 25, 25, 25);
      break;
      
      case 9: g.fillOval(50, 25, 25, 25);
      break;
      }
      repaint();
      return image;
   
    }
      
      
      
      
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
      
     
       //repaint();
        
      }
    }
    

/**
 * this is going to be my class for handeling Focus
 */
private class FocusHandler implements FocusListener {
  
  private Color background = getBackground();
  
  //@ovverride
  public void focusGained(FocusEvent e) {
    setBackground(background.brighter());
  }
  
  //@ovverride
  public void focusLost(FocusEvent e ) {
    setBackground(background);
  }
}



    
    
    
    
  /*
   * here is the method we are testing against
   */
  public static SudokuBase makeBoard() {
      SudokuBase board = new SudokuBoard(2, 3);
      board.setValue(0, 3, 6);
      board.setValue(0, 5, 1);
      board.setValue(1, 2, 4);
      board.setValue(1, 4, 5);
      board.setValue(1, 5, 3);
      board.setValue(2, 3, 3);
      board.setValue(3, 2, 6);
      board.setValue(4, 0, 2);
      board.setValue(4, 1, 3);
      board.setValue(4, 3, 1);
      board.setValue(5, 0, 6);
      board.setValue(5, 2, 1);
      board.fixGivens();
      board.setValue(1, 0, 6);
      board.setValue(3, 1, 5);
      return board;
   }
  
  /**public void setSelected(int row, int col){
  }
  public int getSelectedRow(){
  }
  public int getSelectedColumn(){
  }*/
  
  public static void main(String[] args) {
      JFrame win = new JFrame("Test board");
      win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
      SudokuViewWorks view = new SudokuViewWorks(makeBoard());
     
     //win.add(ggo);
      win.add(view);
      win.setVisible(true);
      win.pack();
   }

}


Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8380
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Shading Every Other Grid

Posted 28 April 2012 - 01:11 PM

Why would you use a MouseListener over a JButton ? Does not make sense
MouseListener are for JComponent that do not react to a click which a JButton does

After all a JButton is just a JLabel with a MouseListener over it implemented by the GUI so you don't have to do it yourself.
Was This Post Helpful? 1
  • +
  • -

#10 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Re: Shading Every Other Grid

Posted 28 April 2012 - 01:51 PM

Alright thank you, you are correct, i am still getting used to the JLabels and Buttons so now i have set it to where if the JLabel is clicked then it will either show as selected or not by getting brighter or darker. I see that in your tutorial you used Focus for that, but is what i did just as good for now...or when i get into selecting and being able to enter a key number with Key listener later mess that up.

Also i am trying to figure out how to to get only one box selected at a time. So if one box is selecte and shaded then unshade that box and shade the new one. I thought i could have a boolean flag that controlled it but then each class would have to return a boolean flag back...so with that would i just create a getShade method in my button?

Here is my updated button code
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class Button extends JLabel implements MouseListener {

	// count the number of time the mouse entered the JButton
	// and the number of times a click was done over it
	int nbOver = 0, nbClick = 0;
	//JLabel label;
	private boolean on = true;
	private boolean select;
	
	Button(boolean off) {
		super();
		off = select;
	    // add the mouseListener
		setSize(getHeight(),getWidth());
	    addMouseListener(this);
	    
	}

	// the following 5 methods must be defined if you
	// implements MouseListener
	public void mouseClicked(MouseEvent arg0) {
	}
	// mouse entered the JLabel increment count and display it
	public void mouseEntered(MouseEvent arg0) {
		nbOver++;
		setText("Over: " + nbOver +  " WORD UP!!");
	}
	public void mouseExited(MouseEvent arg0) {
	}
	// mouse was pressed (clicked and released)
	// increment counter and display it
	public void mousePressed(MouseEvent arg0) {
		if(on==true){
		setBackground(getBackground().darker());
		on = false;
		
	} else {
		setBackground(getBackground().brighter());
		on = true; 
		
		 }
	
	}
	public void mouseReleased(MouseEvent arg0) {
	}
	
	}



Was This Post Helpful? 0
  • +
  • -

#11 dj-wires   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 12-October 11

Re: Shading Every Other Grid

Posted 29 April 2012 - 01:48 AM

So i am having a problem with the focus listener. I tried setting it up the way you had it in your demo but it did not work. Would it be because i am not using other parts. If i implement focus listener and then put in the requestFocus as you have that should push a focus event. Then my focus should work correct. Would you like to see my code?

edit- figured it out!!! Thanks on to the next part!!

This post has been edited by dj-wires: 29 April 2012 - 01:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1