按下按钮时重新绘制Java

时间:2012-04-10 14:55:40

标签: java swing repaint graphics2d

我碰到了一堵墙(在我的大脑中)试图按下按钮来更新我的电路板。我是否正确地认为GameBoard类是需要重绘()编辑的那个?

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
}
}

Button.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();
}
}

这给出了一个空指针异常。那么任何想法如何重绘我的GameBoard?如果我因为愚蠢而改写一切,我不会生气! ;)

由于

2 个答案:

答案 0 :(得分:2)

你对如何使用Java进行绘制有错误的想法。像Panels这样的组件自己绘制,所有绘图都在UI线程上进行。

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

答案 1 :(得分:2)

文章Painting in AWT and Swing可能会对应用程序触发的绘画提供一些观点。下面的例子说明了原理。请注意,setForeground()会自动调用repaint(),因为前景颜色为bound property,但您可以随时自行调用。

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);
        }
    }
}