Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, the first problem you have is a resource issue.</p> <p>Resources stored "within" the application (typically known as embedded resources) can't be loaded like external resources.</p> <pre><code>chick[0] = tk.createImage("stand.png"); </code></pre> <p>Is expecting a file <code>"./stand.png"</code> which doesn't exist. Instead, you need to load the resource through the <code>Class#getResource</code> API...</p> <pre><code>chick[0] = tk.createImage(getClass().getResource("/stand.png")); </code></pre> <p>The second problem you will face is the fact that you are overriding <code>paint</code> of a top level container. This really shouldn't be done. Let's start with the fact it's not double buffered and end with the fact that frames have decorations which sit inside the viewable area. This means that the decorations will overlap what ever you paint to the surface...not pretty...</p> <p>The third problem is you are not telling the image where it should move to. It's static.</p> <p>You need some kind of x/y value that tells the image where it should be painted. You would modify these values by a given x/y delta within you thread before you called <code>repaint</code>...</p> <p>The forth problem you might have is the fact that you are using AWT...which is kind of dated. Swing would solve your double buffering issue for you...IMHO, would make a better choice - there's a lot more documentation and examples on Swing laying around now days ;)</p> <p>While I'm on my hobble horse...I would, personally, recommend <a href="http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html" rel="nofollow noreferrer"><code>ImageIO</code></a> over <code>Toolkit#createImage</code> or <code>ImageIcon</code>, mostly because it supports more formats, but also because it will throw an <code>Exception</code> when the image can't be read for some reason...</p> <p>I have a simple example if Swing, but I won't post it, because I'll get in trouble for running of topic...let me know if you would like to see it</p> <p><strong>Updated with Swing example</strong></p> <p>This uses a embedded image in the default package...</p> <p><img src="https://i.stack.imgur.com/iRab6.png" alt="enter image description here"></p> <pre><code>import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class ChickenDance { public static void main(String[] args) { new ChickenDance(); } public ChickenDance() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private BufferedImage chicken; private int xPos; private int yPos; private int xDelta = 4; public TestPane() { try { chicken = ImageIO.read(getClass().getResource("/Chicken.png")); } catch (IOException ex) { Logger.getLogger(ChickenDance.class.getName()).log(Level.SEVERE, null, ex); } Timer timer = new Timer(40, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { xPos += xDelta; if (xPos + chicken.getWidth() &gt; getWidth()) { xPos = getWidth() - chicken.getWidth(); xDelta *= -1; } else if (xPos &lt; 0) { xPos = 0; xDelta *= -1; } yPos = (getHeight() - chicken.getHeight()) / 2; repaint(); } }); if (chicken != null) { timer.start(); } } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (chicken != null) { Graphics2D g2d = (Graphics2D) g.create(); g2d.drawImage(chicken, xPos, yPos, this); g2d.dispose(); } } } } </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