简体   繁体   中英

change the direction of the ball as the key is pressed

i have been trying to make a ball change its direction as any key is pressed. if the ball is moving sideways as the key will be pressed the ball will start moving downward and as it touch the bottom it moves back in upward position...i think i have written the right code , i cant find anything wrong in it. so can someone please tell me what is problem in this code ?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ASS2 extends JFrame {

    public static void main(String args[]) {
        ASS2 g = new ASS2();
        g.setLayout(new BorderLayout());
        g.setSize(500, 500);
        MyPanel mp = new MyPanel();
        g.add(mp);
        mp.setSize(500, 500);
        mp.setBackground(Color.black);
//mp.addKeyListener(mp);
        g.setVisible(true);
        g.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
}

class MyPanel extends JPanel implements KeyListener {

    {
        addKeyListener(this);
    }
    int xpos = 20, ypos = 200;
    int xtop = 15, ytop = 15;
    int xtemp = 250, ytemp = 250;
    int xbot = 450, ybot = 400;
    int flag = 1, flag1 = 1;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.white);
        g2d.fill(new Ellipse2D.Double(xpos, ypos, 50, 50));
        if (xpos < xbot && flag == 1) {
            xpos++;
            if (xpos == xbot) {
                flag = 0;
            }
        } else if (xpos > xtop && flag == 0) {
            xpos--;
            if (xpos == xtop) {
                flag = 1;
            }
        }

        try {
            Thread.sleep(05);
        } catch (Exception e) {
        }
        repaint(1000);

    }

    public void keyPressed(KeyEvent ae) {

    }

    public void keyReleased(KeyEvent ae) {
        Object t = ae.getKeyCode();
        if (t.equals(KeyEvent.VK_DOWN)) {
            if (ypos < ybot && flag1 == 1) {
                ypos++;
                if (ypos == ybot) {
                    flag1 = 0;
                }
            } else if (ypos > ytop && flag1 == 0) {
                ypos--;
                if (ypos == ytop) {
                    flag1 = 1;
                }
            }
            repaint();
        } else if (t.equals(KeyEvent.VK_RIGHT)) {
            if (xpos < xbot && flag == 1) {
                xpos++;
                if (xpos == xbot) {
                    flag = 0;
                }
            } else if (xpos > xtop && flag == 0) {
                xpos--;
                if (xpos == xtop) {
                    flag = 0;
                }
            }
            repaint();
        }

    }
}

Welcome to the wonderful world of "why you shouldn't use KeyListener s".

Basically, KeyListener will only raise events when the component that the listener is attached to is focusable AND has focus.

Instead, you should be using Key Bindings which allow you to control the focus level at which they will trigger key events.

  • Do not use Thread.sleep in any method that is called within the EDT, especially paint methods.
  • Do not call repaint(1000); inside any paint method or call any method that might trigger a repaint from within paint methods
  • Do not modify state's within paint methods, paint methods paint, that's all.
  • Use some kind of "update" thread/process which is responsible for updating the game model and requesting an update to the view. java.swing.Timer is good simple choice to start with. See How to use Swing Timers
  • When the Swing Timer becomes to limiting for what you are trying to do and you start exploring the use of Thread s, don't modify any UI components outside the EDT. See Concurrency in Swing for more details
  • Always start your programs from within the context of the EDT, see Initial Threads for more details...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM