I have a ball class. Wanna look?
I like sharing my codez! This is just for a pong game I'm writing. <3 I hope this helps. I used graphics.Code:/** * * @author Erica Boyer * @datecreated 31 Mar 2009 * * This class handles the ball. */ import javax.swing.*; import java.awt.*; import java.util.*; public class ball { private final int BALL_DIAMETER = 10; private final int INIT_X = 400 - 5; //Just so its centered private final int INIT_Y = 300 - 5; private int xCoord; private int yCoord; private int height; private int width; private int rise; private int run; private Color color; private int panelHeight,panelWidth; public ball() { setXCoord(); setYCoord(); setRise(); setRun(); setColor(); }//End constructor /** This method sets the initial x-coordinate */ public void setXCoord() { int max = 51; Random rand = new Random(); int tempX = rand.nextInt(max); xCoord = tempX; }//End setXCoord method public int getXCoord() { return(xCoord); }//End getXCoord method /** This method sets the initial y-coordinate */ public void setYCoord() { int max = 51; Random rand = new Random(); int tempY = rand.nextInt(max); yCoord = tempY; }//End setYCoord method public int getYCoord() { return(yCoord); }//End getYCoord method /** This method sets the initial rise */ public void setRise() { int max = 11; Random rand = new Random(); int tempR = rand.nextInt(max); rise = tempR; }//End setRise method public int getRise() { return(rise); }//End getRise method /** This method sets the initial run */ public void setRun() { int max = 11; Random rand = new Random(); int tempR = rand.nextInt(max); run = tempR; }//End setRise method public int getRun() { return(run); }//End getRun method /** This method sets a random color to color */ public void setColor() { int red = (int)(Math.random() * 255); int green = (int)(Math.random() * 255); int blue = (int)(Math.random() * 255); Color randomColor = new Color(red, green, blue); color = randomColor; }//End setColor method public Color getColor() { return(color); }//End getColor method public void paintComponent (Graphics g) { g.setColor(color); g.fillOval(xCoord, yCoord, width, height); } // end method paintComponent public void move() { // If ball is approaching a wall, reverse direction if (xCoord < (0 - run) || xCoord > (panelWidth - width)) { run = -run; } if (yCoord < (0 - rise) || yCoord > (panelHeight - height)) { rise = -rise; } // "Move" ball according to values in rise and run xCoord += run; yCoord += rise; }//End move method }//End ball class







Reply With Quote