我正在制作一款名为Mastermind的游戏。为了创建游戏板,我使用paintComponent绘制到作为来自不同类的参数传递的帧。为什么除了后退按钮之外什么都不打印?
public class mastermindColor extends JPanel //running color game
{
Rectangle rect = new Rectangle(100, 100, 150, 75);
private JButton backButton = new JButton();
JFrame f = new JFrame();
public mastermindColor(final JFrame frame) //creating color game frame
{
String back = BorderLayout.CENTER;
backButton.setText("back");
frame.add(backButton, BorderLayout.NORTH);
frame.add(this, BorderLayout.CENTER);
backButton.setVisible(true);
backButton.addActionListener(new ActionListener() //if back button is selected
{
public void actionPerformed(ActionEvent e)
{
frame.getContentPane().removeAll();
mastermindRunner run = new mastermindRunner();
}
});
frame.setVisible(true);
}
public void paintComponent(Graphics g) //printing the game set up
{
super.paintComponents(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(rect);
}