Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This answer is left here for reference, but is not the correct answer because it renders inside the EDT thread.</strong></p> <p>Here is a working fix! :D Basically the problem is that the ComponentResized is not appropriately called until the mouse is released after a shrink. Also, because the paint and checkSize methods are synchronized, they can exclude each other in rare instances. The fix is to override the validate method in the Frame class. This method is always called if the Frame changes state, including shrinks and growths. So we simply have to check the size in the validate and we can actually completely forget about using a ComponentResized method at all.</p> <p>So, here is working code that compiles as is. I changed some of the variable names to improve my personal readability.</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Insets; import java.awt.Toolkit; import java.awt.image.BufferStrategy; import java.awt.Frame; public class SmoothResize extends Frame { 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); } public SmoothResize() { render(); } private Dimension old_size = new Dimension(0, 0); private Dimension new_size = new Dimension(0, 0); public void validate() { super.validate(); new_size.width = getWidth(); new_size.height = getHeight(); if (old_size.equals(new_size)) { return; } else { render(); } } public void paint(Graphics g) { validate(); } public void update(Graphics g) { paint(g); } public void addNotify() { super.addNotify(); createBufferStrategy(2); } protected synchronized void render() { BufferStrategy strategy = getBufferStrategy(); if (strategy == null) { return; } // 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 { Graphics draw = strategy.getDrawGraphics(); Insets i = getInsets(); int w = (int)(((double)(getWidth() - i.left - i.right))/2+0.5); int h = (int)(((double)(getHeight() - i.top - i.bottom))/2+0.5); draw.setColor(Color.YELLOW); draw.fillRect(i.left, i.top + h, w,h); draw.fillRect(i.left + w, i.top, w,h); draw.setColor(Color.BLACK); draw.fillRect(i.left, i.top, w, h); draw.fillRect(i.left + w, i.top + h, w,h); 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()); } } </code></pre> <p>I hope that works for you!</p> <p><strike>Also, last second edit, I changed the logical ternary operation for the selection of your shrink or expand String. The last comparison was unnecessary because for the comparison in question, the values can be greater than, less than or equal to each other. There is not another possibility that would not generate a <code>NullPointerException</code>.</strike></p> <p>The stricken text is no longer relevant as I have completely removed that entire method. I indicate the other changes I've made to the original post as comments.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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