2012-04-10 102 views
0

我碰到了一面墙(在我的大脑中)试图更新我的按钮按下板。我是否认为GameBoard类是需要重绘()的类?按下按钮时重新绘制Java按钮

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?如果我因为愚蠢而必须重写所有内容,我不会生气! ;)

谢谢

+0

面板 – ControlAltDel 2012-04-10 15:00:13

回答

2

您对如何在Java中绘图有错误的想法。组件如面板绘制自己,所有绘图都在UI线程上进行。

看看这个教程:docs.oracle.com/javase/tutorial/2d/index.html

+0

没有paintComponent方法好,谢谢!我要通过教师工作。 – swit 2012-04-10 15:20:46

+0

+1时间花费;更多[这里](http://stackoverflow.com/a/10094267/230513)。 – trashgod 2012-04-10 18:23:08

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