Java小球移动,下落碰到边缘向上返回。
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassQiuextendsJFra...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Qiu extends JFrame{
Xiao mb=null;
public static void main(String[] args){
Qiu lx=new Qiu();
}
public Qiu(){
mb=new Xiao();
this.add(mb);
this.addKeyListener(mb);
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Xiao extends JPanel implements KeyListener{
int x=250, y=250;
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.fillOval(x,y,20,20);
}
public void keyTyped(KeyEvent e){//当键盘按下的时候 会调用
}
public void keyPressed(KeyEvent e) {
else if(e.getKeyCode()==KeyEvent.VK_SPACE){
Thread t = new Thread() {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
y+=10;
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
t.start();
}
this.repaint();
}
public void keyReleased(KeyEvent e){ //按键抬起
}
}
现在是按下空格自动下落。
要把它变成按下空格下落,碰到壁时 向上走。返回。
求改下! 展开
import javax.swing.*;
import java.awt.event.*;
public class Qiu extends JFrame{
Xiao mb=null;
public static void main(String[] args){
Qiu lx=new Qiu();
}
public Qiu(){
mb=new Xiao();
this.add(mb);
this.addKeyListener(mb);
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class Xiao extends JPanel implements KeyListener{
int x=250, y=250;
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.fillOval(x,y,20,20);
}
public void keyTyped(KeyEvent e){//当键盘按下的时候 会调用
}
public void keyPressed(KeyEvent e) {
else if(e.getKeyCode()==KeyEvent.VK_SPACE){
Thread t = new Thread() {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
y+=10;
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
t.start();
}
this.repaint();
}
public void keyReleased(KeyEvent e){ //按键抬起
}
}
现在是按下空格自动下落。
要把它变成按下空格下落,碰到壁时 向上走。返回。
求改下! 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询