Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not a shadow. Its the border of your sprite. It just happens to be black and appears as a shadow. If you change the amount you shift your sprite (lets say by 50 pixels, not just 1) you will see what i mean.</p> <p>To fix it what you need to do is to draw the background as well each time you update the location of your sprite. Although this will probably produce flickering.</p> <p>The correct way to do it is to change the way you draw your objects. You need to override the paintComponent method of your panel and then simply call repaint each time you have updated the locations of your sprites.</p> <p><strong>EDIT:</strong></p> <p>See this code sample for basic usage. NOTE: This is NOT how you should write animation using Threads. I wrote that to show you what goes in the paintComponent method and wrote the animation Thread to show you that the "shadow" you mentioned is gone. NEVER have a non ending run loop in a thread :)</p> <pre><code>import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Test { public static void main(String[] args) { JFrame f = new JFrame("Test"); MyPanel c = new MyPanel(); f.getContentPane().add(c); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(350, 100); f.setVisible(true); } } class MyPanel extends JPanel { int x = 0; boolean toTheRight = true; public MyPanel() { new Thread(new Runnable() { @Override public void run() { while (true) { x = (toTheRight)?x+5:x-5; if (x&gt;300) toTheRight = false; if (x&lt;0) toTheRight = true; repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.setPaint(Color.white); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setPaint(Color.red); g2.fillOval(x-2, 50, 4, 4); } } </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