import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JLabel;
public class SchStPa extends JFrame implements ActionListener {
/* private GUI gui;
public SchStPa(GUI gui) {
this.gui = gui;
}*/
public void panRand() {
Random rand = new Random();
int i;
i = (rand.nextInt(3));
switch(i) {
case 0:
comp_auswahl = "Schere";
break;
case 1:
comp_auswahl = "Stein";
break;
case 2:
comp_auswahl = "Papier";
break;
}
}
public void change_Result(){
lblNewLabel.setText(auswahl);
lblComputerErgebnis.setText(comp_auswahl);
lblGewonnenHat.setText("Gewonnen hat:" +gewinner);
}
private JPanel contentPane;
private JButton btn_schere;
private JButton btn_Stein;
private JButton btn_Papier;
public String auswahl;
public String comp_auswahl;
//private String wahl;
private JLabel lblDuHast;
private JLabel lblNewLabel;
private JPanel panel;
private JPanel panel_1;
private JPanel panel_2;
private JPanel panel_3;
private JPanel panel_4;
private JLabel lblGewonnenHat;
private String gewinner;
private JLabel lblComputerHat;
private JLabel lblComputerErgebnis;
private JPanel panel_test;
public SchStPa() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
btn_schere = new JButton("Schere");
panel.add(btn_schere);
btn_schere.setActionCommand( "schere" );
btn_Stein = new JButton("Stein");
panel.add(btn_Stein);
btn_Stein.setActionCommand( "stein" );
btn_Papier = new JButton("Papier");
panel.add(btn_Papier);
btn_Papier.setActionCommand( "papier" );
panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new BorderLayout(0, 0));
panel_2 = new JPanel();
panel_1.add(panel_2, BorderLayout.NORTH);
panel_test = new JPanel();
panel_1.add(panel_test, BorderLayout.SOUTH);
lblDuHast = new JLabel("Du hast:");
panel_2.add(lblDuHast);
lblNewLabel = new JLabel(auswahl);
panel_2.add(lblNewLabel);
panel_3 = new JPanel();
panel_1.add(panel_test, BorderLayout.SOUTH);
lblComputerHat = new JLabel("Computer hat:");
panel_test.add(lblComputerHat);
lblComputerErgebnis = new JLabel(comp_auswahl);
panel_test.add(lblComputerErgebnis);
panel_4 = new JPanel();
panel_1.add(panel_4, BorderLayout.CENTER);
panel_4.setLayout(new BorderLayout(0, 0));
lblGewonnenHat = new JLabel("Gewonnen hat:" +gewinner);
panel_4.add(lblGewonnenHat, BorderLayout.SOUTH);
btn_schere.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
auswahl = "Schere";
panRand();
vergleich();
change_Result();
}
});
btn_Stein.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
auswahl = "Stein";
panRand();
vergleich();
change_Result();
}
});
btn_Papier.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
auswahl = "Papier";
panRand();
vergleich();
change_Result();
}
});
/*zeahler();
panRand();*/
}
/*public void zeahler() {
Timer repeat = new Timer();
TimerTask go = new TimerTask() {
@Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
lblNewLabel.setText(auswahl);
lblComputerErgebnis.setText(comp_auswahl);
lblGewonnenHat.setText("Gewonnen hat:" +gewinner);
}
}
);
}
};
repeat.schedule(go, 1000, 500);
}*/
public void vergleich() {
if (auswahl == comp_auswahl) {
gewinner = "Gleichstand";
} else if ((auswahl.equals("Schere")) && (comp_auswahl.equals("Stein"))) {
gewinner = "Dein Panda";
} else if ((auswahl.equals("Schere")) && (comp_auswahl.equals("Papier"))) {
gewinner = "Du!";
} else if ((auswahl.equals("Stein")) && (comp_auswahl.equals("Papier"))) {
gewinner = "Dein Panda";
} else if ((auswahl.equals("Stein")) && (comp_auswahl.equals("Schere"))) {
gewinner = "Du!";
} else if ((auswahl.equals("Papier")) && (comp_auswahl.equals("Schere"))) {
gewinner = "Dein Panda";
} else if ((auswahl.equals("Papier")) && (comp_auswahl.equals("Stein"))) {
gewinner = "Du!";
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
SchStPa frame = new SchStPa();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}