Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><img src="https://i.stack.imgur.com/9YeOU.png" alt="Racing UI"></p> <p>I made a number of changes. Here are some I can recall.</p> <ol> <li>Refactored custom painting from top-level container to <code>JPanel</code>, and moved painting to <code>paintComponent()</code>.</li> <li>Removed <code>Thread</code> and <code>Thread.sleep(n)</code> &amp; replaced with a <code>Timer</code>/<code>ActionListener</code>.</li> <li>Constructed the GUI on the EDT.</li> <li>Removed setting the size of the <code>JFrame</code>. Instead sets a preferred size for the <code>JPanel</code> (the actual drawing area) and calls <code>JFrame.pack()</code> to get the correct overall size.</li> <li>Uses <code>setLocationByPlatform(true)</code> instead of the <a href="https://stackoverflow.com/a/7143398/418556">very splash-screen like</a> <code>setLocationRelativeTo(null)</code>.</li> </ol> <p>Check the code carefully for further tips.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Racing extends JPanel implements KeyListener { private static final long serialVersionUID = -198172151996959655L; //keeps track of player speed double plSpeed = .5; //numbers that represent direction final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, STOP = 5, START = 6; //keeps track of player direction int p1Direction = START; //makes player 1's car Rectangle p1 = new Rectangle ( 100, 25, 30, 30 ); //constructor public Racing() { //define defaults for the JFrame setBackground(Color.BLACK); //makes the screen size setPreferredSize(new Dimension(400,50)); //makes the key listener "wake up" addKeyListener(this); setFocusable(true); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { //refresh screen repaint(); //makes car increase speed a bit if (plSpeed &lt;= 7) { plSpeed += .2; } //lets the car stop if (plSpeed==0) { p1Direction = STOP; } //moves player based on direction if (p1Direction==UP) { p1.y -= (int) plSpeed; } if (p1Direction==DOWN) { p1.y += (int) plSpeed; } if (p1Direction==LEFT) { p1.x -= (int) plSpeed; } if (p1Direction==RIGHT) { p1.x += (int) plSpeed; } if (p1Direction==STOP) { plSpeed = 0; } } }; Timer t = new Timer(75,al); t.start(); } //draws the cars and race track @Override public void paintComponent(Graphics g) { super.paintComponent(g); //draw p1 g.setColor(Color.RED); g.fillRect(p1.x,p1.y,p1.width,p1.height); } //have to input these (so it will compile) public void keyPressed(KeyEvent event) { System.out.println(event); try { //makes car increase speed a bit if (event.getKeyChar()=='w' || event.getKeyChar()=='a' || event.getKeyChar()=='s' || event.getKeyChar()=='d') { plSpeed += .2; //repaint(); } } catch (Exception I) {} } public void keyReleased(KeyEvent event) {} //now, to be able to set the direction public void keyTyped(KeyEvent event) { if (plSpeed &gt; 0) { if (event.getKeyChar()=='a') { if (p1Direction==RIGHT) { p1Brake(); } else { if (p1Direction==LEFT) { } else { p1Direction = LEFT; } } } if (event.getKeyChar()=='s') { if (p1Direction==UP) { p1Brake(); } else { if (p1Direction==DOWN) { } else { p1Direction = DOWN; } } } if (event.getKeyChar()=='d') { if (p1Direction==LEFT) { p1Brake(); } else { if (p1Direction==RIGHT) { } else { p1Direction = RIGHT; } } } if (event.getKeyChar()=='w') { if (p1Direction==DOWN) { p1Brake(); } else { if (p1Direction==UP) { } else { p1Direction = UP; } } } if (event.getKeyChar()=='z') { p1Brake(); } } } public void p1Brake () { try { while (plSpeed != 0) { plSpeed -= .2; } } catch (Exception e) { plSpeed = 0; } } //finally, to start the program public static void main(String[] args) { SwingUtilities.invokeLater( new Runnable() { public void run() { JFrame f = new JFrame("Racing"); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); f.add(new Racing()); f.pack(); f.setLocationByPlatform(true); f.setResizable( false ); f.setVisible( true ); } }); } } </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