import java.awt.*;
import java.awt.event.*;
import KFGTools.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
*
* Beschreibung
*
* @version 1.0 vom 01.02.2012
* @author
*/
public class Rate_die_Zahl extends Frame {
// Anfang Attribute
private Button button1 = new Button();
private NumberField numberField1 = new NumberField();
private Label label1 = new Label();
private Label label2 = new Label();
private int x,versuche=0,spiele=0,punkte=200;
private String user;
private Button button2 = new Button();
private Label label3 = new Label();
private Label label4 = new Label();
private Label label5 = new Label();
private Label label6 = new Label();
private Label label7 = new Label();
private Label label8 = new Label();
private Button button3 = new Button();
private Label label9 = new Label();
private TextField textField1 = new TextField();
private Label label10 = new Label();
private Button button4 = new Button();
private Label label11 = new Label();
private Button button5 = new Button();
private JTextArea jTextArea1 = new JTextArea("");
private JScrollPane jTextArea1ScrollPane = new JScrollPane(jTextArea1);
public static java.sql.Timestamp getTimestamp() {
java.util.Date today = new java.util.Date();
return new java.sql.Timestamp(today.getTime());
}
private Label label12 = new Label();
private Button button6 = new Button();
// Ende Attribute
public Rate_die_Zahl(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { dispose(); }
});
int frameWidth = 200;
int frameHeight = 308;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Panel cp = new Panel(null);
add(cp);
// Anfang Komponenten
button1.setBounds(8, 32, 176, 20);
button1.setLabel("Zufällige Zahl erzeugen");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1_ActionPerformed(evt);
}
});
button1.setVisible(false);
cp.add(button1);
numberField1.setBounds(8, 120, 75, 20);
numberField1.setText("");
numberField1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
numberField1_MouseClicked(evt);
}
});
numberField1.setVisible(false);
numberField1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
numberField1_KeyPressed(evt);
}
});
numberField1.setForeground(new Color(0x333333));
cp.add(numberField1);
label1.setBounds(8, 72, 144, 20);
label1.setText("Bitte eine Zahl zwischen");
label1.setVisible(false);
cp.add(label1);
label2.setBounds(8, 88, 114, 20);
label2.setText("0 und 100 angeben!");
label2.setVisible(false);
cp.add(label2);
button2.setBounds(112, 120, 72, 20);
button2.setLabel("Anwenden");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2_ActionPerformed(evt);
}
});
button2.setVisible(false);
button2.setEnabled(false);
cp.add(button2);
label3.setBounds(8, 176, 59, 20);
label3.setText("Versuche:");
label3.setVisible(false);
cp.add(label3);
label4.setBounds(72, 176, 22, 20);
label4.setText("0");
label4.setAlignment(Label.RIGHT);
label4.setVisible(false);
cp.add(label4);
label5.setBounds(8, 152, 54, 20);
label5.setText("Spiel Nr.");
label5.setVisible(false);
cp.add(label5);
label6.setBounds(64, 152, 22, 20);
label6.setText("0");
label6.setAlignment(Label.LEFT);
label6.setVisible(false);
cp.add(label6);
setTitle("RatedieZahl");
setUndecorated(false);
label7.setBounds(8, 200, 56, 20);
label7.setText("Punkte:");
label7.setVisible(false);
cp.add(label7);
label8.setBounds(64, 200, 32, 20);
label8.setText("200");
label8.setVisible(false);
label8.setAlignment(Label.RIGHT);
cp.add(label8);
button3.setBounds(112, 96, 72, 20);
button3.setLabel("Starten");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button3_ActionPerformed(evt);
}
});
button3.setVisible(true);
cp.add(button3);
label9.setBounds(8, 8, 176, 20);
label9.setText("user");
label9.setVisible(false);
cp.add(label9);
textField1.setBounds(8, 96, 75, 20);
textField1.setText("Name");
textField1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
textField1_MouseClicked(evt);
}
});
textField1.setForeground(Color.GRAY);
textField1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
textField1_KeyPressed(evt);
}
});
cp.add(textField1);
label10.setBounds(8, 64, 176, 19);
label10.setText("Bitte einen Namen eingeben:");
label10.setVisible(true);
cp.add(label10);
cp.setBackground(new Color(0xFFC800));
button4.setBounds(40, 144, 112, 40);
button4.setLabel("Beenden");
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button4_ActionPerformed(evt);
}
});
button4.setEnabled(true);
cp.add(button4);
label11.setBounds(8, 224, 176, 20);
label11.setText("");
label11.setBackground(Color.RED);
label11.setVisible(false);
label11.setForeground(Color.WHITE);
cp.add(label11);
button5.setBounds(112, 152, 73, 65);
button5.setLabel("Beenden");
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button5_ActionPerformed(evt);
}
});
button5.setEnabled(true);
button5.setVisible(false);
cp.add(button5);
jTextArea1ScrollPane.setBounds(8, 280, 176, 162);
jTextArea1.setText("Scores:\n\n");
jTextArea1ScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jTextArea1.setEditable(false);
cp.add(jTextArea1ScrollPane);
label12.setBounds(8, 416, 176, 19);
label12.setText("Datum");
label12.setVisible(false);
label12.setBackground(Color.YELLOW);
cp.add(label12);
button6.setBounds(20, 252, 152, 20);
button6.setLabel("Scores ein-/ ausblenden");
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button6_ActionPerformed(evt);
}
});
cp.add(button6);
// Ende Komponenten
setVisible(true);
}
// Anfang Methoden
public void abspeichern()
{
try
{
BufferedWriter writer=new BufferedWriter(new FileWriter("YYYYYY"));
writer.write(XXXXXXXX);
writer.close();
}
catch(IOException exception)
{
JOptionPane.showMessageDialog(null, "Es ist ein Fehler beim Speichern aufgetreten." );
}
}
public void button1_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
x=Mathe.randint(101);
System.out.println(x);
label11.setText("Zahl erzeugt!");
versuche=0;
punkte=200;
spiele++;
label6.setText(String.valueOf(spiele));
button1.setEnabled(false);
button2.setEnabled(true);
}
public void [B]button2_ActionPerformed[/B](ActionEvent evt) {
// TODO hier Quelltext einfügen
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
label12.setText(dateFormat.format(date));
int y;
y=numberField1.getInt();
versuche++;
if (numberField1.getText().length() == 0) {
GUI.showmessage("Bitte eine Zahl eingeben!");
}
if (y>x) {
label11.setText("Eingegebene Zahl ist zu groß!");
punkte=punkte-20;
}
else{
if (y<x) {
label11.setText("Eingabe ist zu klein!");
punkte=punkte-20;
}
else {
label4.setText(String.valueOf(versuche));
label8.setText(String.valueOf(punkte));
jTextArea1.append(label8.getText());
jTextArea1.append(" Punkte\n");
jTextArea1.append(label12.getText());
jTextArea1.append("\n---------------------------------------\n");
GUI.showmessage("RIIIICHTIIIIG!!!!! :DDDDDDDDD\nSie haben "+versuche+" Versuche benötigt\nund erhalten somit "+punkte+" Punkte!");
button2.setEnabled(false);
button1.setEnabled(true);
versuche=0;
punkte=200;
}
}
label4.setText (String.valueOf(versuche));
label8.setText (String.valueOf(punkte));
if (punkte==0) {
GUI.showmessage ("HAHA du LOSER, hast verloren");
button2.setEnabled(false);
button1.setEnabled(true);
}
}
public void numberField1_MouseClicked(MouseEvent evt) {
// TODO hier Quelltext einfügen
numberField1.clear();
}
public void button3_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
if (textField1.getText().length() == 0 || textField1.getText().equals("Name")) {
GUI.showmessage ("Bitte einen Namen eingeben!");
}
else {
user=textField1.getText();
numberField1.setVisible(true);
textField1.setVisible(false);
button1.setVisible(true);
button2.setVisible(true);
button3.setVisible(false);
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
label4.setVisible(true);
label5.setVisible(true);
label6.setVisible(true);
label7.setVisible(true);
label8.setVisible(true);
label10.setVisible(false);
label9.setText("Hallo "+user+"!");
label9.setVisible(true);
button4.setVisible(false);
button5.setVisible(true);
label11.setVisible(true);
}
}
public void textField1_MouseClicked(MouseEvent evt) {
// TODO hier Quelltext einfügen
textField1.setText("");
textField1.setForeground(Color.black);
}
public void button4_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
System.exit(0);
}
public void textField1_KeyPressed(KeyEvent evt) {
// TODO hier Quelltext einfügen
if(evt.getKeyCode() == KeyEvent.VK_ENTER){
if (textField1.getText().length() == 0 || textField1.getText().equals("Name")) {
GUI.showmessage("Bitte einen Namen eingeben!");
}
else {
user=textField1.getText();
numberField1.setVisible(true);
textField1.setVisible(false);
button1.setVisible(true);
button2.setVisible(true);
button3.setVisible(false);
label1.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
label4.setVisible(true);
label5.setVisible(true);
label6.setVisible(true);
label7.setVisible(true);
label8.setVisible(true);
label10.setVisible(false);
label9.setText("Hallo "+user+"!");
label9.setVisible(true);
button4.setVisible(false);
button5.setVisible(true);
label11.setVisible(true);
}
}
}
public void numberField1_[B]KeyPressed[/B](KeyEvent evt) throws IOException{
// TODO hier Quelltext einfügen
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
label12.setText(dateFormat.format(date));
if(evt.getKeyCode() == KeyEvent.VK_ENTER && button2.isEnabled()){
int y;
y=numberField1.getInt();
versuche++;
if (numberField1.getText().length() == 0) {
GUI.showmessage("Bitte eine Zahl eingeben!");
}
if (y>x) {
label11.setText("Eingegebene Zahl ist zu groß!");
punkte=punkte-20;
}
else{
if (y<x) {
label11.setText("Eingabe ist \n zu klein!");
punkte=punkte-20;
}
else {
label4.setText(String.valueOf(versuche));
label8.setText(String.valueOf(punkte));
jTextArea1.append(label8.getText());
jTextArea1.append(" Punkte\n");
jTextArea1.append(label12.getText());
jTextArea1.append("\n---------------------------------------\n");
GUI.showmessage("RIIIICHTIIIIG!!!!! :DDDDDDDDD\nSie haben "+versuche+" Versuche benötigt\nund erhalten somit "+punkte+" Punkte!");
button2.setEnabled(false);
button1.setEnabled(true);
versuche=0;
punkte=200;
abspeichern();
}
}
label4.setText (String.valueOf(versuche));
label8.setText (String.valueOf(punkte));
if (punkte==0) {
GUI.showmessage ("HAHA du LOSER, hast verloren");
button2.setEnabled(false);
button1.setEnabled(true);
}
}
}
public void button5_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
System.exit(0);
}
public void button6_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
if (getHeight() == 308) {
setSize(200,480);
}
else {
setSize(200,308);
}
}
// Ende Methoden
public static void main(String[] args) {
new Rate_die_Zahl("Rate_die_Zahl");
}
}