import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Ballonspiel extends JApplet implements MouseListener{ private int iMausX1, iMausY1, iMausX2, iMausY2, xPos, yPos, xPosMax, yPosMax, iMaxAnzahlBallone; Thread t1, t2, t3, t4, t5; Dimension groesse; //Oberflächenelemte JPanel timePanel, scorePanel; //Das Spielfeld public static JPanel canvasPanel; JLabel timeLabel, canvasLabel, scoreLabel; JButton startButton; private Image pic; public void init() { System.out.println("Aufruf von INIT"); //Dimensionen ermitteln groesse = this.getSize(); xPosMax=groesse.width; yPosMax=groesse.height; //this.addMouseListener(this); Container inhalt = new Container(); inhalt = this.getContentPane(); timePanel = new BackgroundPanel(new ImageIcon("unten.jpg")); canvasPanel = new JPanel(); canvasPanel.setBackground(new java.awt.Color(255,255,60)); scorePanel = new JPanel(); timeLabel = new JLabel("Zeit"); scoreLabel = new JLabel("Punkte"); startButton = new JButton("START!"); timePanel.setLayout(new GridLayout(2,1)); timePanel.add(startButton); timePanel.setSize(new Dimension(500,400)); startButton.addMouseListener(this); scorePanel.setLayout(new FlowLayout()); canvasPanel.setLayout(null); timePanel.add(timeLabel); scorePanel.add(scoreLabel); inhalt.add(timePanel,BorderLayout.SOUTH); inhalt.add(canvasPanel,BorderLayout.CENTER); inhalt.add(scorePanel,BorderLayout.EAST); //Einlesen des Hintergrundbildes pic = Toolkit.getDefaultToolkit().getImage( "rb007.gif" ); } public void start() { //initialisieren der Threads t3= new Thread( new Ballon3(pic,yPosMax,75,this) ); t4= new Thread( new Ballon3(pic,yPosMax,165,this) ); //t2= new Thread(new Ballon3(pic,yPosMax,265,this)); //t5= new Thread( new Ballon3(pic,yPosMax,235,this) ); } public void stop() { } public void paint (Graphics g) { super.paint(g); } public synchronized void level1(int iAnzahlBallone) { System.out.println("Aufruf von level1" ); iMaxAnzahlBallone=iAnzahlBallone; //t1 = new Thread( new Ballon3(pic,yPosMax,95,this) ); //Starten der Threads // t1.start(); // t2.start(); t3.start(); t4.start(); //t5.start(); } public int getMaxAnzahlBallone() { return(iMaxAnzahlBallone); } public void pruefe() { } public void mouseClicked(MouseEvent e) { //Ereignissbehandlund für den "Start-Button" if(e.getSource()==startButton) { System.out.println("Aufruf Level1 aus 'mouseClicked'" ); //Aufruf von Level1 level1(6); } } public void mouseEntered (MouseEvent e) { } public void mouseExited (MouseEvent e) { } public void mousePressed (MouseEvent e) { } public void mouseReleased (MouseEvent e) { } ///************************************** //* * //* Innere Klasse für ein JPanel * //* mit Hintergrundbild * // * * //*************************************** class BackgroundPanel extends JPanel { ImageIcon icon; public BackgroundPanel(ImageIcon icon) { this.icon = icon; } protected void paintComponent(Graphics g) { super.paintComponent(g); if (this.icon != null) { g.drawImage(icon.getImage(), 0, 0, this); } } public Dimension getPreferredSize() { if (icon != null) { return new Dimension(icon.getIconWidth(), icon.getIconHeight()); } else { return super.getPreferredSize(); } } } // ///***********************************+ // class MausListener extends MouseAdapter // { // // public void MouseClicked(MouseEvent e) // { // System.out.println("->>"+e.getSource().toString() ); // } // // } // //}
import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Ballon3 extends JPanel implements Runnable,MouseListener{ private JLabel bildLabel; private Image bild, icon; private JPanel Spielfeld; private int width, height, ixPunkt, iyPunkt, yPos, yPosMax, xPos, iMaxAnzahlBallone, iGeschwindigkeit; private static int iAnzahlBallone1; private static int iAnzahlBallone2; private Image geplatztI; private boolean isGeplatzt=false, isVerloren=false; Ballonspiel spiel; public Ballon3(Image bild, int yPosMax, int xPos,Ballonspiel spiel) { this.bild=bild; this.spiel=spiel; this.yPosMax=yPosMax; this.xPos=xPos; width=bild.getWidth(this); height= bild.getHeight(this); this.setSize(bild.getWidth(this), bild.getHeight(this)); bildLabel=new JLabel(new ImageIcon(bild)); this.add(bildLabel); this.setBackground(new java.awt.Color(255,255,60)); this.addMouseListener(this); Ballonspiel.canvasPanel.add(this); this.setBounds(xPos,yPos,width,height); //Einlesen des "geplatzt"-Bildes geplatztI=Toolkit.getDefaultToolkit().getImage( "boom.gif" ) ; } public synchronized void run() { iGeschwindigkeit=(int)(Math.random()*100); iAnzahlBallone1++; if(!this.isVisible()) this.setVisible(true); for( yPos=0;yPos<yPosMax && !isGeplatzt;yPos+=3) { this.setBounds(xPos,yPos,width,height); try { Thread.sleep(35); // normal: (iGeschwindigkeit); zum Testen: (35); } catch (Exception ex) { System.out.println(ex ); } Ballonspiel.canvasPanel.repaint(); } if(isGeplatzt) { platzen(); } else if(yPos==yPosMax && iAnzahlBallone1 <= spiel.getMaxAnzahlBallone()) { iAnzahlBallone2++; System.out.println(">>>>>>>>>>>>>>>>Strafe! Nr. " +iAnzahlBallone2); // hier Neustart des Threads } } public void bewegen(int x, int y) { //bewegen über "setBounds" this.setBounds(x,y,width,height); } //platzen des Ballons public void platzen() { isGeplatzt=true; // "geplatzt"-Bild setzen bildLabel.setIcon( new ImageIcon(geplatztI)); try { Thread.sleep(1000); } catch(Exception e) { System.out.println(e); } this.setVisible(false); // if(iAnzahlBallone < spiel.getMaxAnzahlBallone()) // { // isGeplatzt=false; // bildLabel.setIcon(new ImageIcon(bild)); // -> hier Neustart // } // spiel.pruefe() } public void mouseClicked(MouseEvent e) { //platzen lassen auf Mausklick isGeplatzt=true; } public void mouseEntered (MouseEvent e) { } public void mouseExited (MouseEvent e) { } public void mousePressed (MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }