3

我似乎无法解决这里的问题。

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() {
    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 paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.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("Impact", 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) {
    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);
}

}

我运行它并最终得到一个灰屏。有什么帮助吗?

ImageEntity基本上是一个 Image Wrapper 类型的东西BufferedImage。问题似乎出在paintScore()方法上,好像我注释掉了方法的调用,它按预期工作。这是针对我正在尝试制作的 Pong 类型游戏。

4

2 回答 2

5

奇怪的是,这个单一的更改(经过多次更改以使其编译)修复了所述问题:

Font scoreFont = new Font("Arial", Font.PLAIN, 72);

乒乓

于 2012-08-31T16:11:53.460 回答
4

从 gamepanel() 中删除 paintscore 并将其添加到 mainpanel.add(gp) 之后

gp.paintScore(gp.gameScreenGraphics);

尽管将字体大小更改为 24,但您的代码还有其他问题

于 2012-08-31T15:11:49.893 回答