Results 1 to 2 of 2
  1. #1
    shazlin is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default problem with drawline using arrow key

    well, it works fine when i go up and down but when i using left or right arrow key. the all line shift to left or right. can anyone could help me with this?


    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;

    public class KeyEvents extends JPanel implements MouseMotionListener {
    Point p;
    int y=100, x=100;
    int a = x, b = y;
    Graphics g;
    public KeyEvents () {
    addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_UP: b-=10;
    break;
    case KeyEvent.VK_DOWN: b+=10;
    break;
    case KeyEvent.VK_LEFT: a-=10;

    break;
    case KeyEvent.VK_RIGHT: a+=10;
    }
    repaint();
    }
    });
    this.addMouseMotionListener(this);

    }
    public void mouseMoved(MouseEvent e) {
    }
    public void mouseDragged(MouseEvent e){

    }
    public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawLine(x, y, a, b);
    }
    }

    Thank You

  2. #2
    shazlin is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Help in bow and arrow game
    By sahildave1991 in forum AWT / Swing
    Replies: 6
    Last Post: 10-18-2010, 02:34 PM
  2. hove i create arrow?
    By sunolinu in forum Java 2D
    Replies: 8
    Last Post: 02-20-2010, 01:20 AM
  3. drawing ellipse by drawline method?
    By hopey in forum Java 2D
    Replies: 8
    Last Post: 04-18-2009, 11:52 PM
  4. Arrow Button Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM
  5. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •