gpt4 book ai didi

java - JFrame.revalidate() 并产生灰屏 - 困惑

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:00 25 4
gpt4 key购买 nike

我在 JPanel 中有一个方法

private void paintScore(Graphics2D g) {
Font scoreFont = new Font("Arial", Font.PLAIN, 72);
g.setFont(scoreFont);
FontMetrics scoreFontMetrics = g.getFontMetrics();
g.drawString("" + playerOneScore, SCREEN_WIDTH / 2 - 30
- scoreFontMetrics.stringWidth("" + playerOneScore),
SCREEN_HEIGHT / 2);
g.drawString("" + playerTwoScore, SCREEN_WIDTH / 2 + 30,
SCREEN_HEIGHT / 2);

}

当我调用这个方法时,它第一次看起来有效。然后,此后它就再也不起作用了。

如果我注释掉该方法的调用,它始终有效。

但是,如果我调用 JFrame 的 revalidate() 方法,该程序也会始终正常工作。

我不确定这里发生了什么。

如果需要,我可以发布完整的源代码。

提前致谢

<小时/>

这是其余的代码。

主要 JPanel 扩展类。

       import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable, MouseMotionListener {

private static final int SCREEN_WIDTH = 640;
private static final int SCREEN_HEIGHT = 480;
private static final int INDENT = 20;

private int playerOneScore = 0;
private int playerTwoScore = 0;
private ImageEntity playerOne = new ImageEntity("Images/bouncer.bmp");
private ImageEntity playerTwo = new ImageEntity("Images/bouncer.bmp");

private int mouseX = 0;
private int mouseY = 0;

private BufferedImage gameScreen = new BufferedImage(SCREEN_WIDTH,
SCREEN_HEIGHT, BufferedImage.TYPE_INT_RGB);

Graphics2D gameScreenGraphics = gameScreen.createGraphics();

public GamePanel() throws Exception {
paintBackground(gameScreenGraphics);
paintScore(gameScreenGraphics);
paintBouncers(gameScreenGraphics);

}

public void run() {
}

public void mouseMoved(MouseEvent m) {
mouseX = m.getXOnScreen();
mouseY = m.getYOnScreen();
}

public void mouseDragged(MouseEvent m) {
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(gameScreen, 0, 0, this);
}

private void paintBackground(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(Color.WHITE);
for (int i = 0; i < 10; i++) {
g.fillRect(SCREEN_WIDTH / 2 - 5, i * SCREEN_HEIGHT / 10, 10,
(SCREEN_HEIGHT / 10) - 10);
}
}

private void paintScore(Graphics2D g) {
Font scoreFont = new Font("Arial", Font.PLAIN, 72);
g.setFont(scoreFont);
FontMetrics scoreFontMetrics = g.getFontMetrics();
g.drawString("" + playerOneScore, SCREEN_WIDTH / 2 - 30
- scoreFontMetrics.stringWidth("" + playerOneScore),
SCREEN_HEIGHT / 2);
g.drawString("" + playerTwoScore, SCREEN_WIDTH / 2 + 30,
SCREEN_HEIGHT / 2);

}

private void paintBouncers(Graphics2D g) {
g.drawImage(playerOne.getImage(), playerOne.getX(), playerOne.getY(),
this);
g.drawImage(playerTwo.getImage(), playerTwo.getX(), playerTwo.getY(),
this);
}

public static void main(String[] args)throws Exception {
JFrame mainPane = new JFrame("Pong - Mrinank Sharma");
mainPane.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
mainPane.setVisible(true);
mainPane.setResizable(false);
GamePanel gp = new GamePanel();
mainPane.add(gp);
mainPane.revalidate();
}

}

ImageEntity 是一个类,它只是 BufferedImage 的包装器。

再次感谢大家

最佳答案

尝试这样做:

public GamePanel() {
paintBackground(gameScreenGraphics);
paintScore(gameScreenGraphics);
paintBouncers(gameScreenGraphics);
addMouseMotionListener(this);
repaint();
}

您忘记添加鼠标运动监听器并重绘面板。重新验证();在这种情况下没有用。

关于java - JFrame.revalidate() 并产生灰屏 - 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12223370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com