- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的第一个图形 Java 程序,我想做的是重新创建一个简单的经典程序,其中我有多个球在 JFrame
中弹跳。窗口。
到目前为止,我已经成功地使用 run()
中的代码让一个球弹来弹去。方法。这适用于我创建的一个球对象,但现在我想要有很多球,所以我尝试在我的 Ball
中创建一种方法类将使我创建的每个球对象在我的“球世界”中独立弹跳。
现在我关心的是它们从墙上反弹,而不是彼此(我稍后会弄清楚)。
问题:在我的 ballMove(int, int, int, int)
中方法我有四种int
参数,其中前两个参数是 width
和height
window
的,最后两个参数是Xspeed
,和Yspeed
。当我浏览我的if statements
时它将适度设置 x
和y speed
当球击中右侧或底部墙壁时,参数为负值,但当 run()
方法执行 ballMove(int, int, int, int)
再次用这种方法,他们又恢复到积极状态,球从 window 上消失了。我尝试过使用一堆 getter
和setter
我的球类中的方法。我已经在我的 ballMove(int, int, int, int)
中尝试过临时变量方法。我尝试过的任何方法都不起作用。
问题:通过使用我的 Ball
类方法,如何防止我的参数 Xspeed
和Yspeed
当球与墙壁碰撞时,将我的实例速度变量重新初始化为正值?
因为我是图形编程的新手,所以任何有用的建议将不胜感激。
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
public class Main extends JFrame implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int width = 800;
private int height= 600;
private int ballRadius = 50;
private Random rand = new Random();
//Create and initialize a ball object
public Ball ball = new Ball(Color.BLUE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball2 = new Ball(Color.RED, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball3 = new Ball(Color.GREEN, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball4 = new Ball(Color.ORANGE, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//public Ball ball5 = new Ball(Color.YELLOW, ballRadius, ballRadius, rand.nextInt(500), rand.nextInt(500));
//constructor
public Main(){
setSize(width, height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//Paint the ball(s)
public void paint(Graphics g){
super.paint(g);
g.setColor(ball.getColor());
g.fillOval(ball.getBallX(), ball.getBallY(), ball.getWidth(), ball.getHeight());
//g.setColor(ball2.getColor());
//g.fillOval(ball2.getBallX(), ball2.getBallY(), ball2.getWidth(), ball2.getHeight());
//g.setColor(ball3.getColor());
//g.fillOval(ball3.getBallX(), ball3.getBallY(), ball3.getWidth(), ball3.getHeight());
//g.setColor(ball4.getColor());
//g.fillOval(ball4.getBallX(), ball4.getBallY(), ball4.getWidth(), ball4.getHeight());
//g.setColor(ball5.getColor());
//g.fillOval(ball5.getBallX(), ball5.getBallY(), ball5.getWidth(), ball5.getHeight());
}
//Run the program
public static void main(String [] args){
Main main = new Main();
main.setVisible(true);
main.run();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ball.ballMove(width, height, 20, 5);
repaint();
//ball2.ballMove(width, height, 15, 3);
//repaint();
//ball3.ballMove(width, height, 3, 20);
//repaint();
//ball4.ballMove(width, height, 10, 10);
//repaint();
//ball5.ballMove(width, height, 10, 20);
//repaint();
}
}
}
这是我的Ball
类
import java.awt.Color;
import javax.swing.JFrame;
public class Ball extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private int width, height, ball_X, ball_Y;
private int Xspeed;
private int Yspeed;
private Color color;
public Ball(Color color, int width, int height, int ball_X, int ball_Y){
this.width = width;
this.height = height;
this.color = color;
this.ball_X = ball_X;
this.ball_Y = ball_Y;
}
public Color getColor(){
return this.color;
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public int getBallX(){
return this.ball_X;
}
public int getBallY(){
return this.ball_Y;
}
public void setSpeedX(int x){
this.Xspeed = x;
}
public void setSpeedY(int x){
this.Yspeed = x;
}
public int getSpeedX(){
return this.Xspeed;
}
public int getSpeedY(){
return this.Yspeed;
}
public void setBallX(int x){
this.ball_X = x;
}
public void setBallY(int y){
this.ball_Y = y;
}
public void ballMove(int X, int Y, int xSpeed, int ySpeed){
//initialize Xspeed and Yspeed with the parameters of the function
this.setSpeedX(xSpeed);
this.setSpeedY(ySpeed);
//Moves the balls by adding the set speed to the position of the balls each time thread is executed
this.setBallX(this.getBallX() + this.getSpeedX());
this.setBallY(this.getBallY() + this.getSpeedY());
//When the balls hit the walls they are suppose to bounce back until they hit another wall.
if(this.getBallX() + 50 >= X){
this.setSpeedX(-xSpeed);
}
if(this.getBallY() + 50 >= Y){
this.setSpeedY(-ySpeed);
}
if(this.getBallX() + 25 <= 0){
this.setBallX(xSpeed);
}
if(this.getBallY() + 25 <= 0){
this.setSpeedY(ySpeed);
}
}
}
最佳答案
你的问题就在那里:
ball.ballMove(宽度, 高度, 20, 5);
由于这是一个循环,因此每次调用它时,您都会使其朝同一方向移动。如果球撞到墙壁,您将在球移动结束时反转速度,但这并不重要,因为下次您调用它时,球仍然会向 +20、+5 移动。
我的建议是在创建球实例时添加速度参数,并让 ballmove 更新自己的速度。
ball.ballMove(宽度,高度);
其他建议:在实际移动球之前进行碰撞检查。这样你就可以在实际移动它之前确保你没有走错方向。
现在,另一个问题是在您的 main 中,您正在调用可运行对象的 run()
方法。 Run 在当前线程上执行。你需要做这样的事情:
线程 t = new Thread(main);
t.start();
独立于 JFrame 进行绘图和计算。
关于Java图形 "Bouncing Ball"程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23294604/
有谁知道使用 JQuery 使 Google map 标记“弹跳”的方法 Bounce包? 最佳答案 如果您希望标记在完成拖动后弹起,您可以直接从 API 执行此操作。刚刚设置bouncy为真。您甚至
我在 Azure 应用服务上看到了一些有趣的行为,希望有人能够对此发表评论。 复制步骤(所有 Azure 步骤都可以在门户中完成): 在应用服务中创建一个新的 Web 应用(标准定价级别,单实例即可)
我有一个像 CSS 中这样的“通知框”; .NotificationsBox { text-align:center; position:absolute; height:17px; widt
我还不擅长 Android 动画,但我需要在我的新项目中实现一个弹跳动画。以下是我用来完成它的动画文件。 此动画将使要设置动画的对象向下弹跳。我该怎么做才能让它向上反弹? (
我有 3 个 MySQL 表。具有 company_id 和 company_name 的 公司具有 product_id 和 company_id 的 products 名称,包含product_i
我在尝试对相对定位的 div 内的绝对位置 div 使用 jQuery 效果“弹跳”时遇到问题。 #Bounce div 位于容器 div 的上方,当收到特定消息时,它应该在其顶部反弹。但最终发生的是
很多网站都有这样的东西,如果你一直向上滚动,你会得到这种“弹跳”效果,就好像你的页面从浏览器窗口的顶部弹回,显示窗口顶部的空白。 同样的情况,如果你向下滚动,你会得到完全相同的“弹跳”效果。 如果您的
我最近开始用java编写游戏。我有 C#/XNA 背景,并且已经拥有游戏开发经验。但是我的java游戏遇到了问题。我有一个弹跳脚本,可以使游戏中的“粒子”在与固体表面(例如地面)碰撞时弹起。这是管理弹
我正在尝试获得我在网站上看到的这种滚动效果 http://www.unheap.com ,如果您滚动到底部或右侧,您会发现您无法滚动过去。包括这个网站在内的大多数网站都允许您稍微滚动过去,但遇到很多阻
任何人都可以指导我正确的方向来弄清楚如何像这样对 UIImageView 进行动画处理: 在应用程序初始加载时,UIImageView 将从屏幕顶部进入,然后(向上和向下)弹入底部几次,然后稳定在底部
我创建了一个简单的Ball 原型(prototype),其中包括一个draw() 和一个move() 函数。球应该在地板、墙壁和天花板上弹跳。然而,出于某种原因,它并没有停止弹跳,尽管速度 (vy)
我对JS了解不多,但我必须用它来做作业。现在我有一个球从屏幕的一侧弹到另一侧。每次弹跳,屏幕和球的颜色都会改变。但我也希望每次弹跳时的速度略有增加(或者每次弹跳时的随机速度,如果这样更容易的话)。这是
这是我的第一个图形 Java 程序,我想做的是重新创建一个简单的经典程序,其中我有多个球在 JFrame 中弹跳。窗口。 到目前为止,我已经成功地使用 run() 中的代码让一个球弹来弹去。方法。这适
有没有办法在使用 BounceInterpolator 时降低反弹效果的幅度? Android 中的动画?默认情况下,它会产生比我想要的更多的反弹效果。 最佳答案 不幸的是,Android 的 Bou
我有一个带有“全屏”项目的自定义画廊,我已经覆盖了画廊的 onFling() 方法。在这个被重写的函数中,我检查用户是否向左或向右“ throw ”,并相应地采取行动 onKeyDown(KeyEve
例如在iOS 7/8的锁屏上,如果点击而不是向右拖动,会有轻微的弹跳效果。我如何检测点击手势(而不是将其与拖动混淆)并重新创建类似的微妙“弹跳”效果?谁能分享一个代码示例 (swift/obj-c)?
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我一直在这里、Google 等上寻找答案,但似乎无法完全确定这个问题。 我有一个 ID 为 #pin01 的图像。这是 map 上的一个图钉,我在 div 中将其向下动画化,落在 map 图像上(想想
Apple 的 iPhone 应用程序(例如 Music 和 Contants)使用 UITableView 中的搜索栏。当您向下滚动以使搜索栏向下移动时, ScrollView 内容上方的空白区域具
我如何将 jQuery 的 UI Bounce 功能添加到此脚本中?该脚本当前将进度条滑出到设定位置。我希望当它到达该位置时,它会来回弹跳几次,然后休息。 我尝试了一些以前的堆栈溢出答案,但没有一个起
我是一名优秀的程序员,十分优秀!