carookee - group communication for you
Home / JavaForum / Networking
Infos   |   Features   |   Gold-Edition   |   Kundenservice   
java
  Übersicht
  Forum
Beginner
Java allgemein
JDBC
JNI
Networking
Online-Ressourcen
Swing + AWT
XML
Meckerecke
  Mitglieder
LOGIN





· Passwort vergessen
· Kostenlos anmelden
  Information
  Demo
  Features
  Im Vergleich
  Anmeldung
SUCHE
Beiträge, Foren oder Verfasser finden:
  Kundenservice
  Impressum
  Datenschutz
  AGB
Status
4.693 User online
1 User eingeloggt
 

Beiträge
   Antworten     Neuer Beitrag    

Beitrag 38 von 610 (6%) |<   <   >   >|

Autor Halo_Player
Datum 14.10.05, 23:03
Betreff Problem bei Java-Applet-Spiel-Programmierung(moorhuhnartiges Spiel)


Hallo ich hoffe ihr könnt mir bei meinem Problem helfen.
Ich bin dabei ein "moorhuhnspiellartiges" Applet zu programmieren. Auf dem Bildschirm wandern 5 gleiche Bilder
(Array) umher.Bei Mausklick auf eines der Bilder, wird ein Counter hochgezählt -> Trefferanzahl,das angeklickte bild wird gelöscht und ein neus bild gezeichnet.
Dieser Teil des Applet funktioniert.
Jetzt ist im unteren Bereich des Bildschirms eine mit "menüartige" Tabelle mit fünf verschiedenen Bildern gezeichnet. Klickt man auf eins der Bilder soll der Cursur verändert werden, bzw. genauso wie das bild aussehen.
Das funktioniert noch nicht so ganz, sprich das Menü wird zwar dargestellt, bei einem Klick geschieht jedoch nichts.
Der Code hat keine Syntaxfehler. Und von meiner Logik stimmts ;) Der Quelltext ist recht komplex, deswegen hab ich mich auf Kommentare die im Bezug auf das Bildermenü wichtig sind beschränkt. Der Rest geht ja schon.
Ich hoffe ihr könnt mir helfen!
Danke schonmal im Vorraus für die Mühe!

Mfg Halo_Player

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.Toolkit.*;
import java.applet.AudioClip;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class SplashShooter extends Applet {
    int x1;
    int y1;
    int x2;
    int y2;
    int x3; // Ganzahlvariablen
    int y3;
    int x4;
    int y4;
    int x5;
    int y5;
    
    private Image dbImage; //DoubleBuffer
    private Graphics dbg;
    
    Image scheisse, flasche, dose, granate, tomate; // Bildvariablen für das Menü(bzw. die Cursor)
    Image hintergrund; //hintergrundbild
    
    
    Cursor cursorFlasche, cursorDose, cursorGranate, cursorTomate,
    cursorScheisse; //Cursorvariablen
    
    Schuss curAnim=new Schuss(); //Objekt der Klasse Schuss(für Parameterübergabe)
    
    int delay = 50; // timernoistartzeit(bewegung)
    javax.swing.Timer los = new javax.swing.Timer(delay, new starterAlle()); // timer
    
    Image huhn; // image für das bild das über den Hintergrund wandert
    
    Moorhuhn[] hennen = new Moorhuhn[5]; //Array [ max. moorhuhnzahl ] // Bilder Array (für die Moorhühner)
    AudioClip schuss, anfang; // ton
    int bodycount = 0;
    Label counter = new Label("Los geht's!!! - Wähle deine Waffe aus: "); // Counter
    
    public void init() {
        
        
        
        anfang = getAudioClip(getCodeBase(), "doomsday.au"); //Hintergrundmelodie
        anfang.play();
        
        huhn = getImage(getCodeBase(), "huhn2.gif"); // bild für das Array wird geladen
        
        setSize(1024, 768); // appletgröße
        
        setBackground(Color.black);
        setForeground(Color.white);
        
        hintergrund = getImage(getCodeBase(), "stage.gif"); // hintergrundbild wird geladen(imageobjekt)
        
        scheisse = getImage(getCodeBase(), "Scheisshaufen.gif");
        flasche = getImage(getCodeBase(), "flasche.gif"); // Bilder für das Bildermenü wird geladen(imageobjekt)
        tomate = getImage(getCodeBase(), "Tomate.gif");
        granate = getImage(getCodeBase(), "Granate.gif");
        dose = getImage(getCodeBase(), "Dose.gif");
        
        cursorFlasche = getToolkit().createCustomCursor(flasche, new Point(0, 0), "Cursor");
        cursorScheisse = getToolkit().createCustomCursor(scheisse,new Point(0, 0), "Cursor");
        cursorDose = getToolkit().createCustomCursor(dose, new Point(0, 0), "Cursor"); //Cursor mit den einzelnen
        cursorGranate = getToolkit().createCustomCursor(granate, new Point(0, 0), "Cursor"); // einzelnen Bildern
        cursorTomate = getToolkit().createCustomCursor(tomate, new Point(0, 0), "Cursor");
        
        add(counter);
        
        for (int i = 0; i < hennen.length; i++) {
            hennen[i] = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate); // Parameterübergabe der Bilder an
            // Klasse Moorhuhn
        }
        
        los.start();
        addMouseListener(new Schuss());
    }
    
    public void start() {
    }
    
    public void paint( Graphics g ) {
        x1 = this.getWidth() * 1 / 5 - 100;
        y1 = this.getHeight() - 65;
        x2 = this.getWidth() * 2 / 5 - 125;
        y2 = this.getHeight() - 60;
        x3 = this.getWidth() * 3 / 5 - 135; //Die jeweilige x und y-Koordinaten der Bilder(also der Punkt links oben)
        y3 = this.getHeight() - 68;
        x4 = this.getWidth() * 4 / 5 - 125;
        y4 = this.getHeight() - 60;
        x5 = this.getWidth() * 5 / 5 - 140;
        y5 = this.getHeight() - 70;
        
        counter.setBounds(this.getWidth() * 1 / 5 + 20, this.getHeight() - 100,
        250, 20);
        
        g.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 24));// font
        // erzeugen
        // +
        // erstellen
        
        g.drawString( "Du befindest dich auf dem Splash - Deine Lieblingsrapper sind auf der Bühne: ",45, 50);
        
        //Überschrift
        
        g.drawLine(50, this.getHeight() - 10, this.getWidth() - 50, this
        .getHeight() - 10);
        g.drawLine(50, this.getHeight() - 80, this.getWidth() - 50, this
        .getHeight() - 80);
        g.drawLine(50, this.getHeight() - 10, 50, this.getHeight() - 80);
        g.drawLine(this.getWidth() - 50, this.getHeight() - 10,
        this.getWidth() - 50, this.getHeight() - 80);
        g.drawLine(this.getWidth() * 1 / 5, this.getHeight() - 10, this
        .getWidth() * 1 / 5, this.getHeight() - 105);
        g.drawLine(this.getWidth() * 2 / 5, this.getHeight() - 10, this
        .getWidth() * 2 / 5, this.getHeight() - 80);
        g.drawLine(this.getWidth() * 3 / 5, this.getHeight() - 10, this // Rahmen für das Menü
        .getWidth() * 3 / 5, this.getHeight() - 80);
        g.drawLine(this.getWidth() * 4 / 5, this.getHeight() - 10, this
        .getWidth() * 4 / 5, this.getHeight() - 105);
        g.drawLine(this.getWidth() * 1 / 5, this.getHeight() - 105, this
        .getWidth() * 4 / 5, this.getHeight() - 105);
        // _______
        g.drawImage(flasche, x1, y1, this); // Bilder für das Menü |Counter|
        g.drawImage(scheisse, x2, y2, this); // --------------------------------------------
        g.drawImage(tomate, x3, y3, this);
        g.drawImage(granate, x4, y4, this); // | Bild1 | Bild2 | Bild3 | Bild4 | Bild5 |
        g.drawImage(dose, x5, y5, this);
        // --------------------------------------------
        
        g.drawImage(hintergrund, 130, 70, this); // Hintergrungbild wrid gezeichnet
        
        
        
        
        
        
        counter.setBounds(this.getWidth() * 1 / 5 + 20, this.getHeight() - 100, 250, 20);
        
        
        for (int i = 0; i < hennen.length; i++) {
            hennen[i].paint(g);
        }
    }
    
    public void update( Graphics g ) {
        // Initialisierung des DoubleBuffers
        if (dbImage == null) {
            dbImage = createImage(this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics();
        }
        
        // Bildschirm im Hintergrund löschen
        dbg.setColor(getBackground());
        dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
        
        // Auf gelöschten Hintergrund Vordergrund zeichnen
        dbg.setColor(getForeground());
        paint(dbg);
        
        // Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm
        // anzeigen
        g.drawImage(dbImage, 0, 0, this);
        
        
        
    }
    public void cursorSet(){ // Der Cursor soll hier verändert werden
        
        if(curAnim.random()==1){setCursor(cursorFlasche);} //Je nachdem welche zahl von der Anfrage
        else if(curAnim.random()==2){setCursor(cursorGranate);} //random(), in der Klasse Schuss() zurückgegeben
        else if(curAnim.random()==3){setCursor(cursorDose);} //wird, wird der Cursor geändert
        else if(curAnim.random()==4){setCursor(cursorScheisse);}
        else if(curAnim.random()==5){setCursor(cursorTomate);};
        
        
        
        
    }
    
    class starterAlle implements ActionListener { // timeraction
        public void actionPerformed( ActionEvent evt ) {
            for (int i = 0; i < hennen.length; i++) {hennen[i].move();
            }
            repaint();
        }
    }
    
    class Schuss implements MouseListener {
        
        int cursorRandom;
        Moorhuhn booleans = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate);
        // Objekt der klasse Moorhuhn
        
        
        public void mousePressed( MouseEvent e ) {
            int xg = e.getX(); // Mauskoordinaten
            int yg = e.getY();
            
            booleans.flascheClick(xg, yg);
            booleans.granateClick(xg, yg);
            booleans.doseClick(xg, yg); //die Booleans in der Klasse Moohuhn werden aufgerufen
            booleans.flascheClick(xg, yg);
            booleans.scheisseClick(xg, yg);
            
            if (booleans.flascheClick(xg, yg) ) { // wen der boolean wahr ist ....
                booleans = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate); //Parameterübergabe an Moorhuhn
                cursorRandom = 1; //Wert für anfrage random()
                
                } else if (booleans.granateClick(xg, yg)) { // wen der boolean wahr ist ....
                booleans = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate); //Parameterübergabe an Moorhuhn
                cursorRandom =2; //Wert für anfrage random()
                
                
                } else if (booleans.doseClick(xg, yg)) { // wen der boolean wahr ist ....
                booleans = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate); //Parameterübergabe an Moorhuhn
                cursorRandom =3; //Wert für anfrage random()
                
                
                } else if (booleans.scheisseClick(xg, yg)) { // wen der boolean wahr ist ....
                booleans = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate); //Parameterübergabe an Moorhuhn
                cursorRandom =4; //Wert für anfrage random()
                
                } else if (booleans.tomateClick(xg, yg)) { // wen der boolean wahr ist ....
                booleans = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate); //Parameterübergabe an Moorhuhn
            cursorRandom =5; }; //Wert für anfrage random()
            
            
            for (int i = 0; i < hennen.length; i++) {
                
                if (hennen[i].erschossen(xg, yg)) {
                    hennen[i] = new Moorhuhn(huhn,scheisse, flasche, dose, granate, tomate);
                    bodycount++;
                    counter.setText(bodycount + " Sidos");
                    if (bodycount >= 100) {
                        counter.setText("100 Sidos- Ausrottung erfolgreich");
                    }
                    ;
                }
            }
            schuss.play();
        }
        
        public void mouseReleased( MouseEvent e ) {
        }
        
        public void mouseEntered( MouseEvent e ) {
        }
        
        public void mouseExited( MouseEvent e ) {
        }
        
        public void mouseClicked( MouseEvent e ) {
            
            
            
            
            }public int random(){ return cursorRandom;}// Die anfrage Random gibt einen Wert zurück,
        } // der dem jeweiligen Bild das angeklickt entspricht
        
        class Moorhuhn extends Canvas implements ActionListener {
            
            static Random r = new Random();
            
            javax.swing.Timer weiter;
            
            Image h2,zScheisse, zFlasche, zDose, zGranate, zTomate; // neue Bildernamen
            
            int x = -30;
            
            int y = 0;
            int x1;
            int y1;
            int x2;
            int y2;
            int x3; // Ganzahlvariablen
            int y3;
            int x4;
            int y4;
            int x5;
            int y5;
            int richtung;
            
            private Image dbImage; //DoubleBuffer
            private Graphics dbg;
            
            public Moorhuhn( Image h,Image pScheisse,Image pFlasche,Image pDose,Image pGranate ,Image pTomate) {
                // Parmeterübergabe der Bilder
                
                x1 = this.getWidth() * 1 / 5 - 100;
                y1 = this.getHeight() - 65;
                x2 = this.getWidth() * 2 / 5 - 125;
                y2 = this.getHeight() - 60;
                x3 = this.getWidth() * 3 / 5 - 135; //Die jeweilige x und y-Koordinaten der Bilder(also der Punkt links oben)
                y3 = this.getHeight() - 68;
                x4 = this.getWidth() * 4 / 5 - 125;
                y4 = this.getHeight() - 60;
                x5 = this.getWidth() * 5 / 5 - 140;
                y5 = this.getHeight() - 70;
                
                h2 = h;
                zScheisse = pScheisse;
                zFlasche = pFlasche;
                zDose = pDose; //Werte werden den neuen Bildervariablen zugeteilt
                zGranate = pGranate;
                zTomate = pTomate ;
                
                y = zufall(380);
                richtung = zufall(2) % 2;
                if (richtung == 0) {
                    x = (-30);
                }
                if (richtung == 1) {
                    x = 530;
                }
            }
            
            public void paint( Graphics g2 ) {
                g2.drawImage(h2, x, y, this);
            }
            
            public void update( Graphics g2 ) {
                y = zufall(380);
                g2.drawImage(h2, x, y, this);
                
                // Initialisierung des DoubleBuffers
                if (dbImage == null) {
                    dbImage = createImage(this.getSize().width, this.getSize().height);
                    dbg = dbImage.getGraphics();
                }
                
                // Bildschirm im Hintergrund löschen
                dbg.setColor(getBackground());
                dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
                
                // Auf gelöschten Hintergrund Vordergrund zeichnen
                dbg.setColor(getForeground());
                paint(dbg);
                
                // Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm
                // anzeigen
                g2.drawImage(dbImage, 0, 0, this);
                
            }
            
            public static int zufall( int grenze ) {return r.nextInt(grenze);
            }
            
            public void move() {
                if (richtung == 0) {
                    x = x + 5;
                }
                if (richtung == 1) {
                    x = x - 5;
                }
                
                y = y + zufall(10) - 5;
                if (x > 530) {
                    x = -30;
                }
                if (x < -30) {
                    x = 500;
                }
                if (y > 399) {
                    y = 0;
                }
                if (y < -20) {
                    y = 399;
                }
            }
            //Booleans für die Abfrage, ob ein Bild angeklickt wurde
            public boolean flascheClick(int xs, int ys) {
                if (x1 <= xs &&xs <= (x1 + zFlasche.getWidth(this)) // Wenn x-Koordinate vom bild <= Maus-X und Maus-X
                && y1 <= ys && ys <= (y1 + zFlasche.getHeight(this))) { // <= x-Koordinate vom bild + Bildbreite und
                    // Wenn y-Koordinate vom bild <= Maus-Y und Maus-Y
                    // <= x-Koordinate vom bild + Bildbreite
                    return true;
                    } else {
                    return false;
                }
            }
            
            public boolean scheisseClick(int xs, int ys) {
                if (x2 <= xs && xs <= (x2 + zScheisse.getWidth(this))
                && y2 <= ys && ys <= (y2 + zScheisse.getHeight(this))) {
                    return true;
                    } else {
                    return false;
                }
            }
            
            public boolean tomateClick(int xs, int ys) {
                if (x3 <= xs && xs <= (x3 + zTomate.getWidth(this)) && y3 <= ys
                &&ys <= (y3 + zTomate.getHeight(this))) {
                    return true;
                    } else {
                    return false;
                }
            }
            
            public boolean granateClick(int xs, int ys) {
                if (x4 <= xs&& xs <= (x4 + zGranate.getWidth(this))
                && y4 <=ys&& ys <= (y4 + zGranate.getHeight(this))) {
                    return true;
                    } else {
                    return false;
                }
            }
            
            public boolean doseClick(int xs, int ys) {
                if (x5 <= xs &&xs<= (x5 + zDose.getWidth(this)) && y5 <= ys
                &&ys <= (y5 + zDose.getHeight(this))) {
                    return true;
                }
                
                else {
                    return false;
                }
            }
            public boolean erschossen( int xs, int ys ) {
                if (x <= xs && xs <= (x + h2.getWidth(this)) && y <= ys
                && ys <= (y + h2.getHeight(this))) {
                    return true;
                    } else {
                    return false;
                }
            }
            
            public void noi() {
                weiter = new javax.swing.Timer(zufall(10000), this);
                weiter.start();
            }
            
            public void actionPerformed( ActionEvent e ) {
                move();
                weiter.stop();
            }
        }





 Auf diesen Beitrag antworten
 Neuen Beitrag verfassen


|<   <   >   >|

                                                                                                                                                                                                                           

Impressum  · Datenschutz  · AGB  · Infos  · Presse
Ein modernes Forum: teamturn.com