2 Replies - 227 Views - Last Post: 26 April 2012 - 07:19 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 JToggleButtons- Building Sudoku Values

Posted 26 April 2012 - 01:10 PM

Ok i am trying to build a Sudoku Game, i have gone through PBL's tutorial and there are certain parts that are over my head. I currently can get my grid to add with our without using his cell implimentation, but i want to be able to add individual graphics to each button to represent the numbers. I have read that i can create an inner class and override paintcomponent but i do not know if that is how i should do it, with having already used paintComponent in my base class. I simply need help adding the images to the buttons on this part.


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 JToggleButtons- Building Sudoku Values

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: adding Graphics to JToggleButtons- Building Sudoku Values

Posted 26 April 2012 - 06:14 PM

*Moved from tutorial section* Please keep questions in the programming help sections of the site.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: adding Graphics to JToggleButtons- Building Sudoku Values

Posted 26 April 2012 - 07:19 PM

Duplicated post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1