Craps

//Starter Class: import javax.swing.JFrame; public class StarterDice extends JFrame { public StarterDice() { add(new Dice()); setTitle("Board"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new StarterDice(); } } //Board Class: 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 Dice extends JPanel implements Runnable, MouseListener { boolean ingame = false; private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; int x = 0; int y = 0; int Dice1; int Dice2; int rand; int point; int imageX1 = 0; int imageY1 = 0; int imageX2 = 0; int imageY2 = 0; int spacer = 64; int counter = 0; BufferedImage img; String message = "Click Board to Start"; String message2; private Thread animator; public Dice(){ 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,y,70,70); 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); try{ g.drawString(message2, 10, d.height-45); }catch (Exception e){ g.drawString(message, 10, d.height-60); } if (ingame) { placeDiceOnScreen(); g.drawImage(img,100,200, 164,264,imageX1,imageY1,imageX1 + spacer,imageY1 + spacer,null); g.drawImage(img,300,200, 364,264,imageX2,imageY2,imageX2 + spacer,imageY2 + spacer,null); //drawImage(Image img,int dstx1, int dsty1, int dstx2, 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) { int x = e.getX(); int y = e.getY(); openRoll(); ingame = true; cashValue(); } public void openRoll(){ Dice1 = makeRandomNum(1,6); Dice2 = makeRandomNum(1,6); message = " dice1: " + Dice1 + " dice2: " + Dice2; openRollLogic(); } public int makeRandomNum(int min,int max){ rand = (int)(Math.random() * (max)) + 1; return rand; } public void openRollLogic(){ if(Dice1 + Dice2 == 7 || Dice1 + Dice2 == 11) message += " You win!"; else if(Dice1 + Dice2 == 2 || Dice1 + Dice2 == 3 || Dice1 + Dice2 == 12) message += " You lose!"; else{ point = Dice1 + Dice2; Dice1 = makeRandomNum(1,6); Dice2 = makeRandomNum(1,6); counter++; message2 = "point: "; message2 += point; inGameLogic(); } } public void inGameLogic(){ while (Dice1 + Dice2 != point && Dice1 + Dice2 != 7){ Dice1 = makeRandomNum(1,6); Dice2 = makeRandomNum(1,6); counter ++; message += "dice1: " + Dice1 + " dice2: " + Dice2; } if (Dice1 + Dice2 == point) message += " You win!"; else message += " You lose!"; } public void cashValue(){ } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void placeDiceOnScreen(){ if(Dice1>=4){ imageY1 = spacer; }else{ imageY1 = 0; } if(Dice1<=3){ imageX1 = (spacer * Dice1) - spacer; }else{ imageX1 = ((spacer * Dice1) - spacer) - 192; } if(Dice2>=4){ imageY2 = spacer; }else{ imageY2 = 0; } if(Dice2<=3){ imageX2 = (spacer * Dice2) - spacer; }else{ imageX2 = ((spacer * Dice2) - spacer) - 192; } } 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.