开发者

Why is nothing painted when I call repaint()?

开发者 https://www.devze.com 2023-02-09 01:02 出处:网络
First of all, I am a beginner. I\'m trying to make a puzzle game using an array of Pieces. Each Piece represents a number from 1 to 9. I am trying to paint using paintComponent(Graphics g), but when I

First of all, I am a beginner. I'm trying to make a puzzle game using an array of Pieces. Each Piece represents a number from 1 to 9. I am trying to paint using paintComponent(Graphics g), but when I call the repaint() method, nothing happens. There is no error, so there must be some point I am not aware of.

I'm using NetBeans. I created a new desktop application and then added a JPanel and a JButton.

This is my code:

public class PuzzleGame2View extends FrameView {

public Piece pieces[][];

Drawing outer = new Drawing();

public PuzzleGame2View(SingleFrameApplication app) {

super(app);

initComponents();
//more code that netbeans automatically wrote......

public class Drawing extends JFrame implements MouseListener{
    public void paintComponent(Graphics g ){
        g = jPanel1.getGraphics();
        super.paintComponents(g);

        for (int i = 0; i < pieces.length; i++) {
            for (int j = 0; j < pieces.length; j++) {
                if (pieces[i][j].getText()!=null) {
                    g.setColor(Color.red);
                     g.fillRect(i*100, j*100, 100, 100);
                     g.setColor(Color.BLACK);
         开发者_如何学C            g.drawString(pieces[i][j].getText(), i*100 + 50, j*100 + 20);

                }
            }
        }
    }

    public void makePieces(){
    pieces = new Piece[3][3];
    for (int i = 0; i < pieces.length; i++) {
        for (int j = 0; j < pieces.length; j++) {
            if (i == 2 && j == 2){
                pieces[i][j] = new Piece(j, j, null);
            }
             else

            pieces[i][j] = new Piece(j, j, "" + (i*3+j+1) );
        }
    }
}

I am trying to call the repaint() method when I click the button.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
makePieces();
outer.repaint();
}                  

Here's the class Piece:

package puzzlegame2;

public class Piece {
private int row,count;
private String text;

public Piece(int row, int count, String text) {
    this.row = row;
    this.count = count;
    this.text = text;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}
}

It's just the first step; there are many things to do. But I can't go on until I totally understand how public void paintComponent(Graphics g) and repaint() work.

So, please, any help would be appreciated.


Try overriding the paint() method in stead of paintComponents(). repaint() sends a call to paint().

EDIT: One thing you should change in any case is the fact that you are overriding the paintComponent() method of your JFrame. You should in stead override this method in your JPanel and then set your new panel as content panel of the JFrame. And then call repaint() on the panel.


try revalidate() call before repaint

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号