Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is code that renders with an outside Thread doing all the work. It does this by being able to render anything that implements the <code>Renderable</code> interface. I have tested this with both Swing and AWT (<code>JFrame</code> and <code>Frame</code>) and it works with no flickering. Note, it does flicker if you implement onto a <code>JRootPane</code> and set that pane as the <code>JFrame</code>'s root pane. This has to do with how the component is buffered, and could be fixed if that is how you want to use this.</p> <p>If this is still not what you were looking for, just say and I'll give it another go. This is actually fun, as it has been awhile since I've done any Java GUI work.</p> <p>Anyways, here you go:</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Frame; import java.awt.Graphics; import java.awt.Toolkit; import javax.swing.JFrame; public class SmoothResize extends Frame implements Renderable { public static void main(String[] args) { Toolkit.getDefaultToolkit().setDynamicLayout(true); System.setProperty("sun.awt.noerasebackground", "true"); SmoothResize srtest = new SmoothResize(); RenderThread renderThread = new RenderThread(srtest); renderThread.start(); srtest.setSize(100, 100); srtest.setVisible(true); } public SmoothResize() { } public void addNotify() { super.addNotify(); createBufferStrategy(2); } @Override public Dimension getSize() { return new Dimension(getWidth(), getHeight()); } @Override public Graphics acquireGraphics() { return this.getGraphics(); } } class RenderThread extends Thread { Renderable target; Dimension last_size = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); public RenderThread(Renderable d) { if (d == null) { throw new NullPointerException("Drawable target cannot be null."); } target = d; } @Override public void run() { while (true) { render(false); } } private synchronized void render(boolean force) { Dimension size; do { size = target.getSize(); if (size == null) { return; } Graphics draw = target.acquireGraphics(); if (draw == null) { return; } draw.setPaintMode(); int w = (int) (((double) (size.width)) / 2 + 0.5); int h = (int) (((double) (size.height)) / 2 + 0.5); draw.setColor(Color.YELLOW); draw.fillRect(0, h, w, h); draw.fillRect(w, 0, w, h); draw.setColor(Color.BLACK); draw.fillRect(0, 0, w, h); draw.fillRect(w, h, w, h); draw.dispose(); // Repeat the rendering if the target changed size } while (!size.equals(target.getSize())); } } interface Renderable { public Graphics acquireGraphics(); public Dimension getSize(); } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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