Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Flickering during resizing of a window  (Read 848 times)
0 Members and 2 Guests are viewing this topic.
Offline whatever

JGO n00b
*

Posts: 6



« on: 2011-07-01 12:14:19 »

To avoid flickering during the resize of my game window I'am using the sun.awt.noerasebackground property. This works oké for simple drawings. However when the drawing complexity increases so does the flickering! I have tried different things to get rid of the flickering but I'am running out of options.

By the way... I don't wan't to use Toolkit.getDefaultToolkit().setDynamicLayout(false). This eliminates flickering completely but makes it impossible to resize the drawing during a resize.

OS: Vista 64bit;
JRE: build 1.6.0_26-b03

Because code says more than a thousand words  Wink see the example below. Use the static variable COMPLEXITY to increase the "complexity" of the drawing till the point the flickering starts.

Any suggestions to get rid of this flickering?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class FlickerDemo
{
   // The frame to contain this canvas
  private JFrame jFrame = null;
   
   // The canvas to draw on
  private Canvas canvas = null;
   
   // The buffer strategy
  private BufferStrategy strategy = null;
   
   // Drawing routine complexity
  private static final int COMPLEXITY = 10000;
   
   public static void main(String[] args)
   {
      new FlickerDemo().start();

   }
   
   public FlickerDemo()
   {
      // No flickering during resize
     System.setProperty("sun.awt.noerasebackground", "true");
     
      // Acquiring the current Graphics Environment, Device and Configuration
     GraphicsEnvironment graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice graphicsDev = graphicsEnv.getDefaultScreenDevice();
      GraphicsConfiguration graphicsConf = graphicsDev.getDefaultConfiguration();

      // Create a canvas
     canvas = new Canvas(graphicsConf);      
      canvas.setFocusTraversalKeysEnabled(false);
      canvas.setIgnoreRepaint(true);
     
      // Set background color to red to make flickering more visible
     canvas.setBackground(Color.red);
     
      // Create a frame
     jFrame = new JFrame(graphicsConf);
      jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jFrame.setTitle("FlickerDemo");
      jFrame.setFocusTraversalKeysEnabled(false);
      jFrame.setIgnoreRepaint(true);
      jFrame.setAlwaysOnTop(false);
      jFrame.setUndecorated(false);            
     
      // Add canvas to the frame
     jFrame.add(canvas);
     
      // Make the window visible
     jFrame.pack();
      jFrame.setResizable(true);
      jFrame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
      jFrame.setVisible(true);
     
      // Set the size and location of the jframe
     jFrame.setSize(800, 600);
      jFrame.setLocation(0, 0);
     
      // Create the buffering strategy which will allow AWT to manage the accelerated graphics
     canvas.createBufferStrategy(2);
      strategy = canvas.getBufferStrategy();
   }

   public void start()
   {
      while(true)
      {        
         // Get the size of the canvas
        Dimension windowSize = canvas.getSize();
         int screenX = windowSize.width;
         int screenY = windowSize.height;
         
         // Get hold of a graphics context for the accelerated surface
        Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
         
         // Fill background
        g.setColor(Color.black);
         g.fillRect(0, 0, screenX,screenY);

         // Draw contents of window
        draw(g, screenX, screenY);
         
         // finally, we've completed drawing so clear up the graphics
        // and flip the buffer over
        g.dispose();
         strategy.show();
      }
   }
   
   /*
    * Draw a square in the center of the canvas
    */

   private void draw(Graphics2D g, int screenX, int screenY)
   {
      g.setColor(Color.red);
      for(int i=0; i<COMPLEXITY; i++)
      {
         g.fillRect(screenX / 2 - 10, screenY / 2 - 10, 20, 20);
      }
   }  
}
Offline TheUzo007

JGO n00b
*

Posts: 15



« Reply #1 on: 2011-09-20 14:33:07 »

Did you try to use setDoubleBuffered(true) on canvas?

Offline ra4king

JGO Kernel
*****

Posts: 3155
Medals: 196


I'm the King!


« Reply #2 on: 2011-09-20 18:39:41 »

@TheUzo007
1. Please don't revive old threads.
2. All Swing components are automatically double buffered.

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.057 seconds with 19 queries.