Assignment

Solution 

SlotMachine.java 

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.Container;

importjava.awt.Dimension;

importjava.awt.Font;

importjava.awt.Graphics;

importjava.awt.GridLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.util.Random;

importjavax.swing.JButton;

importjavax.swing.JFrame;

importjavax.swing.JOptionPane;

importjavax.swing.JPanel;

// displays output

classOutputPanel extends JPanel {

private String message; //current message to display

publicOutputPanel() {

message = “Welcome to JSlotMachine 1.0”;

setPreferredSize(new Dimension(200, 100));

}

public void setMessage(String message) {

this.message = message;

repaint();

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

Font f = new Font(“Arial”, Font.BOLD, 15);

g.setFont(f);

g.drawString(message, 50, 50);

}

}

// slot panel similar to colort panel

classSlotPanel extends JPanel {

private Color col;

privateint shape;

publicSlotPanel() {

spin(); //spin to display any random shape and color

}

@Override

public void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(col);

if (shape == 0)// rectangle

g.fillRect(20, 20, 100, 100);

else //color

g.fillOval(20, 20, 100, 100);

}

// spin

public void spin() {

Random rnd = new Random();

// random color

intcolorCode = rnd.nextInt(3);

if (colorCode == 0) {

col = Color.RED;

} else if (colorCode == 1) {

col = Color.GREEN;

} else {

col = Color.BLUE;

}

// random shape

shape = rnd.nextInt(2);

repaint();

}

//getters

public Color getCol() {

returnthis.col;

}

publicintgetShape() {

returnthis.shape;

}

}

classSlotFrame extends JFrame {

// slor panels

privateSlotPanelspLeft, spMid, spRight;

privateOutputPanelpnlInfo; //output panel

private double balance; //user balance

publicSlotFrame() {

balance = 1.00; // $1

setDefaultCloseOperation(EXIT_ON_CLOSE);

setBounds(100, 100, 400, 400);

setTitle(“JSlotMachine Version 1.0”);

Container c = getContentPane();

c.setLayout(new BorderLayout());

JPanelpanSouth = new JPanel();

// create and add buttons

JButtonbtnBetMax = new JButton(“Bet Max”);

panSouth.add(btnBetMax);

btnBetMax.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

betAndSpin(0.50);

}

});

JButtonbtnBetMin = new JButton(“Bet Min”);

panSouth.add(btnBetMin);

btnBetMin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

betAndSpin(0.10);

}

});

JButtonbtnSpin = new JButton(“Spin”);

panSouth.add(btnSpin);

btnSpin.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

betAndSpin(0.25);

}

});

c.add(panSouth, BorderLayout.SOUTH);

pnlInfo = new OutputPanel();

c.add(pnlInfo, BorderLayout.NORTH);

JPanelpanCenter = new JPanel();

panCenter.setLayout(new GridLayout(1, 3));

spLeft = new SlotPanel();

spMid = new SlotPanel();

spRight = new SlotPanel();

panCenter.add(spLeft);

panCenter.add(spMid);

panCenter.add(spRight);

c.add(panCenter, BorderLayout.CENTER);

}

public void betAndSpin(double betAmount) {

if(balance <= 0) //if negative or 0, cannot spin

return;

// cap max bet to min of balance or bet

betAmount = Math.min(betAmount, balance);

// spin

spLeft.spin();

spRight.spin();

spMid.spin();

// if all same

if(spLeft.getCol() == spRight.getCol() &&spRight.getCol() == spMid.getCol()

&&spLeft.getShape() == spRight.getShape() &&spRight.getShape() == spMid.getShape()) {

balance += betAmount;

} else { //different

balance -= betAmount;

}

// show output

pnlInfo.setMessage(String.format(“Current Balance: $%.2f”, balance));

// if no balnce left

if(balance <= 0) {

pnlInfo.setMessage(“You have no moeny left. Game Over.”);

}

}

}

// creats and displayts a slot frame

public class SlotMachine {

public static void main(String[] args) {

SlotFramefrm = new SlotFrame();

frm.setVisible(true);

}

}