0

我碰壁(在我的大脑中)试图在按钮按下时更新我的​​板。我是否认为 GameBoard 类是需要重新绘制()的类?

游戏板.java

public class GameBoard extends Panel {

static Compass compass = new Compass();
private static final long serialVersionUID = 1;
Graphics2D g2d;

static final Dimension WINDOW_SIZE = new Dimension(1150, 800);

public void boardMaker() throws Exception {
    JFrame frame = new JFrame("Display image");

    JPanel panel = new JPanel();
    /* unimportant stuff

    .....


    */

    //

    DieRoll roll = new DieRoll("Roll Dies");
    roll.setC(compass);
    roll.setG2D(g2d);
    //
    Button button = new Button("new");
    button.setGameBoard(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(button);
    buttonPanel.add(roll);
    buttonPanel.setPreferredSize(new Dimension(200,100));

    frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
    //
    frame.getContentPane().add(panel);
    frame.setVisible(true);
}

public void paint(Graphics g) {
// not important I think
}
}

按钮.java

public class Button extends JButton implements ActionListener {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JFrame frame = new JFrame();
Compass c = new Compass();

GameBoard gb = new GameBoard();


Button(String text) {
    this.setText(text);
    this.addActionListener(this);
}

void setGameBoard(GameBoard gb) {
    this.gb = gb;
}

@Override
public void actionPerformed(ActionEvent e) {
    gb.g2d.setColor(Color.black);
    gb.g2d.fillRect(100, 100, 100, 200);
    gb.repaint();
}
}

这给出了一个空指针异常。那么知道如何重新粉刷我的游戏板吗?如果我因为愚蠢而不得不重写所有内容,我不会生气!;)

谢谢

4

2 回答 2

2

你对如何用 Java 绘图有错误的想法。像面板这样的组件会自己绘制,所有的绘制都发生在 UI 线程上。

查看本教程:docs.oracle.com/javase/tutorial/2d/index.html

于 2012-04-10T14:59:07.077 回答
2

文章在 AWT 和 Swing 中绘制可能会提供一些关于应用程序触发绘制的观点。下面的例子说明了这个原理。请注意,自动setForeground()调用repaint()是因为前景色是一个绑定属性,但您始终可以自己调用它。

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class SwingPaint {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                final GamePanel gp = new GamePanel();
                f.add(gp);
                f.add(new JButton(new AbstractAction("Update") {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        gp.update();
                    }
                }), BorderLayout.SOUTH);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    private static class GamePanel extends JPanel {

        private static final Random r = new Random();

        public GamePanel() {
            this.setForeground(new Color(r.nextInt()));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }

        public void update() {
            this.setForeground(new Color(r.nextInt()));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Dimension size = this.getSize();
            int d = Math.min(size.width, size.height) - 10;
            int x = (size.width - d) / 2;
            int y = (size.height - d) / 2;
            g.fillOval(x, y, d, d);
            g.setColor(Color.blue);
            g.drawOval(x, y, d, d);
        }
    }
}
于 2012-04-10T18:21:51.053 回答