Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Flickering during resizing of a window  (Read 1735 times)
0 Members and 1 Guest are viewing this topic.
Offline whatever

Senior Newbie





« Posted 2011-07-01 18: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

Junior Member


Projects: 2



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

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

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #2 - Posted 2011-09-21 00:39:41 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (53 views)
2014-04-15 18:08:23

BurntPizza (51 views)
2014-04-15 03:46:01

UprightPath (65 views)
2014-04-14 17:39:50

UprightPath (48 views)
2014-04-14 17:35:47

Porlus (65 views)
2014-04-14 15:48:38

tom_mai78101 (89 views)
2014-04-10 04:04:31

BurntPizza (149 views)
2014-04-08 23:06:04

tom_mai78101 (245 views)
2014-04-05 13:34:39

trollwarrior1 (203 views)
2014-04-04 12:06:45

CJLetsGame (210 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!