Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help with Arrow Keys

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help with Arrow Keys

    I don't know what is going on - I cannot get my arrow keys to affect the program to change the speed of the car.


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    @SuppressWarnings("serial")
    public class RacingCar extends JFrame {
     
    public RacingCar() {
    add(new CarImage());
    }
     
    public static void main(String[] args) {
    JFrame frame = new RacingCar();
    frame.setTitle("Racing Car");
    frame.setSize(300, 150);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true); 
     
    }
     
    class CarImage extends JPanel {
    protected int x = 0;
    protected int y = 150;
    protected int z = 300;
    protected int c = 0;
    protected int speed = 100;
     
    public CarImage() {
    Timer timer1 = new Timer(speed , new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    x += 10;
    c ++;
    repaint();
    }
    });
    timer1.start();
     
    }
     
    public void actionPerformed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        speed -= 50;
     
    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        speed += 50;	
    }	
    }
     
     
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
     
     
    // x = 0;
    y = getHeight();
    z = getWidth();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, z, y);
    Polygon polygon = new Polygon();
    polygon.addPoint(x + 10, y - 21);
    polygon.addPoint(x + 20, y - 31);
    polygon.addPoint(x + 30, y - 31);
    polygon.addPoint(x + 40, y - 21);
     
     
    if (x < z - 40) {
    g.setColor(Color.BLACK);
    g.fillOval(x + 10, y - 11, 10, 10);
    g.fillOval(x + 30, y - 11, 10, 10);
    g.setColor(Color.BLUE);
    g.fillRect(x, y - 21, 50, 10);
    g.setColor(Color.GRAY);
    g.fillPolygon(polygon);
    g.setColor(Color.RED);
    }
    else 
    x = 0;
     
    }
     
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Please help with Arrow Keys

    Duplicate of http://www.javaprogrammingforums.com...rrow-keys.html
    Thread locked

Similar Threads

  1. Code will not work to change speed with the up and down arrow keys
    By rtucker1023 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2013, 04:09 PM
  2. blue arrow in junit
    By walid in forum Threads
    Replies: 9
    Last Post: April 4th, 2013, 01:40 PM
  3. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  4. [SOLVED] Making an image move with arrow keys
    By Clex19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 6th, 2012, 09:45 AM
  5. [SOLVED] Arrow keys to navigate a maze in a console program
    By B25Mitch in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 31st, 2012, 04:58 PM