Skip to main content

Help compiling code

No replies
happygirl1184
Offline
Joined: 2011-11-28
Points: 0

Hello! I have a (probably dumb) question. I hope it's simple and someone can help me! I have code I've written and I can't figure out how to compile it and run it. I had a friend look over my code and he said it runs fine for him. Can someone tell me exactly what to type into a command prompt to get this to run? It has two classes in it, that's my problem. WHen i type in javac Ball.java and then java Ball it gives me an error. same with when i tried javac BlueBall.java java BlueBall. thank you so much!!

//Write a program that adds a new ball each time the user clicks the mouse.
//Provide for a minimum of 20 balls. Randomly choose the color for each new ball.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Ball extends JApplet implements Runnable, MouseListener {
private static final int NB_BALLS = 20;
private BlueBall[] blueBall = new BlueBall[NB_BALLS];
private int nbBalls = 0;

private Thread blueBallThread;

public void init()
{
addMouseListener( this );

blueBallThread = new Thread(this);
blueBallThread.start();
}

public void mousePressed( MouseEvent e )
{
if (nbBalls < NB_BALLS ) {
blueBall[nbBalls] = new BlueBall(e.getX(), e.getY());
blueBall[nbBalls].start();
nbBalls++;
}
}

public void stop()
{

}

public void paint( Graphics g )
{
g.clearRect(0,0,300,300);

for(int i = 0; i < nbBalls; i++)
{
if (blueBall[i].bouncing )
{
g.setColor( blueBall[i].color );
g.fillOval( blueBall[i].x, blueBall[i].y, 10, 10 );
}
}
}

public void run()
{
while (true)
{
try
{
blueBallThread.sleep(100);
}
catch ( Exception e )
{
System.err.println( "Exception: " + e.toString() );
}

for(int i = 0; i < nbBalls; i++)
{
if ( blueBall[i].xUp == true )
blueBall[i].x += blueBall[i].xDx;
else
blueBall[i].x -= blueBall[i].xDx;

if ( blueBall[i].yUp == true )
blueBall[i].y += blueBall[i].yDy;
else
blueBall[i].y -= blueBall[i].yDy;

if ( blueBall[i].y <= 0 )
{
blueBall[i].yUp = true;
blueBall[i].yDy = ( int ) (Math.random() * 5 + 2 );
}
else if ( blueBall[i].y >= 250)
{
blueBall[i].yDy = ( int ) ( Math.random() * 5 + 2 );
blueBall[i].yUp = false;
}

if ( blueBall[i].x <= 0 )
{
blueBall[i].xUp = true;
blueBall[i].xDx = ( int ) ( Math.random() * 5 + 2 );
}
else if ( blueBall[i].x >= 250)
{
blueBall[i].xUp = false;}
blueBall[i].xDx = ( int ) ( Math.random() * 5 + 2 );
}

repaint();
}
}

public void mouseExited( MouseEvent e ) {}

public void mouseClicked( MouseEvent e ) {}

public void mouseReleased( MouseEvent e ) {}

public void mouseEntered( MouseEvent e ) {}
}

class BlueBall extends Thread
{
public boolean xUp, yUp, bouncing;
public int x, y, xDx, yDy;
public Color color;

BlueBall(int x, int y)
{
this.x = x;
this.y = y;
Random ran = new Random();
color = new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255));
bouncing = true;
xUp = true;
yUp = true;
xDx = yDy = 2;
}
}