2 Replies - 1088 Views - Last Post: 26 April 2012 - 11:12 PM Rate Topic: -----

#1 dj-wires  Icon User is offline

  • New D.I.C Head

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

Adding Graphics to JToggleButton/Jpanel- Sudoku Project

Posted 26 April 2012 - 01:51 PM

So i am trying to work through PBL's tutorial on Sudoku and there are parts that are a bit over my head. I have my grid built so far, and the part that i am trying to get right now, is adding individual elements to each button. I know i want to use a switch statement with the different drawing parts, but i can only get one image to post. I will eventually get into selecting a cell, but i want to get this part working first. I would love some help getting these graphics to post to different cells.




import java.awt.Graphics;

import java.awt.*;

import javax.swing.*;

import java.awt.image.*;

 

public class SudokuViewWorks extends JComponent {

 

  private JPanel centerBoard;

  private int cellSize = 48;

  private int numberRows;

  private int numberCols;

  private int boardSize;

  

  public SudokuViewWorks(SudokuBase sb){

    SudokuBase board = sb;

    numberRows = board.rows;

    numberCols = board.columns;

    boardSize = numberRows * numberCols;

    

    

    this.setPreferredSize(new Dimension(boardSize*cellSize + (boardSize*2 + 11), boardSize*cellSize +(boardSize*2 + 11)));

    this.setLayout(new BorderLayout());

    this.setBackground(Color.WHITE);

    this.setVisible(true);

    

  }

    

    public void paintComponent(Graphics g){

      

      // a panel for the sudoku board

      centerBoard = new JPanel(new GridLayout(boardSize, boardSize));

      

      for (int i =0; i < boardSize*boardSize; i++){

        

        //Cell button = new Cell(boardSize,boardSize);

        JToggleButton button = new JToggleButton(""+i);

        ImageIcon icon = new ImageIcon(drawSymbol(g,i));

        button.setIcon(icon);

        centerBoard.add(button);


        

        

      }

 

      //add Jpanel

      this.add(centerBoard);

 

      

    }

    

    

        

  public Image drawSymbol(Graphics g, int symbolNum){

       

     BufferedImage image = new BufferedImage(100,100,

          BufferedImage.TYPE_INT_ARGB);

         g = image.getGraphics();

         

                     

      

     // g.setColor(Color.BLACK);

      switch(symbolNum) {

      case 1:  g.setColor(Color.BLUE);

               g.drawRect(15, 15, 25, 25);

      

      case 2: g.drawOval(67, 67, 25, 25);

        

      case 3: g.drawLine(119, 119, 144, 144);

              g.drawLine(144, 119, 119, 144);

      

      case 4: g.setColor(Color.YELLOW);

           g.fillRect(10, 20, 80, 70);

           g.setColor(Color.RED);

                   

     

      case 5: g.fillOval(50, 25, 25, 25); 

      

      case 6: g.fillOval(50, 25, 25, 25);

      

      case 7: g.fillOval(50, 25, 25, 25);

      

      case 8: g.fillOval(50, 25, 25, 25);

      

      case 9: g.fillOval(50, 25, 25, 25);

      }

      

      return image;

   

    }

      

            

 

 

 

  

  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: Adding Graphics to JToggleButton/Jpanel- Sudoku Project

#2 dj-wires  Icon User is offline

  • New D.I.C Head

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

Re: Adding Graphics to JToggleButton/Jpanel- Sudoku Project

Posted 26 April 2012 - 04:36 PM

I have worked more on this..and have edited the above code to be this...but i am still not getting the buttons to display..i am now getting the images to work, but not the buttons.

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

public class SudokuViewWorks extends JPanel {
  

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

  }
    
    //public void paintComponent(Graphics g){
     
      // a panel for the sudoku board
      //centerBoard = new JPanel(new GridLayout(boardSize, boardSize));
      
     
        
        //Cell button = new Cell(boardSize,boardSize);
        //JButton button = new JButton();
        //ImageIcon icon = new ImageIcon(drawSymbol(g,i));
        //button.setIcon(icon);
        //button.setLayout
        //centerBoard.add(new MyPanel(i));
 
        
        
      }

      //add Jpanel
      //add(region);

      
    }
    
    
    class MyPanel extends JPanel {
      public int num;
      JLabel[][] label = new JLabel[3][3];
      
      public MyPanel(int numb) {
        super(new GridLayout(3,3,1,1));
        for(int i=0; i<3; i++) {
          for (int j = 0; j<3; j++) {
            label[i][j] = new JLabel("i");
            //label[i][j] = new Cell(boardSize,boardSize);
            add(label[i][j]);
            num = numb;
            setBorder(BorderFactory.createLineBorder(Color.black));
            setVisible(true);
           
          }
        }
         repaint();
      }
      
      public Dimension getPreferredSize() {
        return new Dimension(50,50);
      }
      
      public void paintComponent(Graphics g) {
        //super.paintComponent(g);
      
      switch(num) {
      case 1:  g.setColor(Color.BLUE);
               g.drawRect(15, 15, 25, 25);
      
      case 2: g.drawOval(67, 67, 25, 25);
              g.drawString("one", 10,20);
        
      case 3: g.drawLine(119, 119, 144, 144);
              g.drawString("2", 60,70);
              g.drawLine(144, 119, 119, 144);
      
      case 4: g.setColor(Color.YELLOW);
           //g.fillRect(10, 20, 80, 70);
           //g.setColor(Color.RED);
                   
     
      case 5:  g.setColor(Color.GREEN);
               g.fillOval(50, 25, 25, 25); 
      
      case 6: g.fillOval(50, 25, 25, 25);
      
      case 7: g.fillOval(50, 25, 25, 25);
      
      case 8: g.fillOval(50, 25, 25, 25);
      
      case 9: g.fillOval(50, 25, 25, 25);
      }
       //repaint();
        
      }
    }
    
    
    
    
    
  /**
   * 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
  • +
  • -

#3 dj-wires  Icon User is offline

  • New D.I.C Head

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

Re: Adding Graphics to JToggleButton/Jpanel- Sudoku Project

Posted 26 April 2012 - 11:12 PM

hooray!! i figured out that part...now to add them as different keys in the sudoku board and to set my sudoku size to whatever i want to set this as.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1