- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到线程问题。我正在尝试重新创建乒乓球,其中有 2 个玩家的 2 个 Action 监听器。 2 个桨以我希望它们移动的速度移动。但是,“球”(上面涂有椭圆形的矩形对象)移动得太快了。我已经尝试使用 Ball 类中的第二个线程来减慢它的速度,但这似乎不起作用。任何帮助,将不胜感激。这是我的代码:
public class BattleBallz extends JFrame implements Runnable {
AL keyListen1 = new AL();
AL2 keyListen2 = new AL2();
Image dbi;
Graphics dbg;
int x1,x2;
int ballX, ballY, direction;
Ball b1;
public BattleBallz(){
setTitle("Battle Ballz");
setSize(350, 400);
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
addKeyListener(keyListen1);
addKeyListener(keyListen2);
x1 = 150;
x2 = 150;
int direction = 0 + (int)(Math.random() * ((2 - 0) + 1));
b1 = new Ball(direction);
Thread b = new Thread(b1);
b.start();
}
public void paint(Graphics g){
dbi = createImage(getWidth(), getHeight());
dbg = dbi.getGraphics();
paintComponent(dbg);
g.drawImage(dbi, 0, 0, this);
}
public void paintComponent(Graphics g){
Rectangle p1 = new Rectangle(x1,375,60,10);
g.setColor(Color.blue);
g.fillRect(x1, 375, 60, 10);
Rectangle p2 = new Rectangle(x2,45,60,10);
g.setColor(Color.red);
g.fillRect(x2,45,60,10);
b1.paintComponent(g, p1, p2);
repaint();
}
@Override
public void run() {
try {
while(true){
move();
Thread.sleep(5);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void move(){
x1 += keyListen1.getXdirection();
x2 += keyListen2.getXdirection();
if (x1<=0){
x1 =0;
}
if (x1>=305){
x1 = 305;
}
if (x2<=0){
x2=0;
}
if (x2>=305){
x2=305;
}
}
}
public class Ball extends JPanel implements Runnable {
boolean up,down,right,left;
int x = 150, y = 150 ;
public Ball(int direction){
if(direction ==1){
down = true;
}
if (direction ==0){
up = true;
}
}
public void paintComponent(Graphics g, Rectangle p1, Rectangle p2){
super.paintComponents(g);
if(down){
y++;
Rectangle ball = new Rectangle(x,y,49,49);
g.fillOval(x, y, 50, 50);
if (y>=385 || ball.intersects(p1)){
down = false;
up = true;
}
}
if (up){
y--;
Rectangle ball = new Rectangle(x,y,49,49);
g.fillOval(x, y, 50, 50);
if (y<=10 || ball.intersects(p2)){
up = false;
down = true;
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true){
Thread.sleep(30);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
最佳答案
使用 Swing Timer而不是普通的 Thread
,您遇到了 Concurency in Swing 的问题,代码发布到阻塞Event Dispatch Thread
,被Thread.sleep
锁定,无限阻塞
使用 paintComponent
但放在那里 JPanel
,第一。里面的代码行应该是super.paintComponent
,否则paint会累积,覆盖JPanel
getPreferredSize
使用 KeyBindings而不是 KeyListener
关于java - 需要减慢矩形移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599972/
我正在使用 UISnapBehavior,但它的捕捉速度太快了,我不喜欢。有没有办法让它慢下来?或者换句话说:有没有办法用它应该捕捉的点来调整物体的弹性? 最佳答案 我能够通过将 View 附加到 U
我想减慢 SWTBot 的执行速度。 我已经找到了这个 wiki: https://wiki.eclipse.org/SWTBot/FAQ#Can_I_slow_down_the_execution_
我的应用程序中有一个计时错误,只有在我使用 valgrind 时才会发生,因为 valgrind 会大大减慢进程的速度。 (它实际上是一个我无法本地化的 boost::weak_ptr-excepti
问题 我正在创建一个涉及躲避射弹的游戏。玩家控制着一艘船的图像,我不希望船完全一起移动,因为这看起来非常不现实。 问题 有没有办法控制图像移动的速度,如何减慢图像的移动速度? 代码 var game
我在我的 iOS 应用程序中使用了 NSTimer,但由于 SetNeedsDisplay,我没有得到我想要的结果。 我做了一些研究并找到了 CADisplayLink,它为我提供了我想要的动画结果。
我目前正在开发一个项目,当按下按钮时,该项目会将圆从一个空间移动到另一个空间。我的设计如下:当按下按钮时,它会在 for 循环中从 0 到 10 增加圆的坐标。 问题是,我想要的 for 循环运动没有
我想缓慢地制作一个三色渐变动画。 我有一个自定义UIView,如下所示: class MyView: UIView, CAAnimationDelegate { lazy var gradient
当 RAM 达到 x 内存量或调用 didReceiveMemoryWarning() 时,是否有办法减慢处理器速度? func didReceiveMemoryWarning() { sup
有没有办法减慢行插入/删除动画的速度? 在我的特殊情况下,我通过在我的单元格下方添加/删除行来扩展/折叠单元格,我想稍微放慢动画速度。 最佳答案 我正在使用以下技巧在我的项目中以动画方式插入/删除表格
我的 Logo 和页脚中有 scroll-top 属性,但我离页面顶部越远,它向上滚动的速度就越快!所以当我从页面底部滚动到顶部时,它就像火箭一样!我将如何放慢速度?我找不到足够具体的答案 可以看看l
我想放慢由我的 UIDynamicAnimator 生成的动画,以便我可以微调我的 UIDynamicBehaviors。 在 ios 模拟器中,调试菜单下有一个菜单选项,标签为“在最前面的应用程序中
在 OS X 上,可以按住 Shift 键使动画变慢。有什么方法可以通过远程调试器或 Instruments 将其应用于 iOS 吗? (或者,我可以在 QuickTime 中录制并逐帧回放,但我宁愿
我想在 .opacity CSS 属性中减慢动画时间。就像,我希望它延迟 0.2 毫秒或类似的东西。 为了获得更好的想法,将鼠标悬停在我网站上的精选帖子上时会添加不透明度:http://www.the
我希望我的 UIPageViewController 在用户的手指离开屏幕时缓慢滚动到下一页。比默认情况下慢。如果可能的话,对其减速曲线等进行更多控制。 我不想使用 SCPageViewControl
我发现了这个 javascript 自动滚动函数,并通过将其粘贴到 WordPress 站点的头文件中来使其工作。但是,我想减慢滚动速度,以便它不会立即捕捉到页面底部。 我是 javascript 的
我正在使用 UIScrollView 以编程方式为某些内容设置动画。 但是,我需要减慢 View 的滚动速度。 这是我用于滚动的代码: self.scrollView.setContentOffset
我一直在使用 jQuery 滚动来增强我的视差滚动页面。具体来说就是这个。 JQuery Scroll to Next Section 我对 jQuery 完全陌生(过去只使用过一些相当基本的 Jav
如何减慢 Windows 进程? 我知道我需要 Hook QueryPerformanceCounter 但接下来我需要做什么? 需要 Delphi 或 C++ 方面的帮助 最佳答案 我不确定我是否理
我想在我这边控制下载量/速度——在服务器端也一样(礼貌一点)。...不是“我自己的下载管理器”。 让我们想象一下:我允许我的儿子每天从 utube 下载最多 500Mb,但他仍然启动了一个 sessi
在我的网站上,我有多个 href's,我需要在点击它们和加载它们之间添加延迟。由于有数百个 hrefs,我不能为每个单独的 js 函数。 我研究过的两种方法是,将 href 的内容作为变量传递给 ja
我是一名优秀的程序员,十分优秀!