Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're trying to do AWT graphics in a Swing program -- don't do this. Instead there's no place for update in a Swing graphics program and you should not be overriding the paint method but rather the JPanel's paintComponent method. This way you'll take advantage of Swing's double buffering.</p> <p>Here's an <a href="http://sscce.org" rel="nofollow">SSCCE</a> that shows AWT vs Swing animation, though unfortunately, I don't see much difference between the two as I'd hoped.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; @SuppressWarnings("serial") public class PaintVsPaintComponent extends JPanel { public static final String DUKE_WAVE = "http://duke.kenai.com/iconSized/duke4.gif"; private static final int PREF_WIDTH = 300; private static final int PREF_HEIGHT = 250; private static final int TIMER_DELAY = 20; private static final int DELTA_X = 1; private static final int DELTA_Y = DELTA_X; private boolean awtDrawing; private BufferedImage image; private int x = 0; private int y = 0; public PaintVsPaintComponent(boolean awtDrawing, BufferedImage image) { this.awtDrawing = awtDrawing; this.image = image; new Timer(TIMER_DELAY, new ActionListener() { public void actionPerformed(ActionEvent ae) { timerActionPerformed(ae); } }).start(); } private void timerActionPerformed(ActionEvent ae) { x += DELTA_X; y += DELTA_Y; if (x &gt;= getWidth()) { x = 0; } if (y &gt;= getHeight()) { y = 0; } repaint(); } @Override public void paint(Graphics g) { super.paint(g); if (awtDrawing) { drawImage(g); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (!awtDrawing) { drawImage(g); } } private void drawImage(Graphics g) { g.drawImage(image, x, y, null); } @Override public void update(Graphics g) { if (awtDrawing) { paint(g); } else { super.update(g); } } @Override public Dimension getPreferredSize() { return new Dimension(PREF_WIDTH, PREF_HEIGHT); } private static void createAndShowGui() { BufferedImage dukeWaveImage = null; URL dukeWaveUrl; try { dukeWaveUrl = new URL(DUKE_WAVE); dukeWaveImage = ImageIO.read(dukeWaveUrl); PaintVsPaintComponent awtPanel = new PaintVsPaintComponent(true, dukeWaveImage); PaintVsPaintComponent swingPanel = new PaintVsPaintComponent(false, dukeWaveImage); awtPanel.setBorder(BorderFactory.createTitledBorder("AWT Panel")); swingPanel.setBorder(BorderFactory.createTitledBorder("Swing Panel")); JPanel gridPanel = new JPanel(new GridLayout(1, 0)); gridPanel.add(awtPanel); gridPanel.add(swingPanel); JFrame frame = new JFrame("PaintVsPaintComponent"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(gridPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } </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. 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