Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because the rendering is trivial, I find this variation of your example to be very smooth. The render time is well below a half millisecond, so the 12 millisecond period (~83 Hz) is <em>plenty</em> of time to finish a frame, typically taking less that 10% of one core. As the render time grows, the timer thread becomes saturated, and events are coalesced. The effect is magnified on a single core, as rendering competes with garbage collection and external processing demands. Java is not a real-time system, and not all schedulers are created equal.</p> <p>You'll certainly want to profile your actual code, as suggested <a href="https://stackoverflow.com/questions/5916028/how-do-i-get-appcontext-to-release-awt-components-so-they-can-be-garbage-collecte/5918785#5918785">here</a>, to see any correlation with fluctuating performance. One alternative approach is to lengthen the period (decrease the frequency) to meet your rendering deadline and use a larger increment in <code>moveImage()</code> to get the same velocity.</p> <pre><code>import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class Main extends JFrame { private static final int W = 800; private static final int H = 400; public Main() { super("JFrame"); this.add(new ImagePanel()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); setSize(W, H); this.setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Main(); } }); } class ImagePanel extends JPanel { Timer movementTimer; int x, y; public ImagePanel() { x = 0; y = 0; movementTimer = new Timer(12, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { moveImage(); repaint(); } }); movementTimer.start(); } public void moveImage() { x++; y++; if (x &gt; W) { x = 0; } if (y &gt; H) { y = 0; } } @Override public void paintComponent(Graphics g) { super.paintComponent(g); long start = System.nanoTime(); g.setColor(Color.RED); g.fillRect(0, 0, W, H); g.setColor(Color.BLUE); g.fillRect(x, y, 50, 50); double delta = (System.nanoTime() - start) / 1000000d; g.drawString(String.format("%1$5.3f", delta), 5, 15); } } } </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.
    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