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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:54:44+00:00 2026-06-08T10:54:44+00:00

I have a simple Java JFrame canvas. I am updating what is on the

  • 0

I have a simple Java JFrame canvas. I am updating what is on the screen every half second or so, and have flickering. I want to implement double buffering to eliminate the flickering, but I am fairly new to Java and am unfamiliar with how to do so. I have found some examples, but not sure how to implement their methods into mine.

Below is the basic setup of how I have things now. This is not my exact code- just an example of the basic setup.

Thanks for any push in the right direction!

public class myCanvas extends Canvas{
    //variables
    Color rectColor=Color.red;

    public myCanvas()
    {
    }

    public void paint(Graphics graphics)
    {
        //initial setup, such as
        graphics.setColor(rectColor);
        graphics.fillRect(X,Y,W,H);
    }
    public static void main(String[] args)
    {
        myCanvas canvas = new myCanvas();
        JFrame frame = new JFrame("GUI");
        frame.setSize(frameWidth,frameHeight);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(canvas);
        frame.setVisible(true);
        while(true){
            rectColor=Color.green;
            canvas.validate();
            canvas.repaint();
            Thread.sleep(500);
        }
    }
}
  • 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-08T10:54:46+00:00Added an answer on June 8, 2026 at 10:54 am

    First of all, you should avoid mixing heavy- and lightweight components (AWT and SWING), mostly because they use very different methods of drawing to the display (read here if you want to know more).

    So instead of the Canvas, you could use a JPanel. This also gives you a potential solution, because JPanel has a method setDoubleBuffered(boolean), more specifically, this is implemented in the JComponent class.

    I believe it would be sufficient to just replace

    public class myCanvas extends Canvas
    

    by

    public class myCanvas extends JPanel
    

    . Although I haven’t tested this, I hope it helps you!

    EDIT:
    Also, of course, when setting up your frame and canvas in the main method, you’d have to place the method call

    canvas.setDoubleBuffered(true);
    

    somewhere.

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

Sidebar

Related Questions

I have created a simple Java application which has a JFrame and few JButtons.
I have Simple java program named MainController.java. Wehn I try to compile it from
I have a simple java code that reads text csv file that contains sentences
I have a simple Java questions and I need a simple answer, if possible.
I have a simple Java class that I need to serialize to be stored
I have done simple java app for blackberry, while building am getting following error.
I have a simple Java class that has some methods: public class Utils {
I have a simple Java class in which I invoke a call to a
I have a simple java project (adapted from the example here ), which is
I am trying to have a simple java server respond to an XmlHttpRequest, but

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.