dj-wires's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
28 (0.05 per day)
Joined:
12-October 11
Profile Views:
80
Last Active:
User is offline May 07 2012 01:20 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   dj-wires has not set their status

Posts I've Made

  1. In Topic: Shading Every Other Grid

    Posted 29 Apr 2012

    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!!
  2. In Topic: Shading Every Other Grid

    Posted 28 Apr 2012

    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) {
    	}
    	
    	}
    
    
    
  3. In Topic: Shading Every Other Grid

    Posted 28 Apr 2012

    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();
       }
    
    }
    
    
  4. In Topic: Shading Every Other Grid

    Posted 28 Apr 2012

    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();
       }
    
    }
    
    
  5. In Topic: Shading Every Other Grid

    Posted 27 Apr 2012

    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.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

dj-wires hasn't added any friends yet.

Comments

dj-wires has no profile comments yet. Why not say hello?