简体   繁体   中英

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

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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