0

I am working on a GUI for Sudoku. I have a good app for my Droid (SudokuFree) and I would like to copy its functionality and add one small feature (save a board in progress, continue, and revert back if desired). Some code has been commented out to make it more SSCCE, refer to the second post if you have questions. Two posts have helped me very much and I borrowed much the code below from them.

Action listener for JButton array

Building a GUI for a Sudoku Solver (Complete with ASCII Example)

My problem is that JButtons are only visible after they are scolled over. I searched previous posts and only found references to setVisible(true), which I have already done.

Thank you

public class RunSudokuNinja implements Runnable{
    @Override
    public void run() {
        //SudokuEngineInterface sudokuEngine = new SudokuEngine();
        SudokuView sudokuView = new SudokuView();
        //sudokuView.setSudokuImplementation(sudokuEngine);
        sudokuView.setVisible(true);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new RunSudokuNinja());
    }
}

public class SudokuView extends JFrame{
    //SudokuController controller;
    //SudokuEngine sudokuEngine;

    private static final int width = 500;
    private static final int height = 650;
    private static final int yMar = 100;
    private static final int xMar = 25;
    private static final int cellSize = 40;
    private static final int gridSpace = 3;
    private static final int secSpace = 9;

    public JButton[][] btnz = new JButton[9][9];

    public SudokuView() {
        //controller = new SudokuController();
        //sudokuEngine = new SudokuEngine();

        setTitle("Sudoku Ninja 0.1");
        setSize(width, height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setBackground(Color.GRAY);

        int y=yMar;
        for (int r=0;r<9;r++){
            int x=xMar;
            for (int c=0;c<9;c++){
                btnz[r][c]=new JButton("1");
                btnz[r][c].setBounds(x,y,cellSize,cellSize);
                btnz[r][c].setVisible(true);
                add(btnz[r][c]);
                if (c==2||c==5){
                    x = x+cellSize+secSpace;
                }else{
                    x = x+cellSize+gridSpace;
                }
            }
            if (r==2||r==5){
                y = y+cellSize+secSpace;
            }else{
                y = y+cellSize+gridSpace;
            }
        }
        setVisible(true);
    }

    @Override
    public void paint(Graphics g){

    }


    public void setSudokuImplementation(SudokuEngineInterface listener) {
        //controller.setListener(listener);
    }
}
Community
  • 1
  • 1
user2133814
  • 2,431
  • 1
  • 24
  • 34

2 Answers2

3

You're overriding the JFrame's paint method and short circuiting the paint mechanism. Given that there is no functionality in this method, you can remove these lines:

@Override
public void paint(Graphics g){

}

Any intended custom painting is should ideally be done by overriding paintComponent in a sub-class of JComponent rather than in a top level container such as JFrame. This takes advantage of Swing's double buffering paint mechanism.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
3

You are overriding the paint method telling it to do nothing, correct your paint() to this:

public void paint(Graphics g){
    super.paint(g);
    //do whatever you need to do here
}
BackSlash
  • 21,927
  • 22
  • 96
  • 136