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 8703103
In Process

The Archive Base Latest Questions

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

I am making a game canvas using swing and decided to use JTextField ‘s

  • 0

I am making a game canvas using swing and decided to use JTextField‘s for the input of a username and password into the panel.

I am buffering an image then rendering it onto the screen instead of drawing everything directly onto the panel real-time.

I have ran into a problem though, I paint a background and have set both of my text fields to opaque, but it seems that whenever I go to enter something into those text field’s it flashes a black box where the JTextField is.

It happens in both of my username and password fields. Any idea of what the cause of this could be?

Other helpful information: Whenever I click on a text box, both of the components flash black where the first character would be.

EDIT — I just noticed that the login button I have also flashes black when MOUSE_ENTERED and MOUSE_EXIT.

public class GamePanel extends JPanel implements Runnable {


public GamePanel(int width, int height) {
    this.pWidth = width; 
    this.pHeight = height;

    setController(new LoginGameController(this));

    setPreferredSize( new Dimension(pWidth, pHeight));
    setBackground(Color.BLACK);

    setFocusable(true);
    requestFocus();    // the JPanel now has focus, so receives key events

    // create game components

    addMouseListener(this);
    addKeyListener(this);

    setLayout(null);

    startGame();
}
 private void startGame()
  // initialise and start the thread 
  { if (animator == null) {
      animator = new Thread(this);
      animator.start();
    }
  }

public void run() {
    while(true) {
          gameUpdate(); 
          if(getGraphics() != null){
              gameRender();   // render the game to a buffer
              paintScreen();  // draw the buffer on-screen
          }
          try {
            Thread.sleep(28);
          } catch (InterruptedException e) {}
    }
}

private void paintScreen() {
    Graphics2D g = (Graphics2D) getGraphics();

    if ((g != null) && (img != null))
            g.drawImage(img, 0, 0, null);

    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

private void gameRender() {
    if(getWidth() > 0 && getHeight() > 0)
        img = createImage(getWidth(), getHeight());

    if(img != null) {
        Graphics2D g = (Graphics2D) img.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, pWidth, pHeight);

        getController().render(img);

        paintComponents(img.getGraphics());


    }
}

}

Here is text fields: (from a seperate class entirely calling to the GamePanel using getPanel()…)

//Setup Login fields
    usernameTF = new JTextField();
    usernameTF.setOpaque(false);
    usernameTF.getCaret().setBlinkRate(0);
    usernameTF.setForeground(Color.WHITE);
    usernameTF.setBounds(USERNAME_FIELD);
    usernameTF.setBorder(null);
    getPanel().add(usernameTF);

    passwordTF = new JPasswordField();
    passwordTF.setOpaque(false);
    passwordTF.getCaret().setBlinkRate(0);
    passwordTF.setForeground(Color.WHITE);
    passwordTF.setBounds(PASSWORD_FIELD);
    passwordTF.setBorder(null);
    getPanel().add(passwordTF);

    loginBtn = new JButton();
    loginBtn.setOpaque(false);
    loginBtn.setBackground(null);
    loginBtn.setBorder(null);
    loginBtn.setBounds(LOGIN_BUTTON);
    loginBtn.addMouseListener(getPanel());
    getPanel().add(loginBtn);

Thanks!

  • 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-13T02:48:13+00:00Added an answer on June 13, 2026 at 2:48 am

    The basic problem is, you circumventing Swings repaint process and not honoring the EDT when you up-date your graphics.

    JComponent#getGraphics is a temporary/scratch buffer which will be re-draw on the next repaint. Also, if you didn’t create the graphics, you shouldn’t dispose it!

    public void run() {
        while(true) {
              gameUpdate(); 
              if(getGraphics() != null){
                  gameRender();   // render the game to a buffer
                  try {
                      SwingUtilities.invokeAndWait(new Runnable() {
                          public void run() {
                              paintScreen();  // draw the buffer on-screen
                          }
                      });
                  } catch (Exception exp) {
                      exp.printStackTrace(); // please clean this up
                  }
              }
              try {
                Thread.sleep(28);
              } catch (InterruptedException e) {}
        }
    }
    

    I don’t know if this will fix it, but it can’t hurt. (This WILL effect you FPS and you should be taking into consideration how long it took to paint and how long you want to wait)

    Alternativly, rather then calling paintScreen(), you could call repaint (or have paintScreen do it) and override the paintComponent method to paint your buffer.

    This would allow Swing to resume control over the paint process properly.

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

Sidebar

Related Questions

I am making a game engine using HTML5 canvas, and I have decided to
I am making a game using html canvas. Here is my progress: http://db.tt/ei3LlR (use
I'm making a game using javascript + canvas. I use the code below to
I am making a Javascript game with the canvas tag, and I am using
I'm making my first game in canvas/JS and I'm running into an issue with
Background I'm making a canvas game which assumes the user to be using a
I'm making a game in HTML5's Canvas + JavaScript. My CPU use for the
I'm making a game using canvas, and javascript. When the page is longer than
I'm making a game using HTML5 canvas, How do I add a TextBox and
I'm making a javascript game without using canvas, and I want the screen to

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.