每当我开始运行程序时如何设置随机位置?

编程入门 行业动态 更新时间:2024-10-27 00:24:24
本文介绍了每当我开始运行程序时如何设置随机位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个沿随机方向移动的图像。我的问题是,每当我开始运行该程序时,图像始终出现在左上角并向对角线方向移动,碰到墙后,图像就开始向随机方向移动。每次执行或运行程序时,如何使图像显示在随机位置?任何帮助将不胜感激...

I have an image which moves in a random direction. My problem is everytime I start running the program, the image always appear at the upper-left corner and moves a diagonal direction and after hitting the wall, it starts to move random direction. How can I make the image appear in a random position everytime I execute or run the program? Any help would be much appreciated...

这是代码:

public class Ball extends JPanel implements Runnable { private Image ball; private Thread animator; private int x; private int y; private final int DELAY = 50; private int xVelocity = 1; private int yVelocity = 1; private static final int RIGHT_WALL = 400; private static final int LEFT_WALL = 1; private static final int DOWN_WALL = 400; private static final int UP_WALL = 1; private boolean showImage; public Ball() { randomPosition(); setRandomDirection(); setBackground(Color.BLACK); setDoubleBuffered(true); ImageIcon ii = new ImageIcon(this.getClass().getResource("ball.gif")); ball = ii.getImage(); x = y = 10; } public void addNotify() { super.addNotify(); animator = new Thread(this); animator.start(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(ball, x, y, this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void move() { x += xVelocity; y += yVelocity; if (x >= RIGHT_WALL) { x = RIGHT_WALL; randomDirection(); } if (y <= UP_WALL) { y = UP_WALL; randomDirection(); } if (x <= LEFT_WALL) { x = LEFT_WALL; randomDirection(); } if (y >= DOWN_WALL) { y = DOWN_WALL; randomDirection(); } } private void randomDirection() { double speed = 2.0; double direction = Math.random() * 2 * Math.PI; xVelocity = (int) (speed * Math.cos(direction)); yVelocity = (int) (speed * Math.sin(direction)); } private void randomPosition() { x = LEFT_WALL + (int) (Math.random() * (RIGHT_WALL - LEFT_WALL)); y = UP_WALL + (int) (Math.random() * (DOWN_WALL - UP_WALL)); } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); while (true) { cycle(); repaint(); timeDiff = System.currentTimeMillis() - beforeTime; sleep = DELAY - timeDiff; if (sleep > 2) { sleep = 1; } try { Thread.sleep(sleep); } catch (InterruptedException e) { System.out.println("interrupted"); } beforeTime = System.currentTimeMillis(); } } }

推荐答案

Random r = new Random() public Ball() { randomPosition(); setRandomDirection(); setBackground(Color.BLACK); setDoubleBuffered(true); ImageIcon ii = new ImageIcon(this.getClass().getResource("chicken.gif")); ball = ii.getImage(); this.x = r.nextInt(); this.y = r.nextInt(); }

更多推荐

每当我开始运行程序时如何设置随机位置?

本文发布于:2023-11-27 03:23:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每当我   如何设置   位置   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!