import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
public class Pong extends JPanel implements Runnable, MouseListener
{
boolean ingame = false;
private Dimension d;
int BOARD_WIDTH=500;
int BOARD_HEIGHT=500;
int die1;
int die2;
int imageX = 0;
int imageY = 0;
int imageX2 = 0;
int imageY2 = 0;
int spacer2 = 64;
int spacer = 64;
int x = 0;
int cash = 100;
int bet = 0;
int points = 0;
boolean start = false;
BufferedImage img;
String message = "Click Board to Start";
String message2 = "";
String message3 = "";
String message4 = "";
String message5 = "";
private Thread animator;
public Pong()
{
addKeyListener(new TAdapter());
addMouseListener(this);
setFocusable(true);
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
setBackground(Color.black);
try {
img = ImageIO.read(this.getClass().getResource("dice.png"));
} catch (IOException e) {
System.out.println("Image could not be read");
// System.exit(1);
}
if (animator == null || !ingame) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, d.width, d.height);
g.setColor(Color.red);
g.fillRect(0, 0, 50, 50);
g.fillOval(x,20,70,20);
Font small = new Font("Helvetica", Font.BOLD, 14);
Font big = new Font("Helvetica", Font.BOLD, 28);
Font medium = new Font("Helvetica", Font.BOLD, 9);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.black);
g.setFont(small);
g.drawString(message, 10, d.height-60);
g.drawString(message2, 20, d.height-420);
g.drawString(message4, 20, d.height-400);
g.setColor(Color.blue);
g.setFont(medium);
g.drawString(message3, 20, d.height-440);
g.setColor(Color.green);
g.setFont(big);
g.drawString(message5, 50, d.height-470);
if (ingame) {
x = x + 5;
g.drawImage(img,200,200, 400,264,imageX,imageY,imageX + spacer,imageY + spacer,null);
g.drawImage(img,250,250, 500,320,imageX2,imageY2,imageX2 + spacer2,imageY2 + spacer2,null);
//g.drawImage(img,150,250, 500,320,imageX2,imageY2,imageX2 + spacer2,imageY2 + spacer2,null);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
}
public void keyPressed(KeyEvent e) {
//System.out.println( e.getKeyCode());
// message = "Key Pressed: " + e.getKeyCode();
int key = e.getKeyCode();
if(key==49){
bet = bet + 1;
}
if(key==50){
bet = bet + 2;
}
if(key==51){
bet = bet + 3;
}
if(key==52){
bet = bet + 4;
}
if(key==53){
bet = bet + 5;
}
if(key==54){
bet = bet + 6;
}
if(key==55){
bet = bet + 7;
}
if(key==56){
bet = bet + 8;
}
if(key==57){
bet = bet + 9;
}
if(key ==81){
start = true;
}
if(key == 87){
bet = 0;
}
if (key == 69){
start = false;
}
}
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
ingame = true;
message2 = "Cash : " + cash;
message5 = "Welcome to craps!";
message3 = "Put in how much you wish to bet, click to register, w to clear bets, press q to start and e to stop";
message4 = "Bet : " + bet;
message = "die1: " + die1 + " die2: " + die2;
if(start){
openRoll();
openRollLogic();
}
/*
inGameLogic();
cashValue();
*/
}
public void openRoll(){
die1 = makeRandomNumbers(1, 6);
die2 = makeRandomNumbers(1, 6);
openRollLogic();
placeDiceOnScreen();
placeDiceOnScreen2();
}
public int makeRandomNumbers(int min, int max){
int rand = (int) (Math.random() * (max)) + min;
return rand;
}
public void openRollLogic(){
int tot = die1 + die2;
int again = 0;
while (again == 0){
if (tot == 7 || tot == 11){
message = "You win, turn 1, your roll was: " + tot ;
again++;
cash = cash + bet + bet;
}else if (tot == 2 || tot == 3 || tot == 12){
message = "You lose, turn 1, your roll was: " + tot;
again++;
cash = cash - bet;
} else {
int pnt = tot;
//openRoll();
if (pnt == tot){
message = "You Win, Your point is: " + tot;
again++;
cash = cash + bet + bet;
}
else if (tot == 7){
message = "You Lose, Your point is: " + tot;
again++;
cash = cash - bet;
}
}
}
}
public void placeDiceOnScreen(){
imageX = 0;
if(die1>3){
imageY = spacer;
}else{
imageY = 0;
}
if(die1<4){
imageX = (spacer * die1) - spacer;
}else{
imageX = ((spacer * die1) - spacer) - 192;
}
}
public void placeDiceOnScreen2(){
imageX2 = 0;
if(die2>3){
imageY2 = spacer2;
}else{
imageY2 = 0;
}
if(die2<4){
imageX2 = (spacer2 * die2) - spacer2;
}else{
imageX2 = ((spacer2 * die2) - spacer2) - 192;
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 20;
long time =
System.currentTimeMillis();
while (true) {//infinite loop
// spriteManager.update();
repaint();
try {
time += animationDelay;
Thread.sleep(Math.max(0,time -
System.currentTimeMillis()));
}catch (InterruptedException e) {
System.out.println(e);
}//end catch
}//end while loop
}//end of run
}//end of class
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.