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.lang.Math.*;
public class Craps extends JPanel implements Runnable, MouseListener {
boolean ingame;
boolean flag;
private Dimension d;
int BOARD_WIDTH;
int BOARD_HEIGHT;
int x;
int point;
BufferedImage img; //shows image to screen
String message;
String winMessage;
String pointMessage;
private Thread animator;
int die1;
int die2;
//die image positions
int x1;
int x2;
int y1;
int y2;
public Craps() {
x = 0;
point = 0;
ingame = false;
flag = true;
BOARD_WIDTH = 500;
BOARD_HEIGHT = 500;
message = "Click Board to Roll Dice";
winMessage = "";
pointMessage = "";
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
addKeyListener(new TAdapter()); //interactivity with keyboard
addMouseListener(this); //interactivity with mouse
setFocusable(true); //board is in play
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); //sets the size of the board
setBackground(Color.black); //background color
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,18,20,20); //first 2 are x and y coordinates (position), last 2 are height and width of oval
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-480);
g.drawString(winMessage, 10, d.height-450);
g.drawString(pointMessage, 10, d.height-420);
if (ingame) {
//x = x + 5;
//g.drawImage(img,140,200,204,264,0,0,64,64,null); //first 4 for position of image, last 4 for portion of img selected (using corners)
//g.drawImage(img,290,200,354,264,0,0,64,64,null);
//drawImage(Image img,int dstx1, int dsty1, int dstx2, int dsty2,
// int srcx1, int srcy1, int srcx2, int srcy2,ImageObserver observer);
diceImage(die1, die2, g); //displays images of dice
}
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) {
int x = e.getX();
int y = e.getY();
ingame = true;
openRoll(); //calls makeRandomNumbers()
message = "Die 1: " + die1 + " . Die 2: " + die2; //displays dice rolls
if (flag) { //uses conditional logic to reset to opening roll if you win or lose
openRollLogic(die1, die2); //if the rolls are equal to the numbers that win/lose on an open roll(check rules)
} else {
inGameLogic(die1, die2); //if rolls are equal to the point established or 7 you win/lose(check rules)
}
//cashValue();
}
public void openRoll() { //sets die rolls to random numbers
die1 = makeRandomNumbers(1, 6);
die2 = makeRandomNumbers(1, 6);
}
public int makeRandomNumbers(int min, int max) { //generates random numbers
int rand = (int)(Math.random() * max) + min;
return rand;
}
public void openRollLogic(int die1, int die2) {
int roll = die1 + die2;
if ((roll == 7) || (roll == 11)) { //if you roll a 7 or 11 you win
winMessage = "You rolled a " + roll + ". You win.";
pointMessage = "Click to play again.";
flag = true; //gets in and out of if/else statement in mousePressed()
} else if ((roll == 2) || (roll == 3) || (roll == 12)) { //if you roll a 2, 3, or 12, you lose
winMessage = "You rolled a " + roll + ". You lose.";
pointMessage = "Click to play again.";
flag = true;
} else { //point established
winMessage = "You have established a point.";
pointMessage = "Your point is " + roll + ".";
point = roll;
flag = false;
}
}
public void inGameLogic(int die1, int die2) {
int roll = die1 + die2;
if (roll == point) { //if the next roll is equal to the point established, you win
winMessage = "You matched the point. You win.";
pointMessage = "Click to play again.";
flag = true;
} else if (roll == 7) { //if roll is equal to 7, you lose
winMessage = "You rolled a 7. You lose.";
pointMessage = "Click to play again.";
flag = true;
} else {
winMessage = "You didn't match the point. Click to roll again.";
flag = false;
}
}
public void diceImage(int die1, int die2, Graphics g) { //method to display die images
switch (die1) {
case 1:
g.drawImage(img,140,200,204,264,0,0,64,64,null); //if die1 rolls a 1, use this part of the image
break;
case 2:
g.drawImage(img,140,200,204,264,64,0,128,64,null);
break;
case 3:
g.drawImage(img,140,200,204,264,128,0,192,64,null);
break;
case 4:
g.drawImage(img,140,200,204,264,0,64,64,128,null);
break;
case 5:
g.drawImage(img,140,200,204,264,64,64,128,128,null);
break;
case 6:
g.drawImage(img,140,200,204,264,128,64,192,128,null);
break;
}
switch (die2) {
case 1:
g.drawImage(img,290,200,354,264,0,0,64,64,null);
break;
case 2:
g.drawImage(img,290,200,354,264,64,0,128,64,null);
break;
case 3:
g.drawImage(img,290,200,354,264,128,0,192,64,null);
break;
case 4:
g.drawImage(img,290,200,354,264,0,64,64,128,null);
break;
case 5:
g.drawImage(img,290,200,354,264,64,64,128,128,null);
break;
case 6:
g.drawImage(img,290,200,354,264,128,64,192,128,null);
break;
}
}
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.