Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By wsaryada

Thread: if anyone familiar with more advanced graphics please help me get working arrow keys

  1. #1
    auther is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default if anyone familiar with more advanced graphics please help me get working arrow keys

    here's my code:
    public Game() {
    frame.add(panel);
    frame.add(canvas);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    canvas.setBackground(Color.cyan);
    canvas.setColor(Color.black);
    canvas.fillOval(1, 400, 20, 20);
    panel.addKeyListener(
    new KeyAdapter() {
    //i need to know what to compare this too
    if(KeyEvent.VK_LEFT){
    x -= 10;
    repaint();}
    //i need to know what to compare this too
    if(KeyEvent.VK_RIGHT){
    x += 10;
    repaint();}});}

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: if anyone familiar with more advanced graphics please help me get working arrow k

    The KeyAdapter abstract class allows you to implement these methods:

    Java Code:
    @Override
    public void keyTyped(KeyEvent e) {
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
    }
    From the KeyEvent type argument you can get what key was pressed. For example if (KeyEvent.VK_LEFT == e.getKeyCode()) then do something.
    Last edited by wsaryada; 04-03-2013 at 06:41 AM.
    auther likes this.

  3. #3
    auther is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: if anyone familiar with more advanced graphics please help me get working arrow k

    oh thanks

  4. #4
    auther is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: if anyone familiar with more advanced graphics please help me get working arrow k

    I want to set up a program so when I make the sprite move passed the screen it changes color and sets the sprite at the left side of the screen again here’s my code:
    private void checkMovement() {
    if (x >= 800) {
    x = 0;
    canvas.setBackground(Color.green);
    } else if (x <= 0) {
    x = 0;
    }
    }
    Last edited by auther; 04-03-2013 at 08:53 PM.

Similar Threads

  1. Etch A Sketch Like Program Using Arrow Keys
    By jmanes92 in forum AWT / Swing
    Replies: 6
    Last Post: 04-25-2012, 12:35 AM
  2. Replies: 4
    Last Post: 12-03-2011, 05:38 PM
  3. how to move a rectangle with arrow keys,
    By jmu2101 in forum AWT / Swing
    Replies: 3
    Last Post: 09-27-2011, 02:13 AM
  4. Arrow Keys?
    By Alerhau in forum New To Java
    Replies: 10
    Last Post: 06-17-2011, 08:45 PM
  5. Moving a ball with Arrow Keys
    By kekcklemen in forum Java Applets
    Replies: 5
    Last Post: 02-25-2011, 11:15 PM

Posting Permissions

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