/*
* Created on 11.10.2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author tsluga
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Vector;
import java.util.List;
import java.util.Random;
import java.io.File;
import java.applet.Applet;
import java.applet.AudioClip;
public class Game1 extends JFrame implements WindowListener {
private MainScreen mainscreen = null;
private GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
private GraphicsDevice gs_device = environment.getDefaultScreenDevice();
public JLayeredPane layeredpane = this.getLayeredPane(); // Frame zum einstetzen
private ImageIcon background = new ImageIcon("sluga/hintergrund.jpg"); // Hintergrundbild laden
private JLabel backLabel = new JLabel(background); // Hintergrund in ein Label laden
private JButton ausgang = new JButton("Ausgang");
private Random rand = new Random();
private int tmp; // Zufällige Kombination für das richtige Tier
private int btn_tmp; // Wird für die Auswahl von Buttons benötigt
private int animal_rnd; // Wird für die Zufällige Wahl von einem Tier benötigt
/*
* Dies sind die 3 Buttons für das Spiel , in diese Buttons kommen
* die Tiere auf die man klicken kann !
*/
private JButton tier1 = new JButton();
private JButton tier2 = new JButton();
private JButton tier3 = new JButton();
/*
* Für die Buttons wird ein JLabel benötigt, in das
* ein Bild von einem Tier geladen wird.
*/
private JLabel tier1_label = new JLabel();
private JLabel tier2_label = new JLabel();
private JLabel tier3_label = new JLabel();
/*
* Für das Spiel werden 3 Variablen benötigt, eine davon enthält
* eine Zahl in der, der Button vom richtigen Spiel gespeichert ist
*/
private int button_1;
private int button_2;
private int button_3;
/*
* Hier werden nun alle Grafiken in einem Vector für das Spiel gespeichert,
* hier befinden sich aber nur die Grafiken für die Tiere, zu dennen es
* auch ein Geräusch gibt
* tiere_v steht für tiere_vorhanden also Tiere mit Geräuschen.
* Die einzelnen Bilder werden in der Methode fillVec() eingefügt
*/
private List tiere_v = new Vector();
private List tiere_a = new Vector();
private List tiere_b = new Vector();
/*
* Hier wird der richtige Button der im Zufallsmodus ausgesucht wird
* abgespeichert.
*/
private int richtiger_button;
/*
* Wird für das Abspielen der Wave Dateien benötigt
*/
File f = null;
/*
* zum löschen
*/
private JLabel label = new JLabel();
private int richtiges_tier;
public Game1(MainScreen mainscreen){
this.mainscreen = mainscreen;
if(gs_device.isFullScreenSupported()){
layeredpane.add(backLabel,new Integer(-30001));
this.getRootPane().setOpaque(true);
((JComponent) this.getContentPane()).setOpaque(false);
this.fillVec();
this.addAnimalButtons();
this.setUndecorated(true);
this.setSize(1024,768); // Größe einstellen
this.addWindowListener(this); // WindowListener einfügen
this.gs_device.setFullScreenWindow(this); // Fullscreen starten
this.ausgang.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Game1.this.tier1.remove(tier1_label);
Game1.this.tier2.remove(tier2_label);
Game1.this.tier3.remove(tier3_label);
Game1.this.mainscreen.setVisible(true);
Game1.this.mainscreen.midiplayer.startMusik();
Game1.this.setVisible(false);
}
});
}
try{
// DisplayMode einstellen
gs_device.setDisplayMode(new DisplayMode(1024,
768,
32,
gs_device.getDisplayMode().getRefreshRate()));
}catch(Exception ex){
gs_device.setFullScreenWindow(null);
}
}
/*
* Diese Methode füllt den Vector tier_v mit den Bildern
* der Tiere. Aus diesem Vector wird per Zufallsgenerator ein
* Bild mit Geräusch ausgewählt.
*
*/
private void fillVec(){
/*
* Hier wird der Vector mit den Tieren gefüllt,
* zu den es auch ein Geräusch gibt
*/
this.tiere_v.add(File.separatorChar+"sluga"+File.separatorChar+"images"+File.separatorChar+"tiere_v"+File.separatorChar+"affe.gif");
this.tiere_v.add("sluga/images/tiere_v/baer.gif");
this.tiere_v.add("sluga/images/tiere_v/elefant.gif");
this.tiere_v.add("sluga/images/tiere_v/hund.gif");
this.tiere_v.add("sluga/images/tiere_v/hahn.gif");
this.tiere_v.add("sluga/images/tiere_v/tiger.gif");
this.tiere_v.add("sluga/images/tiere_v/wolf.gif");
this.tiere_v.add("sluga/images/tiere_v/schwein.gif");
this.tiere_v.add("sluga/images/tiere_v/katze.gif");
this.tiere_v.add("sluga/images/tiere_v/kuh.gif");
this.tiere_v.add("sluga/images/tiere_v/schaf.gif");
this.tiere_v.add("sluga/images/tiere_v/frosch.gif");
this.tiere_v.add("sluga/images/tiere_v/pferd.gif");
this.tiere_v.add("sluga/images/tiere_v/ente.gif");
/*
* Hier wird der Vector gefüllt, mit den Tieren
* aus Ordner tiere_a, also der ersten Gruppe von Tieren,
* zu dennen es keine Tiergeräusche gibt
*/
this.tiere_a.add("sluga/images/tiere_a/hase1.gif");
this.tiere_a.add("sluga/images/tiere_a/krebs.gif");
this.tiere_a.add("sluga/images/tiere_a/nashorn.gif");
this.tiere_a.add("sluga/images/tiere_a/schild3.gif");
this.tiere_a.add("sluga/images/tiere_a/schlange.gif");
this.tiere_a.add("sluga/images/tiere_a/seepferdchen.gif");
this.tiere_a.add("sluga/images/tiere_a/spinne.gif");
this.tiere_a.add("sluga/images/tiere_a/spinne1.gif");
this.tiere_a.add("sluga/images/tiere_a/tintenfisch.gif");
/*
* Hier wird der Vector mit den Tieren aus dem Ordner
* tiere_b gefüllt
*/
this.tiere_b.add("sluga/images/tiere_b/eichhoernchen.gif");
this.tiere_b.add("sluga/images/tiere_b/fledermaus.gif");
this.tiere_b.add("sluga/images/tiere_b/fliege1.gif");
this.tiere_b.add("sluga/images/tiere_b/fliege2.gif");
this.tiere_b.add("sluga/images/tiere_b/fuchs.gif");
this.tiere_b.add("sluga/images/tiere_b/krabbe.gif");
this.tiere_b.add("sluga/images/tiere_b/krokodil.gif");
this.tiere_b.add("sluga/images/tiere_b/maus.gif");
this.tiere_b.add("sluga/images/tiere_b/muecke.gif");
this.tiere_b.add("sluga/images/tiere_b/nilpferd.gif");
this.tiere_b.add("sluga/images/tiere_b/schild1.gif");
this.tiere_b.add("sluga/images/tiere_b/schild2.gif");
this.tiere_b.add("sluga/images/tiere_b/schmetterling.gif");
this.tiere_b.add("sluga/images/tiere_b/scorpion.gif");
this.tiere_b.add("sluga/images/tiere_b/fisch.gif");
this.tiere_b.add("sluga/images/tiere_b/giraffe.gif");
}
public void randAnimals(){
this.tier1.remove(tier1_label);
this.tier2.remove(tier2_label);
this.tier3.remove(tier3_label);
/*
* Als erstes wird zufällig die Position des Tieres ausgesucht,
* das es zu erraten gibt, dieses wird dann in den Button geladen
*/
// 1. Einen der 3 Button zufällig aussuchen
this.tmp = this.rand.nextInt(3);
// 2. In den Richtigen Button packen
// 2.1. Button 1 wurde ausgesucht
System.out.println(this.tiere_v.isEmpty());
if(this.tmp == 0){
this.richtiger_button = 0; // Der Richtige Button ist der 1te
this.animal_rnd = this.rand.nextInt(this.tiere_v.size());
this.tier1_label = new JLabel(new ImageIcon(this.tiere_v.get(animal_rnd).toString()));
this.tier1.add(tier1_label);
this.richtiges_tier = this.animal_rnd;
// 2.1.1 Einen aus den übrigen Buttons aussuchen
this.btn_tmp = this.rand.nextInt(2);
// Falls Ergebnis 0, folgende Kombination
if(this.btn_tmp == 0){
// Zufälliges Tier für Button 2 aus tiere_a
this.animal_rnd = this.rand.nextInt(this.tiere_a.size());
this.tier2_label = new JLabel(new ImageIcon(this.tiere_a.get(animal_rnd).toString()));
this.tier2.add(tier2_label);
// Zufälliges Tier für Button 3 aus tiere_b
this.animal_rnd = this.rand.nextInt(this.tiere_b.size());
this.tier3_label = new JLabel(new ImageIcon(this.tiere_b.get(animal_rnd).toString()));
this.tier3.add(tier3_label);
}
else if(this.btn_tmp == 1){
// Zufälliges Tier für Button 2 aus tiere_b
this.animal_rnd = this.rand.nextInt(this.tiere_b.size());
this.tier2_label = new JLabel(new ImageIcon(this.tiere_b.get(animal_rnd).toString()));
this.tier2.add(tier2_label);
// Zufälliges Tier für Button 3 aus tiere_a
this.animal_rnd = this.rand.nextInt(this.tiere_a.size());
this.tier3_label = new JLabel(new ImageIcon(this.tiere_a.get(animal_rnd).toString()));
this.tier3.add(tier3_label);
}
}
// 2.1 Button 2 wurde ausgesucht
else if(this.tmp == 1){
this.richtiger_button = 1; // Der richtige Button ist der zweite
this.animal_rnd = this.rand.nextInt(this.tiere_v.size());
this.tier2_label = new JLabel(new ImageIcon(this.tiere_v.get(animal_rnd).toString()));
this.tier2.add(tier2_label);
this.richtiges_tier = this.animal_rnd;
// 2.1.1 Einen der übrigen Button aussuchen
this.btn_tmp = this.rand.nextInt(2);
// Falls Ergebnis 1
if(this.btn_tmp == 0){
// Für Button 1 ein zufälliges Tier aus tiere_a aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_a.size());
this.tier1_label = new JLabel(new ImageIcon(this.tiere_a.get(animal_rnd).toString()));
this.tier1.add(tier1_label);
// Für Button 3 ein zufälliges Tier aus tiere_b aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_b.size());
this.tier3_label = new JLabel(new ImageIcon(this.tiere_b.get(animal_rnd).toString()));
this.tier3.add(tier3_label);
}
else if(this.btn_tmp == 1){
// Für Button 1 ein Zufälliges Tier aus tiere_b aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_b.size());
this.tier1_label = new JLabel(new ImageIcon(this.tiere_b.get(animal_rnd).toString()));
this.tier1.add(tier1_label);
// Für Button 3 ein zufälliges Tier aus tiere_a ausschen
this.animal_rnd = this.rand.nextInt(this.tiere_a.size());
this.tier3_label = new JLabel(new ImageIcon(this.tiere_a.get(animal_rnd).toString()));
this.tier3.add(tier3_label);
}
}
// 3. Button wurde für das richtige Tier ausgesucht
else if(this.tmp == 2){
this.richtiger_button = 2; // Der richrige Button ist der dritte
this.animal_rnd = this.rand.nextInt(this.tiere_v.size());
this.tier3_label = new JLabel(new ImageIcon(this.tiere_v.get(animal_rnd).toString()));
this.tier3.add(tier3_label);
richtiges_tier = animal_rnd;
// Zufällige Tiere für Button 1 und 2 aussuchen
this.btn_tmp = this.rand.nextInt(2);
// Falls Button 1 ausgesucht
if(this.btn_tmp == 0){
// Zufälliges Tier aus tiere_a für Button 1 aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_a.size());
this.tier1_label = new JLabel(new ImageIcon(this.tiere_a.get(animal_rnd).toString()));
this.tier1.add(tier1_label);
// Zufälliges Tier aus tiere_b für Button 2 aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_b.size());
this.tier2_label = new JLabel(new ImageIcon(this.tiere_b.get(animal_rnd).toString()));
this.tier2.add(tier2_label);
}
else if(this.btn_tmp == 1){
// Zufälliges Tier aus tiere_b für Button 1 aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_b.size());
this.tier1_label = new JLabel(new ImageIcon(this.tiere_b.get(animal_rnd).toString()));
this.tier1.add(tier1_label);
// Zufälliges Tier aus tiere_a für Button 2 aussuchen
this.animal_rnd = this.rand.nextInt(this.tiere_a.size());
this.tier2_label = new JLabel(new ImageIcon(this.tiere_a.get(animal_rnd).toString()));
this.tier2.add(tier2_label);
}
}
this.playAnimalSound();
}
private void playAnimalSound(){
this.label.setBounds(100,100,100,80);
this.layeredpane.add(label);
// In der Methode randAnimals wurde der Affe ausgesucht
if(this.richtiges_tier == 0){
label.setText(tiere_v.get(richtiges_tier).toString());
}
else if(this.richtiges_tier == 1){
label.setText("Bär");
}
else if(this.richtiges_tier == 2){
f = new File("sluga/images/tiere_v/waves/elefant.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 3){
f = new File("sluga/images/tiere_v/waves/hund.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 4){
f = new File("sluga/images/tiere_v/waves/hahn.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 5){
f = new File("sluga/images/tiere_v/waves/tiger.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
sound.stop();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 6){
f = new File("sluga/images/tiere_v/waves/wolf.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 7){
f = new File("sluga/images/tiere_v/waves/schwein.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 8){
f = new File("sluga/images/tiere_v/waves/katze.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 9){
f = new File("sluga/images/tiere_v/waves/kuh.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 10){
f = new File("sluga/images/tiere_v/waves/schaf.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 11){
f = new File("sluga/images/tiere_v/waves/frosch.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 12){
f = new File("sluga/images/tiere_v/waves/pferd.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
else if(this.richtiges_tier == 13){
f = new File("sluga/images/tiere_v/waves/ente.wav");
try{
AudioClip sound = Applet.newAudioClip( f.toURL() );
sound.play();
}catch(Exception ex) {}
}
}
/*
* In dieser Methode werden die Buttons in Spiel eingefügt.
* In den Buttons befinden sich die Tiere
*/
public void addAnimalButtons(){
// Position der Buttons
this.tier1.setBounds(185,200,190,190);
this.tier2.setBounds(395,200,190,190);
this.tier3.setBounds(605,200,190,190);
// Hintergrund auf WHITE setzen
this.tier1.setBackground(Color.WHITE);
this.tier2.setBackground(Color.WHITE);
this.tier3.setBackground(Color.WHITE);
// In das Spiel einfügen
this.layeredpane.add(tier1);
this.layeredpane.add(tier2);
this.layeredpane.add(tier3);
// Actionlistener einfügen
this.tier1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(Game1.this.richtiger_button == 0){
Game1.this.randAnimals();
Game1.this.addGraphics();
}
}
});
this.tier2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(Game1.this.richtiger_button == 1){
Game1.this.randAnimals();
Game1.this.addGraphics();
}
}
});
this.tier3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(Game1.this.richtiger_button == 2){
Game1.this.randAnimals();
Game1.this.addGraphics();
}
}
});
}
public void addGraphics(){
// Größe vom Label initilisieren
backLabel.setSize(background.getIconWidth(),background.getIconHeight());
// Hintergrundbild setzen und ContentPane durchsichtig machen
layeredpane.setLayer(backLabel,-31001);
this.setVisible(true);
this.mainscreen.setVisible(false);
ausgang.setBounds(600,600,100,100);
this.layeredpane.add(ausgang);
}
public void removeGraphics(){
this.setVisible(false);
this.mainscreen.setVisible(true);
}
public void windowClosing( WindowEvent event ) {
System.exit( 0 );
}
public void windowClosed( WindowEvent event ) {}
public void windowDeiconified( WindowEvent event ) {}
public void windowIconified( WindowEvent event ) {}
public void windowActivated( WindowEvent event ) {}
public void windowDeactivated( WindowEvent event ) {}
public void windowOpened( WindowEvent event ) {}
}