Java Programming (13.2)----craps gambling game design, comprehensive application exercises

Source: Internet
Author: User
Tags event listener

Face object listener inherit interface settings window set picture Settings button set text box random number

Craps Gambling Game Cast 2 dice, the first round 7, 11 for the player to win, 2,3,12 for the banker win, then the player to lose 7, the same as the first round of points to win;

1. Make Dice First

Package Com.lovo.homework;import Java.awt.graphics;import Java.awt.image;import javax.swing.imageicon;/** * Class: Dice * @ Author Abe properties: Points Picture */public class Dice {private int point;private static image[] Images = new Image[6];static {//static load Picture for (int i = 0; i < images.length; i++) {ImageIcon icon = new ImageIcon ("dice/" + (i + 1) + ". jpg"); Images[i] = icon.ge Timage ();}} /** * Constructor */public Dice () {this.point = (int) (Math.random () * 6 + 1);} /** * Draw a dice * xy coordinate icon size 135*135 */public void Draw (Graphics g, int i, int x, int y) {g.drawimage (dice.images[i], X, Y, 13 5, 135, NULL);} public int GetPoint () {return point;} public static image[] GetImages () {return images;}}

2. window and Main program

Package Com.lovo.homework;import Java.awt.button;import Java.awt.font;import java.awt.graphics;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jframe;import Javax.swing.jlabel;import javax.swing.timer;/** * Class: Window * @author Abe * Parent class: JFrame * Interface: Listener * Properties: Many */@SuppressWarnings ("Ser  Ial ") public class MyFrame extends JFrame implements ActionListener {private Button B1 = New button (" Start ");p rivate button B2 = New button ("Stop");p rivate button B3 = New button ("One more game! ");p ublic JLabel jl1 = new JLabel ();p ublic JLabel jl2 = new JLabel ();p ublic JLabel jl3 = new JLabel ();p rivate Timer timer = Null;private Dice p1;private Dice p2;private int round = 0;private int first = 0;private Boolean gameover = false;/** *  Interface: overriding event listener * @author Abe */@Overridepublic void actionperformed (ActionEvent e) {Object obj = E.getsource (); if (!gameover) {if (obj = = B1) {Timer.start (); Jl3.settext ("N" + (round + 1) + "Round"), repaint (),} else if (obj = = b2) {timer.stop (); if (Rou nd = = 0) {first = P1.getpoint () + p2.getpoint () Jl1.settext ("First cast points:" + "), Round++;switch" {Case 2:case 3:case 12:jl2.se Ttext ("The player first-round cast out" + "+" point, the dealer wins!! Gameover = true;break;case 7:case 11:jl2.settext ("The player has won the" + First + "point in the initial round). "); gameover = True;}} else if (first = = P1.getpoint () + p2.getpoint ()) {Round++;jl2.settext ("the player throws the same" + "+" points as the same as the same one), the player wins!! "); gameover = true;} else if (p1.getpoint () + p2.getpoint () = = 7) {Round++;jl2.settext ("the player throws 7 points and the dealer wins!") "); gameover = true;} else {Round++;jl2.settext ("player throws" + (P1.getpoint () + p2.getpoint ()) + "point, Game continues ~");}} else if (obj = = timer) {p1 = new Dice ();p 2 = new Dice (); repaint ();}} if (obj = = B3 && Gameover = = True) {Gameover = False;round = 0;jl1.settext (""); Jl2.settext ("");}} /** * Constructor */public MyFrame () {this.settitle ("Craps Amusement Machine"); This.setsize (+); This.setdefaultcloseoperation (exit_on _close); this.setresizable (false); This.setlocationrelativeto (null); This.setlayout (null); Jl1.setbounds (50, 250, New Font ("Microsoft Jl1.setfont(Font.Bold, Jl2.setbounds), Jl2.setfont (New Font ("Microsoft Jas Black", Font.Bold,)); Jl3.setbounds (50, 150); Jl3.setfont (New Font ("Microsoft Jas Black", Font.Bold)); B1.addactionlistener (this); B2.addactionlistener (this); B3.addactionlistener (this); timer = new Timer (+, this), B1.setbounds (275, N, 100, 50); B3.setbounds (670, +, +), This.add (B1); This.add (B2); This.add (b3); This.add (JL1); This.add (JL2); This.add (JL3);} /** * Rewrite: Paint method */@Overridepublic void Paint (Graphics g) {super.paint (g); if (P1! = null && P2! = null) {P1.draw (g , P1.getpoint ()-1, 2.draw (g, P2.getpoint ()-1, 425, 100);p;}}

The window is a bit big ... Too lazy to adjust ...

3.

Package com.lovo.homework;/** * Class: Actuator? * @author Abe */public class Crapstest {public static void main (string[] args) {new MyFrame (). setvisible (True);}}

Java Programming (13.2)----craps gambling game design, comprehensive application exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.