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 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

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • 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 The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

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.