两个简单的弹跳球

时间:2015-04-25 05:38:13

标签: java

我是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);
}
}

1 个答案:

答案 0 :(得分:2)

以下是有关的4行:

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

这些方法都没有实现。

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