Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

sHUFFLE BORAD GAME NEED HELP!

Hi,

I am trying to create a Shuffle board game, i seem to have problem aligning the circles, could some one please see the code and help me.

This is the main class

import java.util.Vector;
import java.util.Enumeration;
import java.util.Random;
import java.math.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ShuffleBoardApplication extends JFrame {

private ShuffleBoardPanel table; //billiard table panel

public ShuffleBoardApplication (String title) {
super(title);
setSize(250,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(100,100);
table = new ShuffleBoardPanel(this);
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
add(table); //add the whole table view

update();

}



public void update() {

repaint(); //repaint the window

}



public static void main(String args[]) {
ShuffleBoardApplication frame = new ShuffleBoardApplication("Off Da Bank");
frame.setVisible(true); //show the frame
}
}

this is the helper classes

import java.util.ArrayList;
import java.util.Vector;
import java.util.Random;
import java.math.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

// This class represents the view Billiard Table
public class ShuffleBoardPanel extends JPanel implements ActionListener{

public static int TableWidth = 300; //table view width in pixels
public static int TableHeight = 500; //table view length in pixels
private int OwnerWidth; //width of the owner frame
private int OwnerHeight; //height of the owner frame
public static int Border = TableWidth/10; //Width of table border

//banks which account for ball radius
private int leftWall = Border + weight.RADIUS;
private int top = Border + weight.RADIUS;
private int rightWall = TableWidth + Border - weight.RADIUS;
private int bottom = TableHeight + Border - weight.RADIUS;

private weight selectedBall = null; //weight being shot
private Point cueTip = null; //tip of the shooting cue
private Point cueEnd = null; //end of the shooting cue

private ArrayList<weight> Weights = new ArrayList<weight>(); //the game Weights
private ArrayList<weight> DeadBalls = new ArrayList<weight>(); //the balls in the pockets

public static ArrayList<Point> pockets = new ArrayList<Point>(); //locations of table pockets

private JFrame owner;

// private CollisionSet collisions = new CollisionSet();

//Timer used to draw motion
int milliseconds = 2; //time between timer events
Timer timer = new Timer(milliseconds, this);

public ShuffleBoardPanel (JFrame parent) {


owner = parent; //The frame the panel lives in

setTableSize();
initialize();
timer.start();

update();

}

private void setTableSize(){
OwnerWidth = owner.getWidth() ; //table view width in pixels
OwnerHeight = owner.getHeight(); //table view length in pixels
Border = OwnerWidth/10; //Width of table border
TableWidth = OwnerWidth - 2*Border; //width of table not including border
TableHeight = OwnerHeight - 2*Border -30; //height of table not including border
leftWall = Border + weight.RADIUS;
top = Border + weight.RADIUS;
rightWall = TableWidth + Border - weight.RADIUS;
bottom = TableHeight + Border - weight.RADIUS;
//Border = TableWidth/10;
setSize(TableWidth + 2*Border, TableHeight + 2*Border);
setPreferredSize(new Dimension(TableWidth+2*Border,TableHeight+2*Border ) );

}


public void initialize(){
//initialize the game based on the size of the table


Weights.clear();
DeadBalls.clear();
weight.RADIUS = TableWidth/20;

//Create some balls on the billiard table
int startX = TableWidth/2 + Border; //constants for setting up initial ball position
int startY = TableHeight/3;
int dX = weight.RADIUS;
int dY = 2*dX;


Weights.add(new weight(new Point(startX,startY), Color.gray));
Weights.add(new weight(new Point(startX-dX, startY-dY), Color.gray));
Weights.add(new weight(new Point(startX+dX, startY-dY), Color.gray));
Weights.add(new weight(new Point(startX, startY-2*dY), Color.gray));



} //end initialize




public void update() {
//update the panels

repaint();
}


// Return the weight at the given location (if one exists)


// This is the Timer event handler
// Advance any Weights that are currently in motion
public void actionPerformed(ActionEvent e) {



update();
}


// This is the method that is responsible for painting table and Weights
public void paintComponent(Graphics aPen) {



if( (OwnerWidth != owner.getWidth()) ||
(OwnerHeight != owner.getHeight() ) ) {
//table has resized
setTableSize();
initialize();
}

//draw the billard table
super.paintComponent(aPen);
setBackground(Color.orange.darker().darker());



//Draw boundary
aPen.setColor(Color.black.darker().darker().darker ().darker());
aPen.drawRect(Border,Border, TableWidth, TableHeight);
aPen.drawRect(Border,Border, TableWidth, 50);
aPen.drawRect(Border,Border, TableWidth, 90);

//Draw Weights
for (int i = 0; i<Weights.size(); i++)
{
weight st = Weights.get(i);
st.drawWith(aPen);
}




}

}


this is the one that created the weights

import java.util.*;
import java.awt.*;
import java.io.*;

//This class represents a Billiard weight.
public class weight {
// ====
// Here is a class variable to keep track of the radius of the Balls
public static int RADIUS = ShuffleBoardPanel.TableWidth/20; //radius of ball

// These are the instance variables
private Color color; //colour of the weight center
boolean moving = false; //indicates if weight is moving or stopped
private double x; //X location of the ball
private double y; //Y location of the ball
private Point location = new Point(0,0); //x-y location of the weight

//Horizontal and Vertical Velocities
//vx<0 means left, vx>0 means right; vy<0 means up, vy>0 means down
private double vx; //horizontal velocity in pixels/millisecond
private double vy; //vertical velocity in pixels/millisecond

//Constructor
public weight(Point aPoint, Color ballColor) {

color = ballColor;
location = aPoint;
x = location.getX();
y = location.getY();
//make the ball stopped
vx = 0.0;
vy = 0.0;
moving = false;

}

// The get & set methods
public Color getColour() { return color; }
public Point getLocation() { return location; }
public int getLocationX() {return location.x;}
public int getLocationY() {return location.y;}
public int getRadius() { return weight.RADIUS; }

public void setLocation(Point aPoint) {
location.x = aPoint.x;
location.y = aPoint.y;
x = aPoint.getX(); y = aPoint.getY(); }

public void setLocation(int theX, int theY) {
location.x = theX;
location.y = theY;
x = location.getX();
y = location.getY();
}

public void setMoving(boolean isMoving) { moving = isMoving; }
public boolean isMoving() {return moving; } //answer whether ball is moving

//get and set the velocities of the ball
public double getvx() {return vx;} //get horizontal velocity
public double getvy() {return vy;} //get vertical velocity
public void setvx( double v ) {vx = v; moving = ((vx != 0.0) || (vy != 0.0)); }
public void setvy( double v ) {vy = v; moving = ((vx != 0.0) || (vy != 0.0));}

public void stop() { vx=0; vy = 0; moving = false; }


// Draw the ball using the given Graphics object
public void drawWith(Graphics aPen) {


//Draw the colored center of weight
aPen.setColor(color);
aPen.fillOval(location.x - weight.RADIUS, location.y - weight.RADIUS, weight.RADIUS * 2, weight.RADIUS * 2);
aPen.setColor(Color.red);
aPen.fillOval(location.x - RADIUS, location.y - RADIUS, weight.RADIUS, weight.RADIUS);
// Draw a black border around the weight
aPen.setColor(Color.black);
aPen.drawOval(location.x - weight.RADIUS, location.y - weight.RADIUS, weight.RADIUS * 2, weight.RADIUS * 2);

}

// Draw the ball using the given Graphics object
public void drawShadowWith(Graphics aPen) {



}

}

Thanks,
Kim
[10755 byte] By [kimstanely] at [2007-11-11 8:13:07]