Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code is producing a gray screen

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.

like image 344
DreamsOfHummus Avatar asked Mar 07 '26 21:03

DreamsOfHummus


2 Answers

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

Pong

like image 72
Andrew Thompson Avatar answered Mar 10 '26 12:03

Andrew Thompson


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

like image 29
UIlrvnd Avatar answered Mar 10 '26 12:03

UIlrvnd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!