/* A java file by Kenneth L. Moore This file shows some basic java functionality by implementing moving balls with apparent mass. You can adjust the ball speed by Thread.sleep. The large balls should move lazily and the small balls should zip around. */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Ball extends Applet implements Runnable, ActionListener{ // declare global variables - define rectangle private int rectLeftX = 0, rectRightX = 300; private int rectTopY = 0, rectBottomY = 300; Button bStart, bStop; boolean go=true; // for flickerless animation, buffer the image drawing. Image gBuffImage; Graphics gBuff; // just for fun, make this applet run as a thread. Thread runner; // declare the balls to be instantiated. BallObject theBall1, theBall2, theBall3, theBall4, theBall5, theBall6, theBall7, theBall8; // over ride update to prevent flicker. public void update(Graphics g){ paint(g); } public void start(){ if (runner == null){ runner = new Thread(this); } } public void stop(){ if(runner != null){ runner = null; } } public void destroy(){ gBuff.dispose(); } public void run(){ Thread theThread = Thread.currentThread(); while(runner == theThread){ repaint(); } } public void init (){ /* instantiate the balls with small balls having greater change in position to simulate lesser mass. */ // set up an image and a graphics pallet to write to gBuffImage = createImage(getSize().width,getSize().height); gBuff = gBuffImage.getGraphics(); gBuff.setColor(Color.white); gBuff.fillRect(0,0,getSize().width,getSize().height); setLayout(null); setBackground(Color.white);// get rid of (ugg) default gray. bStart = new Button("Start"); bStop = new Button("Stop"); bStart.setLocation(20,5); bStop.setLocation(80,5); bStart.setSize(50,30); bStop.setSize(50,30); bStart.addActionListener(this); bStop.addActionListener(this); add(bStart);add(bStop); theBall1 = new BallObject(20,20,12,3,10,Color.blue); theBall2 = new BallObject(150,140,-5,-10,20,Color.red); theBall3 = new BallObject(100,250,6,2,14,Color.green); theBall4 = new BallObject(200,100,-5,-1,25,Color.cyan); theBall5 = new BallObject(150,150,-1,-3,30,Color.magenta); theBall6 = new BallObject(50,50,-10,-20,6,Color.black); theBall7 = new BallObject(100,80,-1,-2,35,Color.orange); theBall8 = new BallObject(150,175, -1, 2, 40, Color.pink); } public void paint(Graphics g){ if(go){ try { // delay 1/20 th of a second for animation Thread.sleep(50); // update ball positions doBall(theBall1); doBall(theBall2); doBall(theBall3); doBall(theBall4); doBall(theBall5); doBall(theBall6); doBall(theBall7); doBall(theBall8); // dump the buffer g.drawImage(gBuffImage,0,0,this); } catch (InterruptedException e){// do nothing } } repaint(); } public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("Start")){ go = true; repaint(); } else if (e.getActionCommand().equals("Stop")){ go = false; } } public void doBall(BallObject aBall){ // blank old ball gBuff.setColor(Color.white); gBuff.fillOval(aBall.getX(),aBall.getY(),aBall.getD(),aBall.getD()); // check for bounds violations if(aBall.getX() <= rectLeftX) aBall.xChange = -aBall.xChange; if(aBall.getX() >= rectRightX) aBall.xChange = -aBall.xChange; if(aBall.getY() <= rectTopY) aBall.yChange = -aBall.yChange; if(aBall.getY() >= rectBottomY) aBall.yChange = -aBall.yChange; // move ball aBall.xPosition = aBall.xPosition + aBall.xChange; aBall.yPosition = aBall.yPosition + aBall.yChange; gBuff.setColor(aBall.getColor()); gBuff.fillOval(aBall.xPosition,aBall.yPosition,aBall.getD(),aBall.getD()); } class BallObject{ private int xPosition = 20, yPosition = 20, xChange = 9, yChange = 3, diameter= 10; Color ballColor; // give default constructor public BallObject(){ } // give override constructor public BallObject(int xP, int yP, int xCh, int yCh, int dia, Color ballC){ diameter = dia; ballColor = ballC; xPosition = xP; xChange = xCh; yChange = yCh; } public int getX(){ return xPosition; } public int getY(){ return yPosition; } public int getD(){ return diameter; } public Color getColor(){ return ballColor; } } }