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: Code will not work to change speed with the up and down arrow keys

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

    Default Code will not work to change speed with the up and down arrow keys

    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Code will not work to change speed with the up and down arrow keys

    To get key events you need to use either a key listener or key bindings.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. 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
  2. [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
  3. Change colour of JComboBox arrow button
    By IanSawyer in forum AWT / Java Swing
    Replies: 0
    Last Post: April 21st, 2012, 04:05 PM
  4. [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
  5. can someone show me how to change speed of an java engine.
    By codenoob in forum Java Theory & Questions
    Replies: 19
    Last Post: December 14th, 2011, 12:26 PM