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.*;
import java.util.*;
public class Craps extends JPanel implements Runnable, MouseListener
{
boolean ingame = false;
private Dimension d;
int BOARD_WIDTH=500;
int BOARD_HEIGHT=500;
int die1;
int die2;
int money = 100;
int bet = 10;
int x = 0;
boolean firstRoll = true;
boolean continued = false;
int lastValue = 0;
BufferedImage img;
String message = "Click Board to Start";
String statusMessage = "";
private Thread animator;
public Craps()
{
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);
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.black);
g.setFont(small);
g.drawString(message, 10, d.height-60);
g.drawString(statusMessage, 10, d.height - 40);
if (ingame) {
g.drawString("Money: " + money, 10, 20);
g.drawString("Bet: " + bet, 10, 40);
g.drawString("Point: " + lastValue, 10,60);
x = x + 5;
if (die1 <= 3) {
g.drawImage(img,100,200, 200, 300, ((64*die1) - 64),0,(64*die1),64,null);
}
else {
g.drawImage(img,100,200, 200, 300, ((64*(die1-3)) - 64),64,(64*(die1-3)),128,null);
}
if (die2 <= 3) {
g.drawImage(img,225,200, 325, 300, ((64*die2) - 64),0,(64*die2),64,null);
}
else {
g.drawImage(img,225,200, 325, 300, ((64*(die2-3)) - 64),64,(64*(die2-3)),128,null);
}
//g.drawImage(img,100,200, 264,264, 0,0,64,64,null);
//drawImage(Image img,int dstx1, int dsty1, int dstx 2, int dsty2,
//int srcx1, int srcy1, int srcx2, int srcy2,ImageObserver observer);
}
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==39){
}
}
}
public void mousePressed(MouseEvent e) {
if (money > 0) { //firstRoll || continued) {
int x = e.getX();
int y = e.getY();
ingame = true;
openRoll();
message = "die1: " + die1 + " die2: " + die2;
openRollLogic();
}
/*
inGameLogic();
cashValue();
*/
}
public void openRollLogic(){
int dice = die1 + die2;
if (firstRoll) {
if (dice == 7 || dice == 11){
System.out.println("You win!");
statusMessage = "You win!";
//continued = false;
}
else if (dice == 2 || dice == 3 || dice == 12){
System.out.println("You lose!");
statusMessage = "You lose!";
//continued = false;
} else {
continued = true;
lastValue = dice;
}
firstRoll = false;
} else {
if (dice == lastValue) {
System.out.println("You win! Got same value.");
statusMessage = "You win! Got same value.";
//continued = false;
}
else if (dice == 7) {
System.out.println("You lose!");
statusMessage = "You lose!";
//continued = false;
} else
statusMessage = "";
}
}
public void openRoll(){
die1 = makeRandomNumber(1,6);
die2 = makeRandomNumber(1,6);
}
public int makeRandomNumber(int min,int max){
int rand = (int)(Math.random() * (max))+min;
return rand;
}
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
}
}
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.