java写抽小球问题_想用java做个运动小球,球出来了但不会动,不知道哪里有问题...

importjava.awt.*;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;publicclassBall1{publicstaticvoidmain(Stringargs[]){Framew=newFrame();w.setSize(400,6...

import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class Ball1 {

public static void main (String args[]){

Frame w=new Frame();

w.setSize(400, 600);

w.setBackground(Color.black);

MyPanel mp=new MyPanel();

w.add(mp);

Thread t=new Thread();

t.start();

w.show();

w.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

}

class MyPanel extends Panel implements Runnable{

int x=100;

int y=0;

public void paint(Graphics g){

g.setColor(Color.pink);

g.fillOval(x, y, 40, 40);

}

public void run() {

while(true) {

x ++;

y ++;

try {

Thread.sleep(6);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

repaint();

}

}

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 实现多线程小球运动需要以下步骤: 1. 创建小球类 Ball,并实现 Runnable 接口 ```java public class Ball implements Runnable{ private int x; private int y; private int r; private int vx; private int vy; private Color color; private Component component; public Ball(Component component, int x, int y, int r, int vx, int vy, Color color){ this.component = component; this.x = x; this.y = y; this.r = r; this.vx = vx; this.vy = vy; this.color = color; } public void run(){ while (true){ //更新小球位置 x += vx; y += vy; //检查小球是否碰到边界 if (x - r < 0){ vx = -vx; x = r; } else if (x + r > component.getWidth()){ vx = -vx; x = component.getWidth() - r; } if (y - r < 0){ vy = -vy; y = r; } else if (y + r > component.getHeight()){ vy = -vy; y = component.getHeight() - r; } //绘制小球 component.repaint(); try{ Thread.sleep(10); } catch (InterruptedException e){ e.printStackTrace(); } } } //绘制小球 public void draw(Graphics g){ g.setColor(color); g.fillOval(x - r, y - r, 2 * r, 2 * r); } } ``` 2. 在主函数中创建多个小球,并启线程 ```java public static void main(String[] args){ JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); JPanel panel = new JPanel(){ protected void paintComponent(Graphics g){ super.paintComponent(g); for (Ball ball : balls){ ball.draw(g); } } }; frame.add(panel); Ball[] balls = new Ball[10]; for (int i = 0; i < balls.length; i++){ int x = (int)(Math.random() * 300 + 50); int y = (int)(Math.random() * 300 + 50); int r = (int)(Math.random() * 20 + 10); int vx = (int)(Math.random() * 10 - 5); int vy = (int)(Math.random() * 10 - 5); Color color = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)); balls[i] = new Ball(panel, x, y, r, vx, vy, color); new Thread(balls[i]).start(); } } ``` 通过以上步骤,我们就可以实现多个小球在窗口中运动了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值