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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:52:49+00:00 2026-05-13T10:52:49+00:00

I am using a loop to invoke double buffering painting. This, together with overriding

  • 0

I am using a loop to invoke double buffering painting. This, together with overriding my only Panel’s repaint method, is designed to pass complete control of repaint to my loop and only render when it necessary (i.e. some change was made in the GUI).

This is my rendering routine:

    Log.write("renderer painting");

    setNeedsRendering(false);

    Graphics g = frame.getBufferStrategy().getDrawGraphics();

    g.setFont(font);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, window.getWidth(),window.getHeight());

    if(frame != null)
        window.paint(g);

    g.dispose();

    frame.getBufferStrategy().show();

As you can see, it is pretty standard. I get the grpahics object from the buffer strategy (initialized to 2), make it all black and pass it to the paint method of my “window” object.

After window is done using the graphics object, I dispose of it and invoke show on the buffer strategy to display the contents of the virtual buffer.

It is important to note that window passes the graphics object to many other children components the populate the window and each one, in turn, uses the same instance of the graphics object to draw something onto the screen: text, shapes, or images.

My problem begins to show when the system is running and a large image is rendered. The image appears to be cut into seveeal pieces and drawn again and again (3-4 times) with different offsets inside of where the image is supposed to be rendered. See my attached images:

This is the original image:
alt text http://img109.imageshack.us/img109/8308/controller.png

This is what I get:
alt text http://img258.imageshack.us/img258/3248/probv.png

Note that in the second picture, I am rendering shapes over the picture – these are always at the correct position.

Any idea why this is happening?
If I save the image to file, as it is in memory, right before the call to g.drawImage(…) it is identical to the original.

  • 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-13T10:52:50+00:00Added an answer on May 13, 2026 at 10:52 am

    Uh, you are using Swing ?
    Normally Swing automatically renders the image, you can’t switch it off. The repaint()
    method is out of bounds because Swing has a very complicated rendering routine due to
    method compatibility for AWT widgets and several optimizations, inclusive drawing only
    when necessary !
    If you want to use the High-Speed Drawing API, you use a component with a BufferStrategy
    like JFrame and Window, use

    setIgnoreRepaint(false);

    to switch off Swing rendering, set up a drawing loop and paint the content itself.
    Or you can use JOGL for OpenGL rendering. The method you are using seems completely
    at odds with correct Java2D usage.

    Here the correct use:

    public final class FastDraw extends JFrame {
      private static final transient double NANO = 1.0e-9;
    
    
     private BufferStrategy bs;
    
     private BufferedImage frontImg;
    
     private BufferedImage backImg;
    
     private int PIC_WIDTH,
                 PIC_HEIGHT;
    
      private Timer timer;
    
      public FastDraw() {
        timer = new Timer(true);
        JMenu menu = new JMenu("Dummy");
        menu.add(new JMenuItem("Display me !"));
        menu.add(new JMenuItem("Display me, too !"));
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        setJMenuBar(menuBar);
    
        setIgnoreRepaint(true);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent evt) {
            super.windowClosing(evt);
            timer.cancel();
            dispose();
            System.exit(0);
          }
        });
        try {
          backImg = javax.imageio.ImageIO.read(new File("MyView"));
          frontImg = javax.imageio.ImageIO.read(new File("MyView"));
        }
        catch (IOException e) {
          System.out.println(e.getMessage());
        }
        PIC_WIDTH = backImg.getWidth();
        PIC_HEIGHT = backImg.getHeight();
        setSize(PIC_WIDTH, PIC_HEIGHT);
    
    
        createBufferStrategy(1); // Double buffering
        bs = getBufferStrategy();
        timer.schedule(new Drawer(),0,20);
      }
      public static void main(String[] args) {
        new FastDraw();
      }
    
      private class Drawer extends TimerTask {
    
        private VolatileImage img;
    
        private int count = 0;
    
        private double time = 0;
    
        public void run() {
          long begin = System.nanoTime();
          Graphics2D  g  = (Graphics2D) bs.getDrawGraphics();
          GraphicsConfiguration gc = g.getDeviceConfiguration();
          if (img == null)
            img = gc.createCompatibleVolatileImage(PIC_WIDTH, PIC_HEIGHT);
          Graphics2D g2 = img.createGraphics();
          // Zeichenschleife
          do {
            int valStatus = img.validate(gc);
            if (valStatus == VolatileImage.IMAGE_OK)
              g2.drawImage(backImg,0,0,null);
            else {
              g.drawImage(frontImg, 0, 0, null);
            }
            // volatile image is ready
            g.drawImage(img,0,50,null);
            bs.show();
          } while (img.contentsLost());
          time = NANO*(System.nanoTime()-begin);
          count++;
          if (count % 100 == 0)
            System.out.println(1.0/time);
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to clean up this type of loop using LINQ? List<Car>
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I am using XmlReader in .NET to parse an XML file using a loop:
I cannot seem to access the context object using a loop context is set:
I'm using reflection to loop through a Type 's properties and set certain types
I just want a quick way (and preferably not using a while loop)of createing
How can I iterate over each file in a directory using a for loop?
I'm using the following code to loop through a directory to print out the
I'm using the following syntax to loop through a list collection: For Each PropertyActor
I have a loop that reads each line in a file using getline() :

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.