gpt4 book ai didi

java - 两个简单的弹跳球

转载 作者:行者123 更新时间:2023-12-02 04:43:55 25 4
gpt4 key购买 nike

我是 Java 新手,现在我正在处理图形,并且正在做一个简单的弹跳球。起初,我创建了一个弹跳球,它起作用了,然后我又添加了一个球,但发生了错误。 “错误:找不到符号”发生在第 33、43、55 和 59 行。您能帮我吗?看来我已经调用了所有需要的变量。

import javax.swing.*;
import java.awt.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.Timer;
public class BouncingBall extends JPanel
{
//1st ball
int x1 = 250, y1 = 100;
int width1 = 50, height1 = 50;
int xpos1 = 0, ypos1 = 0;
//2nd ball
int x2 = 20, y2 = 20;
int width2 = 100, height2 = 100;
int xpos2 = 0, ypos2 = 0;
java.util.Timer timer;
static JFrame frame;
public BouncingBall()
{
frame = new JFrame("Bouncing Ball");
frame.setSize(400,400);
frame.setVisible(true);
setForeground(Color.red);
timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
{
if(x1 < 0)
xpos1 = 1;
if(x1 >= getWidth1() - 45)
xpos1 = -1;
if(y1 < 0)
ypos1 = 1;
if(y1 >= getHeight1() - 45)
ypos1 = - 1;
x1 += xpos1;
y1 += ypos1;
repaint();
}
{
if(x2 < 0)
xpos2 = 1;
if(x2 >= getWidth2() - 45)
xpos2 = -1;
if(y2 < 0)
ypos2 = 1;
if(y2 >= getHeight2() - 45)
ypos2 = - 1;
x2 += xpos2;
y2 += ypos2;
repaint();
}
}
}, 0, 5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2D.fillOval(x1,y1,width1,height1);
g2D.fillOval(x2,y2,width2,height2);
}
public static void main(String args[])
{
BouncingBall ball = new BouncingBall();
frame.add(ball);
}
}

最佳答案

以下是有问题的 4 行:

if(x1 >= getWidth1() - 45)
if(y1 >= getHeight1() - 45)
if(x2 >= getWidth2() - 45)
if(y2 >= getHeight2() - 45)

这些方法均未实现。

您可能想要的是 frame.getWidth()frame.getHeight()

关于java - 两个简单的弹跳球,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29861448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com