package makeDeal;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
public class Doors extends JFrame{
private int prizeDoor,doorChosen,eliminatedDoor;
private final int NUM_OF_DOORS=3;
private final int HEIGHT=400;
private final int WIDTH=500;
private JPanel instructions,switchDoor,doorPanel,card1,card2;
private JButton door1;
private JButton door2;
private JButton door3;
private JButton change,stay,restart,test,test1;
private ImageIcon winImage,loseImage,door;
private CardLayout cardLayout;
private Host host;
private Deal newGame;
private Contestant con;
final String TEST="test";
final String TEST1="change";
JPanel cards;
/***********************************
* The Doors constructor initializes
* the fields and builds a portion
* of the GUI.
***********************************/
public Doors(){
host=new Host();
con=new Contestant();
door=new ImageIcon("C:\\Users\\Owner\\Pictures\\project JPGS\\door.jpg");
door1=new JButton(door);
door2=new JButton(door);
door3=new JButton(door);
instructions=new JPanel();
JLabel firstInstructions=new JLabel("Pick a door.");
instructions.add(firstInstructions);
buildDoorPanel();
buildCommunicationPanel();
winImage=new ImageIcon("C:\\Users\\Owner\\Pictures\\project JPGS\\winImage.jpg");
loseImage=new ImageIcon("C:\\Users\\Owner\\Pictures\\project JPGS\\loseImage.jpg");
setVisible(true);
}
/******************************************
* The buildDoorPanel builds the portion
* of the frame that has the door buttons
* and registers them to the ButtonListener
* class.
*******************************************/
public void buildDoorPanel(){
JPanel panel= new JPanel();
JPanel blank=new JPanel();
panel.add(instructions);
//Creates the look and layout of the GUI.
setTitle("Lets Make a Deal!");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(30,10));
//Adds the components to the container.
doorPanel=new JPanel();
doorPanel.setLayout(new GridLayout(1,3));
doorPanel.add(door1);
doorPanel.add(door2);
doorPanel.add(door3);
add(doorPanel, BorderLayout.CENTER);
add(panel, BorderLayout.NORTH);
add(blank,BorderLayout.SOUTH);
//Registers each of the buttons.
door1.addActionListener(new ButtonListener());
door2.addActionListener(new ButtonListener());
door3.addActionListener(new ButtonListener());
//////////////////////////////////////////////////////////////
//cardLayout = new CardLayout();
cards=new JPanel();
card1=new JPanel();
card2=new JPanel();
test=new JButton("test");
test1=new JButton("test1");
cards.setLayout(new CardLayout());
card1.add(test);
card2.add(test1);
cards.add(card1, TEST);
cards.add(card2,TEST1);
add(cards, BorderLayout.WEST);
test.addActionListener(new CardListener());
//////////////////////////////////////////////////////////////////
}
/*******************************************
* The buildCommunicationPanel method builds
* the buttons that prompt the user to either
* switch their door with the host or keep
* the one they chose initially.
*******************************************/
public void buildCommunicationPanel(){
switchDoor=new JPanel();
stay=new JButton("Stay");
change= new JButton("Switch");
restart=new JButton("New Game");
switchDoor.add(stay);
switchDoor.add(change);
switchDoor.add(restart);
add(switchDoor,BorderLayout.SOUTH);
stay.addActionListener(new CommunicationListener());
change.addActionListener(new CommunicationListener());
restart.addActionListener(new CommunicationListener());
}
public int getPrizeDoor()
{
return prizeDoor;
}
public void setPrizeDoor()
{
Random randNum = new Random();
prizeDoor=randNum.nextInt(NUM_OF_DOORS);
}
/*********************************************
* The playGame method runs the game after the
* door is chosen by the player.
*********************************************/
public void playGame(){
//Initializes the doorChosen and prizeDoor variables.
int doorChosen=con.getDoorChosen();
setPrizeDoor();
prizeDoor=getPrizeDoor();
//Gets the eliminated door from the host class.
host.setEliminatedDoor(con,prizeDoor);
eliminatedDoor=host.getEliminatedDoor();
}
/***********************************************
* The ButtonListener inner class is used
* after the user chooses a door from the
* graphical interface.
***********************************************/
private class ButtonListener implements ActionListener{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e){
JLabel secondInstructions=new JLabel("Would you like to keep or change doors.");
instructions.add(secondInstructions);
//This set of if else statements is used to initialize
//the doorChosen field of the contestant class.
if(eliminatedDoor<0){
if(e.getSource()==door1){
con.setDoorChosen(0);}
else if(e.getSource()==door2){
con.setDoorChosen(1);}
else if(e.getSource()==door3){
con.setDoorChosen(2);}}
//This set of if else statements turns the door eliminated
//by the host black and then disables the eliminated door.
if(eliminatedDoor==0){
//door1.(loseImage);
door1.disable();}
else if(eliminatedDoor==1){
//door2.setCursor(Color.BLACK);
door2.disable();}
else if(eliminatedDoor==2){
//door3.update(loseImage);
door3.disable();}
playGame();
System.out.print("prize door"+prizeDoor+" chosen "+doorChosen+" eliminated "+eliminatedDoor);
setVisible(true);
}
}
private class CommunicationListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int win;
if(e.getSource()==restart)
newGame=new Deal();
if(e.getSource()==change)
con.switchDoors(host);
System.out.println("after switch"+con.getDoorChosen());
con.determineWin(getPrizeDoor());
win=con.getWins();
if(win>0){
door2=new JButton(winImage);
setVisible(true);}
//else{
//icon.setIcon(loseImage);
//add(icon,BorderLayout.CENTER);
//setVisible(true);}
}
}
private class CardListener implements ActionListener{
public void actionPerfomed(ActionEvent e){
CardLayout cl=(CardLayout) (cards.getLayout());
if (e.getSource()==test)
cl.next(cards);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
} |