Newbie10
Newbie10

Reputation: 99

Two Simple Bouncing Ball

I'm new to Java and now I'm working on a graphics and I'm doing a simple bouncing ball. at first I created a one bouncing ball and it worked then I added one more ball but an error occurred. "error: cannot find symbol" it occurred on line 33,43,55 and 59. can you help me pls? it seems that I have called all the variables needed.

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

Upvotes: 0

Views: 195

Answers (1)

Anubian Noob
Anubian Noob

Reputation: 13596

Here are the 4 lines in question:

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

None of these methods are implemented.

What you probably want are frame.getWidth() and frame.getHeight().

Upvotes: 2

Related Questions