trying to implement solution and getting cant resolve symbol error
Hey guys trying to implement a solution that I received for my bouncing ball program. Was trying to get the ball to start in the center of the applet and start and stop moving with mouse clicks instead of buttons.
Getting an error in BBPanel saying cant resolve symbol in the following line:
if (m_bb.getTimer().isRunning())
cant figure out how to solve the problem--can anyone shed some light on this thanks
here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/////////////////////////////////////////////////////////////////// BBPanel
class BBPanel extends JPanel implements MouseListener
{
BallInBox m_bb;// The bouncing ball panel
//========================================================== constructor
/** Creates a panel with the controls and bouncing ball display. */
private JButton startButton;
private JButton stopButton;
BBPanel()
{
//... Create components
m_bb = new BallInBox();
startButton = new JButton("Start");
stopButton = new JButton("Stop");
//... Add Listeners
startButton.addActionListener(new StartAction());
stopButton.addActionListener(new StopAction());
//... Layout inner panel with two buttons horizontally
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
//... Layout outer panel with button panel above bouncing ball
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.NORTH);
this.add(m_bb, BorderLayout.CENTER);
addMouseListener(this);
}//end constructor
public void mouseClicked(MouseEvent e)
{
if (m_bb.getTimer().isRunning())
{
stopButton.doClick();
}
else
{
startButton.doClick();
}
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
////////////////////////////////////// inner listener class StartAction
class StartAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(true);
m_bb.setFirstTime(false);
}
}
//////////////////////////////////////// inner listener class StopAction
class StopAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(false);
}
}
}//endclass BBPanel
import java.awt.*;
public class Ball
{
//... Constants
final static int DIAMETER = 21;
//... Instance variables
private int m_x;// x and y coordinates upper left
private int m_y;
private int m_velocityX;// Pixels to move each time move() is called.
private int m_velocityY;
private int m_rightBound; // Maximum permissible x, y values.
private int m_bottomBound;
private Color color; //ball color
//======================================================== constructor
public Ball(int x, int y, int velocityX, int velocityY)
{
m_x = x;
m_y = y;
m_velocityX = velocityX;
m_velocityY = velocityY;
color=Color.BLACK;
}
//======================================================== setBounds
public void setBounds(int width, int height)
{
m_rightBound = width - DIAMETER;
m_bottomBound = height - DIAMETER;
}
//========================================================= setColor
public void setColor(Color color)
{
this.color = color;
}
//==========================================================setRandomColor
public void setRandomColor()
{
// New random color
int r = (int)(255*Math.random());
int g = (int)(255*Math.random());
int b = (int)(255*Math.random());
setColor(new Color(r,g,b));
}
//============================================================== move
public void move()
{
//... Move the ball at the give velocity.
m_x += m_velocityX;
m_y += m_velocityY;
//... Bounce the ball off the walls if necessary.
if (m_x < 0)
{// If at or beyond left side
m_x = 0;// Place against edge and
m_velocityX = -m_velocityX; // reverse direction.
setRandomColor(); // Change color
}
else if (m_x > m_rightBound)
{ // If at or beyond right side
m_x = m_rightBound;// Place against right edge.
m_velocityX = -m_velocityX; // Reverse direction.
setRandomColor(); // Change color
}
if (m_y < 0)
{// if we're at top
m_y= 0;
m_velocityY = -m_velocityY;
setRandomColor(); // Change color
}
else if (m_y > m_bottomBound)
{ // if we're at bottom
m_y= m_bottomBound;
m_velocityY = -m_velocityY;
setRandomColor(); // Change color
}
}
//============================================================== draw
public void draw(Graphics g, int x, int y, boolean first_time)
{
g.setColor(color);
if (first_time)
{
g.fillOval(x, y, DIAMETER, DIAMETER);
m_x = x;
m_y = y;
}
else
{
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
// g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
//============================================= getDiameter, getX, getY
public int getDiameter() { return DIAMETER;}
public int getX(){ return m_x;}
public int getY(){ return m_y;}
//======================================================== setPosition
public void setPosition(int x, int y)
{
m_x = x;
m_y = y;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class BallInBox extends JPanel
{
//============================================== fields
//... Instance variables representing the ball.
private Ball m_ball = new Ball(0, 0, 2, 3);
private boolean first_time = true;
//... Instance variables for the animiation
private intm_interval = 35; // Milliseconds between updates.
Timer m_timer;// Timer fires to anmimate one step.
//========================================================== constructor
/** Set panel size and creates timer. */
public BallInBox()
{
setPreferredSize(new Dimension(200, 80));
setBorder(BorderFactory.createLineBorder(Color.BLACK));
m_timer = new Timer(m_interval, new TimerAction());
}
public void setFirstTime(boolean first_time)
{
this.first_time = first_time;
}
//========================================================= setAnimation
/** Turn animation on or off.
*@param turnOnOff Specifies state of animation.
*/
public void setAnimation(boolean turnOnOff)
{
if (turnOnOff)
{
m_timer.start(); // start animation by starting the timer.
}
else
{
m_timer.stop();// stop timer
}
}
//======================================================= paintComponent
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Paint background, border
m_ball.draw(g,getWidth()/2 - Ball.DIAMETER/2, getHeight()/2 -
Ball.DIAMETER/2, first_time);// Draw the ball.
}
//////////////////////////////////// inner listener class ActionListener
class TimerAction implements ActionListener
{
//================================================== actionPerformed
/** ActionListener of the timer. Each time this is called,
* the ball's position is updated, creating the appearance of
* movement.
*@param e This ActionEvent parameter is unused.
*/
public void actionPerformed(ActionEvent e) {
m_ball.setBounds(getWidth(), getHeight());
m_ball.move(); // Move the ball.
repaint();// Repaint indirectly calls paintComponent.
}
}
}//endclass
import javax.swing.*;
/////////////////////////////////////////////////////////////// BBDemo
public class BBDemo extends JApplet {
//============================================== applet constructor
public BBDemo() {
add(new BBPanel());
}
//============================================================ main
public static void main(String[] args) {
JFrame win = new JFrame("Bouncing Ball Demo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setContentPane(new BBPanel());
win.pack();
win.setVisible(true);
System.out.println(win.getSize());
}
}//endclass BBDemo
thanks for the help--some copies of this code and how i progressed are in some of my other threads if you click on my name
any help is apprecaited
thanks rich

