Craps

//starter import javax.swing.JFrame; public class Starter extends JFrame { public Starter() { add(new Craps()); setTitle("Craps"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new Starter(); } } //craps 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; boolean runAgain = true; private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; int x; int die1; int die2; int dice; int point; int dice2; int die12; int die22; BufferedImage img; String message = "Click Board to Start"; String message2=""; String message3=""; String message4=""; String message5=""; private Thread animator; boolean roll = false; Scanner scan = new Scanner(System.in); public Craps()//constructor class { x = 0; 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); Font small = new Font("Helvetica", Font.BOLD, 14); FontMetrics metr = this.getFontMetrics(small); g.setColor(Color.white); g.setFont(small); g.drawString(message, 10, d.height-80); g.drawString(message2, 10, d.height-65); g.drawString(message3, 10, d.height-50); g.drawString(message4, 10, d.height-400); g.drawString(message5, 10, d.height-35); if (ingame) { x = x + 5; // g.drawImage(img,0,0,200,200 ,null); if (die1 == 1) g.drawImage(img, 100, 200, 164, 264, 0, 0, 64, 64, null);//die1 if (die1 == 2) g.drawImage(img, 100, 200, 164, 264, 64, 0, 128, 64, null); if (die1 == 3) g.drawImage(img, 100, 200, 164, 264, 128, 0, 192, 64, null); if (die1 == 4) g.drawImage(img, 100, 200, 164, 264, 0, 64, 64, 128, null); if (die1 == 5) g.drawImage(img, 100, 200, 164, 264, 64, 64, 128, 128, null); if (die1 == 6) g.drawImage(img, 100, 200, 164, 264, 128, 64, 192, 128, null); if (die2 == 1) g.drawImage(img, 200, 200, 264, 264, 0, 0, 64, 64, null);//die2 if (die2 == 2) g.drawImage(img, 200, 200, 264, 264, 64, 0, 128, 64, null); if (die2 == 3) g.drawImage(img, 200, 200, 264, 264, 128, 0, 192, 64, null); if (die2 == 4) g.drawImage(img, 200, 200, 264, 264, 0, 64, 64, 128, null); if (die2 == 5) g.drawImage(img, 200, 200, 264, 264, 64, 64, 128, 128, null); if (die2 == 6) g.drawImage(img, 200, 200, 264, 264, 128, 64, 192, 128, null); if (die12 == 1) g.drawImage(img, 100, 200, 164, 264, 0, 0, 64, 64, null);//die1 if (die12 == 2) g.drawImage(img, 100, 200, 164, 264, 64, 0, 128, 64, null); if (die12 == 3) g.drawImage(img, 100, 200, 164, 264, 128, 0, 192, 64, null); if (die12 == 4) g.drawImage(img, 100, 200, 164, 264, 0, 64, 64, 128, null); if (die12 == 5) g.drawImage(img, 100, 200, 164, 264, 64, 64, 128, 128, null); if (die12 == 6) g.drawImage(img, 100, 200, 164, 264, 128, 64, 192, 128, null); if (die22 == 1) g.drawImage(img, 200, 200, 264, 264, 0, 0, 64, 64, null);//die2 if (die22 == 2) g.drawImage(img, 200, 200, 264, 264, 64, 0, 128, 64, null); if (die22 == 3) g.drawImage(img, 200, 200, 264, 264, 128, 0, 192, 64, null); if (die22 == 4) g.drawImage(img, 200, 200, 264, 264, 0, 64, 64, 128, null); if (die22 == 5) g.drawImage(img, 200, 200, 264, 264, 64, 64, 128, 128, null); if (die22 == 6) g.drawImage(img, 200, 200, 264, 264, 128, 64, 192, 128, 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(); ingame=true; if (roll == false){ openRoll(); message = "Your initial roll is: " + die1 + " and " + die2 + " = " + (die1 + die2); openRollLogic(); return; } if (roll == true){ rolls(); message5 = "You now rolled: " + die12 + " and " + die22 + " = " + (die12 + die22); inGameLogic(); } } public void openRoll(){ die1 = createRandomNums(1,6); die2 = createRandomNums(1,6); dice = die1 + die2; } public void rolls(){ die12 = createRandomNums(1,6); die22 = createRandomNums(1,6); dice2 = die12 + die22; } public int createRandomNums (int min,int max){ int rnd = (int) (Math.random( ) * (max)) + min; return rnd; } public void openRollLogic(){ if (dice == 7 || dice == 11) { message4 = "You immedietly won! WOW!"; }else if (dice == 2 || dice == 3 || dice == 12) { message4 = "You immedietly crapped out!"; }else{ //(dice != 2 || dice != 3 || dice != 12 || dice != 7 || dice != 11) message2 = "Your point is: " + (dice); message3 = "Click to roll again."; point = dice; roll = true; } } public void inGameLogic(){ //int point = dice; if (point == dice2){ message = ""; message4 = "You won!"; //roll = false; }else if (dice2 == 7){ message = ""; message4 = ("You've crapped out!"); //roll = false; }else{ message = ""; //rolls(); } } // public void pointEstablished(point){ //} //public void dice public void mouseClicked(MouseEvent e){ } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); int animationDelay = 10; 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
this one works but is very basic

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.