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

  • Home
  • SEARCH
  • 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 6220449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:57:30+00:00 2026-05-24T07:57:30+00:00

I’m planning to write a simple spaceshooter. I have read that the repaint() method

  • 0

I’m planning to write a simple spaceshooter. I have read that the repaint() method is only a request, and it doesn’t execute every time it’s called. I believe I’m noticing the effects of this, as my spaceship tends to lag ever so slightly when I’m moving it. Currently I’m simply drawing my ship in a a JPanel’s paintComponent() method, and keep calling repaint() on regular intervals (my panel’s also Runnable). Seeing as repaint() may potentially screw me over, I’m trying to find a way to work arround it, however I’ve ran out of ideas. The code I have so far:

private void renderGraphics() {
    if (MyImage == null) {
        MyImage = new BufferedImage(getPreferredSize().width,
                getPreferredSize().height, BufferedImage.TYPE_INT_RGB);
    }
    MyGraphics = MyImage.getGraphics();
    MyGraphics.setColor(Color.BLACK);
    MyGraphics.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
    MyGraphics.drawImage(ship.getImage(), ship.getCurrentX(), ship.getCurrentY(), null);       
}

The idea was to create my own graphics, and then make the JPanel draw it, and keep calling this instead of repaint() in my run() method, however I have no idea how to do that. I’d appriciate any input on the matter.

  • 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-05-24T07:57:32+00:00Added an answer on May 24, 2026 at 7:57 am

    There are multiple ways to approach this.

    The best is probably to use BufferStrategy and draw to that, of which I have included a code snippet that should work for you.

    You can take this one step further and abandon Swing altogether, just using Frame/BufferStrategy. There is a fully working example (from which the code snippet was taken and adapted) in my question here:

    AWT custom rendering – capture smooth resizes and eliminate resize flicker

    Anyway, here is an implementation BufferStrategy that you should be able to just drop in:

    // you should be extending JFrame
    public void addNotify() {
        super.addNotify();
        createBufferStrategy(2);
    }
    
    private synchronized void render() {
        BufferStrategy strategy = getBufferStrategy();
        if (strategy==null) return;
        sizeChanged = false;
        // Render single frame
        do {
            // The following loop ensures that the contents of the drawing buffer
            // are consistent in case the underlying surface was recreated
            do {
                MyGraphics draw = strategy.getDrawGraphics();
                draw.setColor(Color.BLACK);
                draw.fillRect(0, 0, getPreferredSize().width, getPreferredSize().height);
                draw.drawImage(ship.getImage(), ship.getCurrentX(), ship.getCurrentY(), null);
                draw.dispose();
    
                // Repeat the rendering if the drawing buffer contents 
                // were restored
            } while (strategy.contentsRestored());
    
            // Display the buffer
            strategy.show();
    
            // Repeat the rendering if the drawing buffer was lost
        } while (strategy.contentsLost());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.