Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:48:12+00:00 2026-06-10T17:48:12+00:00

I have a method in a JPanel private void paintScore(Graphics2D g) { Font scoreFont

  • 0

I have a method in a 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);

}

When I call this method, the very first time it appears to work. Then, after that it never works again.

If I comment out the invocation of the method, it consistently works.

However, If I call JFrame's revalidate() methods, the program also consistently works.

I am not sure what is going on here.

I can post full source code if required.

Thanks in Advance


Here is the rest of the code.

Main JPanel extended class.

       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 is a class which simply is a wrapper for BufferedImage.

Thanks again guys

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:48:14+00:00Added an answer on June 10, 2026 at 5:48 pm

    Try doing this:

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

    You forgot to add mouse motion listener, and to repaint panel. revalidate(); has no use in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method like so private bool VerbMethod(string httpVerb, string methodName, string url,
I have a JPanel on which some drawing is performed using paintComponent method and
I have a class whitch extends JPanel: public class ButtonPanel extends JPanel { private
I have JFrame , and I added my JPanel class with paintComponent() method. For
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method with two parameters: bool1 and bool2. Both are boolean of course.
I have method that returns module path of given class name def findModulePath(path, className):
I have method in a class that I need to make sure is only
I've got some 3rd party beans that have method signatures that fit quite well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.