Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Adding to @camickr's suggestion:</p> <p>Create a BufferedImage (BI), wrap it in a IconImage, set it as the icon for a JLabel. Paint changes onto the BI and call JLabel's repaint() to flush those. changes to the screen. Caching the partial image in a BI lessens the amount of work for the paint routine. It just needs to blit the image, not render the image. Use SwingWorkers to deploy background threads to run the calculations and pass the results back to the EDT for painting.</p> <p>As long as you're talking about a static image, that will work fine. If you are considering something more like a video game (some fixed images and other moving images) take a look at VolatileImage. There is a good description here: <a href="http://gpwiki.org/index.php/Java:Tutorials:VolatileImage" rel="nofollow">http://gpwiki.org/index.php/Java:Tutorials:VolatileImage</a></p> <p><strong>Update:</strong></p> <p>The following gives me a bit over 80 fps:</p> <pre><code>public class Demo extends javax.swing.JPanel { private Image src = null; public Demo() { new Worker().execute(); } protected void paintComponent(Graphics g) { super.paintComponent(g); if (src != null) g.drawImage(src, 0, 0, this); } private class Worker extends SwingWorker&lt;Void, Image&gt;{ private final Color[] colors = { Color.red, Color.green, Color.blue }; protected void process(List&lt;Image&gt; chunks){ for (Image bufferedImage : chunks){ src = bufferedImage; repaint(); } } protected Void doInBackground() throws Exception{ int frames = 0; int[] mem = new int[1024 * 768]; long start = System.currentTimeMillis(); long end = start + 15000; long last = start; while (last &lt; end){ int col = colors[frames % colors.length].getRGB(); for (int y = 0; y &lt; 768; y++) for (int x = 0; x &lt; 1024; x++) mem[x + y * 1024] = col; Image img = createImage(new MemoryImageSource(1024, 768, mem, 0, 1024)); BufferedImage bi = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, 0, 0, null); g2.dispose(); publish(bi); last = System.currentTimeMillis(); frames++; } System.err.println("Frames = " + frames + ", fps = " + ((double) frames / (last - start) * 1000)); return null; } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run(){ JFrame jf = new JFrame(); jf.getContentPane().add(new Demo(), BorderLayout.CENTER); jf.setSize(1024, 768); jf.setVisible(true); } }); } } </code></pre>
 

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