简体   繁体   English

JPanel在paint()方法后面渲染

[英]JPanel rendered behind the paint() method

I am trying to create a board game. 我正在尝试创建一个棋盘游戏。 The left side of my windows is my actual board game while on the right side I was going to have a few buttons and timers. 我的窗户的左侧是我的实际棋盘游戏,而在右侧我将有几个按钮和计时器。 I paint my board using the paint method but when I try to create a new JPanel thats rendered in the background. 我使用paint方法绘制我的板,但是当我尝试创建一个在后台渲染的新JPanel时。 I know because I can see some black around the edges of the game. 我知道,因为我可以在游戏边缘看到一些黑色。

This is my main code that creates my JFrame and calls my board class. 这是我的主要代码,它创建我的JFrame并调用我的board类。

    frame.setSize(864, 480);
    frame.add(new Board());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setCursor(frame.getToolkit().createCustomCursor(new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), "null"));

    JPanel panel = new JPanel();
    frame.add(panel);
    panel.setBackground(Color.BLACK);

This is my code that paints the go board image. 这是我的代码,描绘了go board图像。 This is in my Board class 这是我的董事会课程

super.paint(g);
    s.location = MouseInfo.getPointerInfo();
    s.mouse = s.location.getLocation();
    s.updateBlackX(s.mouse);
    s.updateBlackY(s.mouse);
    s.updateWhiteX(s.mouse);
    s.updateWhiteY(s.mouse);
    Graphics2D g2d = (Graphics2D) g;
    g2d.drawImage(board, 0, 0, null);
    paintArray(g2d);
    if (turn == 1)
        g2d.drawImage(s.getBlackStone(), s.getBlackX() - f.getX() - 15, s.getBlackY() - f.getY() - 35, null);
    else
        g2d.drawImage(s.getWhiteStone(), s.getWhiteX() - f.getX() - 15, s.getWhiteY() - f.getY() - 35, null);

This is my game during run-time with further explanation as to my problem. 这是我在运行期间的游戏,并进一步解释了我的问题。 http://imgur.com/cAIS9aR http://imgur.com/cAIS9aR

Thank you in advance for any and all help! 提前感谢您的帮助!

I am trying to create a board game. 我正在尝试创建一个棋盘游戏。 The left side of my windows is my actual board game while on the right side I was going to have a few buttons and timers. 我的窗户的左侧是我的实际棋盘游戏,而在右侧我将有几个按钮和计时器。

Create two panels, one for the Board class and the other for the buttons then add them to the frame as follows: 创建两个面板,一个用于Board类,另一个用于按钮,然后将它们添加到框架中,如下所示:

frame.add(gamePanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);

That is don't try to do all you custom painting and game playing on a single panel. 那就是不要试图在一个面板上进行自定义绘画和游戏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM