Note that there are some explanatory texts on larger screens.

plurals
  1. POAWT custom rendering - capture smooth resizes and eliminate resize flicker
    primarykey
    data
    text
    <p>I've been looking at this for several months and so far this is the best I have come up with.</p> <p>The structure (render outside of EDT) is not up for debate, as our application operates this way and will not be rewritten. The application has a layout model and a scripting model which are integrated and drive rendering, so the render must be performed outside of the AWT paint model.</p> <p>What I am trying to arrive at is the optimal and reliable way to perform custom rendering.</p> <p>The following SSCCE works fairly well for us. However, during frame resizes, it has 2 drawbacks:</p> <ul> <li>There is occasional flicker, especially on rapid resizes</li> <li>The "smooth resize" hack which is to invoke resize (via checkSize here) from a paint() call only works well for expansions. When reducing the frame it usually does not render until the mouse button is released</li> <li>Also, but not so evident here, it does throw occasional IllegalStateExceptions - is it OK to simply catch/ignore these?</li> </ul> <p>Also useful is input on whether this is the optimal approach for a custom render path that takes place outside of the EDT. I have tried most, and done fairly extensive research. This combination (backbuffer image, double buffer strategy) seems to work the best.</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.Insets; import java.awt.Toolkit; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.image.BufferStrategy; public class SmoothResize extends Frame implements ComponentListener, MouseMotionListener { public SmoothResize() { addComponentListener(this); addMouseMotionListener(this); } private boolean sizeChanged = false; private Dimension old = new Dimension(0, 0); private synchronized void checkSize(String source) { int width = getWidth(); int height = getHeight(); if (old.width == width &amp;&amp; old.height == height) return; sizeChanged = true; String type = (old.width &gt; width &amp;&amp; old.height &gt; height) ? "shrink" : (old.width &lt; width &amp;&amp; old.height &lt; height) ? "expand" : "resize"; System.out.println(source + " reports " + type + ": "+getWidth()+", "+getHeight()); old.setSize(width, height); } public void componentResized(ComponentEvent arg0) { checkSize("componentResized"); } public void mouseMoved(MouseEvent e) { checkSize("mouseMoved"); } public void paint(Graphics g) { checkSize("paint"); } public void update(Graphics g) { paint(g); } public void addNotify() { super.addNotify(); createBufferStrategy(2); } private synchronized void render() { BufferStrategy strategy = getBufferStrategy(); if (strategy==null || !sizeChanged) return; sizeChanged = false; // Render single frame do { // The following loop ensures that the contents of the drawing buffer // are consistent in case the underlying surface was recreated do { System.out.println("render"); Graphics draw = strategy.getDrawGraphics(); Insets i = getInsets(); int w = getWidth()-i.left-i.right; int h = getHeight()-i.top-i.bottom; draw.setColor(Color.YELLOW); draw.fillRect(i.left, i.top+(h/2), w/2, h/2); draw.fillRect(i.left+(w/2), i.top, w/2, h/2); draw.setColor(Color.BLACK); draw.fillRect(i.left, i.top, w/2, h/2); draw.fillRect(i.left+(w/2), i.top+(h/2), w/2, h/2); draw.dispose(); // Repeat the rendering if the drawing buffer contents // were restored } while (strategy.contentsRestored()); // Display the buffer strategy.show(); // Repeat the rendering if the drawing buffer was lost } while (strategy.contentsLost()); } public static void main(String[] args) { Toolkit.getDefaultToolkit().setDynamicLayout(true); System.setProperty("sun.awt.noerasebackground", "true"); SmoothResize srtest = new SmoothResize(); //srtest.setIgnoreRepaint(true); srtest.setSize(100, 100); srtest.setVisible(true); while (true) { srtest.render(); } } public void componentHidden(ComponentEvent arg0) { } public void componentMoved(ComponentEvent arg0) { } public void componentShown(ComponentEvent arg0) { } public void mouseDragged(MouseEvent e) { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload