Code is producing a gray screen

纵饮孤独 提交于 2019-12-14 03:50:05

问题


I can't seem to work out what the problem is here.

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);
}

}

I run this and end up getting a grey screen. Any help?

ImageEntity is basically an Image Wrapper type thing for BufferedImage. The problem seems to be in the paintScore() method, as if I comment off the calling of the method, it works as intended. This is for a Pong type game I am trying to make.


回答1:


Oddly, this single change (after a number of changes to get it to compile) fixes the stated problem:

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




回答2:


remove paintscore from gamepanel() and add it in main after mainpanel.add(gp)

gp.paintScore(gp.gameScreenGraphics);

there is something else wrong with your code though change font size to 24



来源:https://stackoverflow.com/questions/12218098/code-is-producing-a-gray-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!