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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:21:33+00:00 2026-05-13T07:21:33+00:00

Next semester we have a module in making Java applications in a team. The

  • 0

Next semester we have a module in making Java applications in a team. The requirement of the module is to make a game. Over the Christmas holidays I’ve been doing a little practice, but I can’t figure out the best way to draw graphics.

I’m using the Java Graphics2D object to paint shapes on screen, and calling repaint() 30 times a second, but this flickers terribly. Is there a better way to paint high performance 2D graphics in Java?

  • 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-13T07:21:33+00:00Added an answer on May 13, 2026 at 7:21 am

    What you want to do is to create a canvas component with a BufferStrategy and render to that, the code below should show you how that works, I’ve extracted the code from my self written Engine over here.

    Performance solely depends on the stuff you want to draw, my games mostly use images. With around 1500 of them I’m still above 200 FPS at 480×480. And with just 100 images I’m hitting 6k FPS when disabling the frame limiting.

    A small game (this one has around 120 images at once at the screen) I’ve created can be found here (yes the approach below also works fine as an applet.)

    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.Toolkit;
    import java.awt.Transparency;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    
    public class Test extends Thread {
        private boolean isRunning = true;
        private Canvas canvas;
        private BufferStrategy strategy;
        private BufferedImage background;
        private Graphics2D backgroundGraphics;
        private Graphics2D graphics;
        private JFrame frame;
        private int width = 320;
        private int height = 240;
        private int scale = 1;
        private GraphicsConfiguration config =
                GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice()
                    .getDefaultConfiguration();
    
        // create a hardware accelerated image
        public final BufferedImage create(final int width, final int height,
                final boolean alpha) {
            return config.createCompatibleImage(width, height, alpha
                    ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
        }
    
        // Setup
        public Test() {
            // JFrame
            frame = new JFrame();
            frame.addWindowListener(new FrameClose());
            frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            frame.setSize(width * scale, height * scale);
            frame.setVisible(true);
    
            // Canvas
            canvas = new Canvas(config);
            canvas.setSize(width * scale, height * scale);
            frame.add(canvas, 0);
    
            // Background & Buffer
            background = create(width, height, false);
            canvas.createBufferStrategy(2);
            do {
                strategy = canvas.getBufferStrategy();
            } while (strategy == null);
            start();
        }
    
        private class FrameClose extends WindowAdapter {
            @Override
            public void windowClosing(final WindowEvent e) {
                isRunning = false;
            }
        }
    
        // Screen and buffer stuff
        private Graphics2D getBuffer() {
            if (graphics == null) {
                try {
                    graphics = (Graphics2D) strategy.getDrawGraphics();
                } catch (IllegalStateException e) {
                    return null;
                }
            }
            return graphics;
        }
    
        private boolean updateScreen() {
            graphics.dispose();
            graphics = null;
            try {
                strategy.show();
                Toolkit.getDefaultToolkit().sync();
                return (!strategy.contentsLost());
    
            } catch (NullPointerException e) {
                return true;
    
            } catch (IllegalStateException e) {
                return true;
            }
        }
    
        public void run() {
            backgroundGraphics = (Graphics2D) background.getGraphics();
            long fpsWait = (long) (1.0 / 30 * 1000);
            main: while (isRunning) {
                long renderStart = System.nanoTime();
                updateGame();
    
                // Update Graphics
                do {
                    Graphics2D bg = getBuffer();
                    if (!isRunning) {
                        break main;
                    }
                    renderGame(backgroundGraphics); // this calls your draw method
                    // thingy
                    if (scale != 1) {
                        bg.drawImage(background, 0, 0, width * scale, height
                                * scale, 0, 0, width, height, null);
                    } else {
                        bg.drawImage(background, 0, 0, null);
                    }
                    bg.dispose();
                } while (!updateScreen());
    
                // Better do some FPS limiting here
                long renderTime = (System.nanoTime() - renderStart) / 1000000;
                try {
                    Thread.sleep(Math.max(0, fpsWait - renderTime));
                } catch (InterruptedException e) {
                    Thread.interrupted();
                    break;
                }
                renderTime = (System.nanoTime() - renderStart) / 1000000;
    
            }
            frame.dispose();
        }
    
        public void updateGame() {
            // update game logic here
        }
    
        public void renderGame(Graphics2D g) {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, width, height);
        }
    
        public static void main(final String args[]) {
            new Test();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer you need to create images for those states like focussed… May 14, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer Following a suggestion from Bruce Eckel's book Thinking in Java,… May 14, 2026 at 4:38 pm
  • Editorial Team
    Editorial Team added an answer This should work: if ($('.' + className + ' tr:last… May 14, 2026 at 4:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.