Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are not constructing on the <a href="http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html" rel="nofollow noreferrer">EDT</a>. An instance of <a href="http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html" rel="nofollow noreferrer"><code>javax.swing.Timer</code></a> makes this easy, as the action event handler executes on the <a href="http://java.sun.com/docs/books/tutorial/uiswing/concurrency/initial.html" rel="nofollow noreferrer">EDT</a>.</p> <p>Addendum 1: You <em>may</em> decide you need <a href="http://java.sun.com/docs/books/tutorial/extra/fullscreen/doublebuf.html" rel="nofollow noreferrer">double buffering</a>, but first compare the code below to yours and see. If you go that route, you might look at this <a href="http://www.stumbleupon.com/su/8wGoxm/www.cokeandcode.com/info/tut2d.html" rel="nofollow noreferrer">tutorial</a>.</p> <p>Addendum 2: The example below shows how to maintain an offscreen buffer, but it is sometimes easier to use the <a href="http://java.sun.com/javase/6/docs/api/javax/swing/JPanel.html#JPanel%28boolean%29" rel="nofollow noreferrer"><code>JPanel(boolean isDoubleBuffered)</code></a> constructor for a similar effect.</p> <pre><code>import java.awt.event.KeyAdapter; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import static java.awt.Color.*; /** @see http://stackoverflow.com/questions/2114455 */ public class GUI extends JPanel implements ActionListener { int x, y, x1, y1, x2, y2, changeY, changeY2; int changeX = 1; int changeX2 = 1; Timer t = new Timer(100, this); public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new GUI(true).display(); } }); } public GUI(boolean doubleBuffered) { super(doubleBuffered); this.setPreferredSize(new Dimension(320, 240)); } private void display() { JFrame frame = new JFrame("GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addKeyListener(new KeyListener()); frame.add(this); frame.pack(); frame.setVisible(true); t.start(); } @Override public void paintComponent(Graphics g) { g.setColor(WHITE); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(BLACK); g.fillRect(x, y, 100, 100); g.setColor(RED); g.fillRect(x1, y1, 50, 50); g.setColor(BLUE); g.fillRect(x2, y2, 25, 25); } public void change() { if (x1 &gt;= x + 50 &amp;&amp; changeY == 0 &amp;&amp; changeX == 1) { changeX = 0; changeY = 1; } else if (y1 &gt;= y + 50 &amp;&amp; changeX == 0 &amp;&amp; changeY == 1) { changeX = -1; changeY = 0; } else if (x1 &lt;= x &amp;&amp; changeX == -1 &amp;&amp; changeY == 0) { changeX = 0; changeY = -1; } else if (y1 &lt;= y &amp;&amp; changeY == -1 &amp;&amp; changeX == 0) { changeX = 1; changeY = 0; } x1 += changeX * 5; y1 += changeY * 5; } public void change2() { if (x2 &gt;= x1 + 25 &amp;&amp; changeY2 == 0 &amp;&amp; changeX2 == 1) { changeX2 = 0; changeY2 = 1; } else if (y2 &gt;= y1 + 25 &amp;&amp; changeX2 == 0 &amp;&amp; changeY2 == 1) { changeX2 = -1; changeY2 = 0; } else if (x2 &lt;= x1 &amp;&amp; changeX2 == -1 &amp;&amp; changeY2 == 0) { changeX2 = 0; changeY2 = -1; } else if (y2 &lt;= y1 &amp;&amp; changeY2 == -1 &amp;&amp; changeX2 == 0) { changeX2 = 1; changeY2 = 0; } x2 += changeX2 * 2; y2 += changeY2 * 2; } @Override public void actionPerformed(ActionEvent e) { change(); change2(); repaint(); } private class KeyListener extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { int d = 5; if (e.getKeyCode() == KeyEvent.VK_UP) { if (y - d &gt;= 0) { y -= d; y1 -= d; y2 -= d; } } if (e.getKeyCode() == KeyEvent.VK_DOWN) { if (y + 100 + d &lt;= getHeight()) { y += d; y1 += d; y2 += d; } } if (e.getKeyCode() == KeyEvent.VK_LEFT) { if (x - d &gt;= 0) { x -= d; x1 -= d; x2 -= d; } } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { if (x + 100 + d &lt;= getWidth()) { x += d; x1 += d; x2 += d; } } } } } </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. 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