Variablen in einer anderen Klasse auf Änderungen überwachen

purzel

Bekanntes Mitglied
Hallo,

ich sitze jetzt schon seit Stunden an meinem Problem, komme irgendwie nicht weiter. Folgendes habe ich geplant:

Klasse A:

Überprüfung ob variable 1 aus Klasse B folgenden Wert enthält, dann führe dies und das aus

Klasse B:

Variable 1 wird geändert durch if/else etc. Prüfungen
Bereitgestellt durch getter/setter Methoden


Nun würde ich gerne wissen wie ich die Variable 1 in der Klasse A überprüfen kann ob diese in der Klasse B geändert wurde. Ich hoffe ihr versteht mein Problem.

Vllt. gibt es ja auch elegantere Methoden dazu. Ich hoffe es kann mir jemand helfen. 🙂

Hier mein QT:

Klasse PingPong (A):
Java:
package pingpong;

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class PingPong extends JApplet implements Runnable {
	private static final long serialVersionUID = 1L;
	LayoutManager layout;
	Thread thread;
	
	public void init() {
		
	}
	
	public void start() {
		if(thread == null) {
	        thread = new Thread(this);
	        thread.start();
	    }
		
	layout = new GridLayout(0, 1);
        setLayout(layout);
        
        MainDrawing draw = new MainDrawing();
        add(draw);
		System.out.println("getGamestatus: " + draw.getGamestatus());
		if(draw.getGamestatus() == 0) {
			System.out.println("Spiel zu Ende!");
			add(new JLabel("Ihr Name: "));
			add(new JTextField(""));
		}
	}
	
	@SuppressWarnings("deprecation")
	public void stop() {
		if(thread != null && thread.isAlive()) {
			thread.stop();
			thread = null;
		}
	}
	
	public void destroy() {
		
	}

	public void run() {
		while(true) {
			repaint();
			try {
				Thread.sleep(15);
			} catch(InterruptedException e) {
				
			}
		}
	}
}

Klasse MainDrawing (B):
Java:
package pingpong;

import java.awt.Event;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class MainDrawing extends JPanel implements MouseListener, KeyListener {
	private static final long serialVersionUID = 1L;
	private Image dbImage;
	private Graphics dbg;
	int kugel_xpos = 0;
	int kugel_ypos = 0;
	boolean kugel_direction_xpos = true;
	boolean kugel_direction_ypos = true;
	int balken_hoehe = 50;
	int balken_xpos = 5;
	int balken_ypos = 5;
	int points = -1;
	int counter;
	int gamestatus = -1;
	
	public MainDrawing() {
	    addMouseListener((MouseListener)this);
	    addKeyListener((KeyListener)this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		//g2d.drawRect(balken_xpos, balken_ypos, 20, 20);
		if(gamestatus == -1) {
			gamestop(g2d);
		} else if(gamestatus == 1) {
			gamestart(g2d);
			for(int i = 999999; i >= 0; i--) {
				counter -= i;
				if(gamestatus == 1 && counter == 0) {
					gamestatus = 0;
				}
			}
		} else if(gamestatus == 0) {
			gameend(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 mouseClicked(MouseEvent e) {
		if(gamestatus == -1 && e.getClickCount() == 2) {
			gamestatus = 1;
		}
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}
	
	public void keyPressed(KeyEvent e) {
		// Taste nach oben
	    if (e.getKeyCode() == 38) {
	      balken_ypos -= 1;
	    // Taste nach unten
	    } else if (e.getKeyCode() == 40) {
	      balken_ypos += 1;
	    } else {
	    	System.out.println("Key: " + e.getKeyCode());
	    }
	}

	public void keyTyped(KeyEvent e) {
		
	}

	public void keyReleased(KeyEvent e) {
		
	}
	
	public void gamestart(Graphics g) {
		g.drawRect(0, 0, 153, 153);
		g.drawString(points + " Punkte", 153 / 2, 15);
		g.drawString(counter + " s", 153 / 2, 25);
		//g.drawString("Balken Startposition y: " + balken_ypos, 25, 25);
		//g.drawString("Balken Endposition: " + (balken_ypos + balken_hoehe), 25, 35);
		g.fillRect(balken_xpos, balken_ypos, 15, balken_hoehe);
		g.fillOval(kugel_xpos, kugel_ypos, 15, 15);
		if(kugel_direction_xpos == true) {
			kugel_xpos += 1;
			if(kugel_xpos == 140) {
				kugel_direction_xpos = false;
			}
		} else {
			kugel_xpos -= 1;
			if(kugel_xpos == balken_xpos + 14) {
					kugel_direction_xpos = true;
			}
		}
		if(kugel_direction_ypos == true) {
			kugel_ypos += 1;
			if(kugel_ypos == 140) {
				kugel_direction_ypos = false;
			}
		} else {
			kugel_ypos -= 1;
			
			if(kugel_ypos == 0) {
				kugel_direction_ypos = true;
			}
		}
		if(kugel_ypos > balken_ypos && kugel_ypos < (balken_ypos + balken_hoehe) && kugel_xpos == balken_xpos + 14) {
			points++;
		} else if(kugel_ypos != balken_ypos) {
			
		}
	}
	
	public void gamestop(Graphics g) {
		g.drawString("Bitte Spiel mit Doppelklick starten!", 0, 153 / 2);
	}
	
	public void gameend(Graphics g) {
		g.drawString("Das Spiel ist zu Ende, ", 10, 75);
		g.drawString("ihre Punkte: " + points, 10, 90);
	}
	
	public int getGamestatus() {
		return gamestatus;
	}
	
	public void setGamestart(int gamestatus) {
		this.gamestatus = gamestatus;
	}
}

Nun soll ich klasse "PingPong" die Variable "gamestatus" der Klasse "MainDrawing" überprüft werden und bei änderung ausgegeben werden.

LG

Purzel
 
Ich habe das jetzt versucht umzusetzen, folgender QT besteht jetzt:

PingPong.java
Java:
package pingpong;

import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class PingPong extends JApplet implements Runnable, Observer {
	private static final long serialVersionUID = 1L;
	LayoutManager layout;
	Thread thread;
	
	public void update(Observable beobachtbarer, Object text) {
		System.out.println(text);
	}
	
	public void init() {
		
	}
	
	public void start() {
		if(thread == null) {
	        thread = new Thread(this);
	        thread.start();
	    }
		
		layout = new GridLayout(0, 1);
        setLayout(layout);
        
        MainDrawing draw = new MainDrawing();
        add(draw);
		System.out.println("getGamestatus: " + draw.getGamestatus());
		
		
		
		if(draw.getGamestatus() == 0) {
			System.out.println("Spiel zu Ende!");
			add(new JLabel("Ihr Name: "));
			add(new JTextField(""));
		}
	}
	
	@SuppressWarnings("deprecation")
	public void stop() {
		if(thread != null && thread.isAlive()) {
			thread.stop();
			thread = null;
		}
	}
	
	public void destroy() {
		
	}

	public void run() {
		while(true) {
			repaint();
			try {
				Thread.sleep(15);
			} catch(InterruptedException e) {
				
			}
		}
	}
}

MainDrawing.java
Java:
package pingpong;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class MainDrawing extends JPanel implements MouseListener, KeyListener {
	private static final long serialVersionUID = 1L;
	private Image dbImage;
	private Graphics dbg;
	int kugel_xpos = 0;
	int kugel_ypos = 0;
	boolean kugel_direction_xpos = true;
	boolean kugel_direction_ypos = true;
	int balken_hoehe = 50;
	int balken_xpos = 5;
	int balken_ypos = 5;
	int points = -1;
	int counter;
	int gamestatus = -1;
	
	public MainDrawing() {
		addMouseListener((MouseListener)this);
		addKeyListener((KeyListener)this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		//g2d.drawRect(balken_xpos, balken_ypos, 20, 20);
		if(gamestatus == -1) {
			gamestop(g2d);
		} else if(gamestatus == 1) {
			gamestart(g2d);
			for(int i = 999999; i >= 0; i--) {
				counter -= i;
				if(gamestatus == 1 && counter == 0) {
					gamestatus = 0;
					
				}
			}
		} else if(gamestatus == 0) {
			gameend(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 mouseClicked(MouseEvent e) {
		if(gamestatus == -1 && e.getClickCount() == 2) {
			gamestatus = 1;
		}
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}
	
	public void keyPressed(KeyEvent e) {
		// Taste nach oben
	    if (e.getKeyCode() == 38) {
	      balken_ypos -= 1;
	    // Taste nach unten
	    } else if (e.getKeyCode() == 40) {
	      balken_ypos += 1;
	    } else {
	    	System.out.println("Key: " + e.getKeyCode());
	    }
	}

	public void keyTyped(KeyEvent e) {
		
	}

	public void keyReleased(KeyEvent e) {
		
	}
	
	public void gamestart(Graphics g) {
		g.drawRect(0, 0, 153, 153);
		g.drawString(points + " Punkte", 153 / 2, 15);
		g.drawString(counter + " s", 153 / 2, 25);
		//g.drawString("Balken Startposition y: " + balken_ypos, 25, 25);
		//g.drawString("Balken Endposition: " + (balken_ypos + balken_hoehe), 25, 35);
		g.fillRect(balken_xpos, balken_ypos, 15, balken_hoehe);
		g.fillOval(kugel_xpos, kugel_ypos, 15, 15);
		if(kugel_direction_xpos == true) {
			kugel_xpos += 1;
			if(kugel_xpos == 140) {
				kugel_direction_xpos = false;
			}
		} else {
			kugel_xpos -= 1;
			if(kugel_xpos == balken_xpos + 14) {
					kugel_direction_xpos = true;
			}
		}
		if(kugel_direction_ypos == true) {
			kugel_ypos += 1;
			if(kugel_ypos == 140) {
				kugel_direction_ypos = false;
			}
		} else {
			kugel_ypos -= 1;
			
			if(kugel_ypos == 0) {
				kugel_direction_ypos = true;
			}
		}
		if(kugel_ypos > balken_ypos && kugel_ypos < (balken_ypos + balken_hoehe) && kugel_xpos == balken_xpos + 14) {
			points++;
		} else if(kugel_ypos != balken_ypos) {
			
		}
	}
	
	public void gamestop(Graphics g) {
		g.drawString("Bitte Spiel mit Doppelklick starten!", 0, 153 / 2);
	}
	
	public void gameend(Graphics g) {
		g.drawString("Das Spiel ist zu Ende, ", 10, 75);
		g.drawString("ihre Punkte: " + points, 10, 90);
	}
	
	public int getGamestatus() {
		return gamestatus;
	}
	
	public void setGamestart(int gamestatus) {
		this.gamestatus = gamestatus;
	}
	
	GameStatus gs = new GameStatus();
	gs.setGameStatus(gamestatus); <== [COLOR="Red"]Multiple markers at this line
	- Syntax error on token "gamestatus", VariableDeclaratorId expected after this 
	 token
	- Syntax error on token(s), misplaced construct(s)[/COLOR]
}

GameStatus.java
Java:
package pingpong;

import java.util.Observable;

public class GameStatus extends Observable {
	private int gamestatus;
	
	public GameStatus() {
		super();
	}
	
	public void setGameStatus(int gamestatus) {
		this.gamestatus = gamestatus;
		super.setChanged();
		super.notifyObservers(gamestatus);
	}
	
	public int getGamestatus() {
		return gamestatus;
	}
}

Wieso bekomme ich in der Klasse MainDrawing diesen Fehler(siehe QT!)?

LG

Purzel
 
Du kannst keine Anweisungen "einfach so" in die Klasse schreiben; dort gehören (fast) nur Variablen- und Methodendeklarationen hin; irgendwelche Anweisungen wie gs.setGameStatus( gamestatus) gehören in eine Methode.
 
Du kannst keine Anweisungen "einfach so" in die Klasse schreiben; dort gehören (fast) nur Variablen- und Methodendeklarationen hin; irgendwelche Anweisungen wie gs.setGameStatus( gamestatus) gehören in eine Methode.

Danke für den Tipp, klappt super!

Nur ein Problem habe ich noch:

in PingPong.java:
Java:
public void update(Observable beobachtbarer, Object gs) {
		System.out.println("Beobachter " + nr + " meldet: Text = " + gs);
		gamestatus = (Integer)gs;
		if(gamestatus == 0 && nr == 2) {
			System.out.println("Spiel zu Ende!");
			
		} 
	}

da kann ich prüfen ob der Wert stimmt, wie kann ich aber ausserhalb von "update" den Wert prüfen? Erhalt da keine Ausgabe ???:L
 
Und wie?! Andere haben vllt das gleiche Problem oder du später auch iwann mal wieder. Da freut man sich über Antworten. Also schon in deinem interesse: poste die Lösung

Sorry, ich habe diese Vorgangsweise benutzt:

PropertyChangeListener (Java Standard: Muster Observer ? Wikibooks, Sammlung freier Lehr-, Sach- und Fachbücher)

Hier der komplette Quellcode:

PingPong.java
Java:
package pingpong;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

//public class PingPong extends JApplet implements Runnable, Observer, PropertyChangeListener {
public class PingPong extends JApplet implements Runnable, PropertyChangeListener {
	private static final long serialVersionUID = 1L;
	LayoutManager layout;
	Thread thread;
	int gamestatus;
	JLabel points = new JLabel("");
	int punkte;
	
	public void propertyChange(PropertyChangeEvent pce) {
		/*System.out.println("Bei der " + pce.getSource() + " wurde der Parameter \"" + 
							pce.getPropertyName() + "\" von \"" + pce.getOldValue() + 
							"\" auf \"" + pce.getNewValue() + "\" geaendert.");*/
		if(pce.getPropertyName().equals("key")) {
			System.out.println("KeyCode: " + pce.getNewValue());
		}
		if(pce.getPropertyName().equals("gamestatus") && pce.getNewValue().equals("0")) {
			System.out.println("Gamestatus: 0");
		}
		if(pce.getPropertyName().equals("points")) {
			System.out.println("Punkte: " + pce.getNewValue());
			points.setText("" + pce.getNewValue());
			punkte = (Integer)pce.getNewValue();
		}
		if(pce.getPropertyName().equals("gamestatus") && pce.getNewValue().equals("0")) {
			System.out.println("Spiel zu Ende!");
			final JDialog popup = new JDialog();
			popup.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			popup.setSize(350, 100);
			popup.setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			// Label Punkte
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 0; // Column
			gbc.gridy = 0; // Row
			gbc.gridheight = 1;
			gbc.gridwidth = 1;
			gbc.insets = new Insets(0, 0, 0, 0); //Abstand: oben, links, unten, rechts
			popup.add(new JLabel("Ihre Punkte: "), gbc);
			// Punkte
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 1; // Column
			gbc.gridy = 0; // Row
			gbc.gridheight = 1;
			gbc.gridwidth = 1;
			gbc.insets = new Insets(0, 0, 0, 0); //Abstand: oben, links, unten, rechts
			popup.add(points, gbc);
			// Label Name
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 0; // Column
			gbc.gridy = 1; // Row
			gbc.gridheight = 1;
			gbc.gridwidth = 1;
			gbc.insets = new Insets(0, 0, 0, 10); //Abstand: oben, links, unten, rechts
			popup.add(new JLabel("Ihr Name: "), gbc);
			// Textfeld Name
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 1; // Column
			gbc.gridy = 1; // Row
			gbc.gridheight = 1;
			gbc.gridwidth = 2;
			gbc.insets = new Insets(0, 0, 0, 0); //Abstand: oben, links, unten, rechts
			final JTextField tf_name = new JTextField(20);
			popup.add(tf_name, gbc);
			// Button Speichern
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 0; // Column
			gbc.gridy = 2; // Row
			gbc.gridheight = 1;
			gbc.gridwidth = 1;
			gbc.insets = new Insets(0, 0, 0, 10); //Abstand: oben, links, unten, rechts
			JButton save = new JButton("Speichern");
			save.setEnabled(false);
			save.setToolTipText("Diese Funktion ist leider noch nicht implementiert!");
			save.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(tf_name.getText().equals("")) {
						
					} else {
						new Save(punkte, tf_name.getText(), popup);
					}
				}
			});
			popup.add(save, gbc);
			// Button Abbrechen
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.gridx = 1; // Column
			gbc.gridy = 2; // Row
			gbc.gridheight = 1;
			gbc.gridwidth = 1;
			gbc.insets = new Insets(0, 0, 0, 0); //Abstand: oben, links, unten, rechts
			JButton abort = new JButton("Abbrechen");
			abort.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					abort(popup);
				}
			});
			popup.add(abort, gbc);
			
			popup.setResizable(false);
			popup.setVisible(true);
		}
	}
	
	public void init() {
		
	}
	
	public void start() {
		if(thread == null) {
	        thread = new Thread(this);
	        thread.start();
	    }
		
		// Menu
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("Datei");
        JMenu overMenu = new JMenu("Hilfe");
        menuBar.add(fileMenu);
        menuBar.add(overMenu);
        JMenuItem exitAction = new JMenuItem("Beenden");
        JMenuItem infoAction = new JMenuItem("Info");
        //infoAction.setIcon(new ImageIcon(getClass().getResource("CustomImage/info.png")));
        fileMenu.add(exitAction);
        overMenu.add(infoAction);
        exitAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                exit();
            }
        });
        infoAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                
            }
        });
		
		layout = new GridLayout(0, 1);
        setLayout(layout);
        
        MainDrawing draw = new MainDrawing();
        add(draw);
	}
	
	@SuppressWarnings("deprecation")
	public void stop() {
		if(thread != null && thread.isAlive()) {
			thread.stop();
			thread = null;
		}
	}
	
	public void destroy() {
		
	}

	public void run() {
		while(true) {
			repaint();
			try {
				Thread.sleep(15);
			} catch(InterruptedException e) {
				
			}
		}
	}
	
	public void exit() {
		System.exit(0);
	}
	
	public void abort(JDialog popup) {
		popup.dispose();
	}
}

MainDrawing.java
Java:
package pingpong;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Arrays;

import javax.swing.JPanel;

public class MainDrawing extends JPanel implements MouseListener, KeyListener {
	private static final long serialVersionUID = 1L;
	private Image dbImage;
	private Graphics dbg;
	int kugel_xpos = 0;
	int kugel_ypos = 0;
	boolean kugel_direction_xpos = true;
	boolean kugel_direction_ypos = true;
	int balken_hoehe = 50;
	int balken_xpos = 5;
	int balken_ypos = 5;
	int points = -1;
	int counter;
	int gamestatus = -1;
	
	public MainDrawing() {
		addMouseListener((MouseListener)this);
		addKeyListener((KeyListener)this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		//g2d.drawRect(balken_xpos, balken_ypos, 20, 20);
		if(gamestatus == -1) {
			gamestop(g2d);
		} else if(gamestatus == 1) {
			gamestart(g2d);
			for(int i = 999999; i >= 0; i--) {
				counter -= i;
				if(gamestatus == 1 && counter == 0) {
					gamestatus = 0;
				    Updatable_Objects aktu = new Updatable_Objects(new Quelle());
					aktu.addPropertyChangeListener(new PingPong());
					aktu.setGameStatus("" + gamestatus);
					aktu.setPoints(points);
				}
			}
		} else if(gamestatus == 0) {
			gameend(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 mouseClicked(MouseEvent e) {
		if(gamestatus == -1 && e.getClickCount() == 2) {
			gamestatus = 1;
		}
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}
	
	public void keyPressed(KeyEvent e) {
		Updatable_Objects aktu = new Updatable_Objects(new Quelle());
		aktu.addPropertyChangeListener(new PingPong());
		// Taste nach oben
	    if(e.getKeyCode() == 38) {
	      balken_ypos -= 1;
	      aktu.setKeyCode("" + e.getKeyCode());
	    // Taste nach unten
	    } else if(e.getKeyCode() == 40) {
	      balken_ypos += 1;
	      aktu.setKeyCode("" + e.getKeyCode());
	    } else {
	    	System.out.println("Key: " + e.getKeyCode());
	    	aktu.setKeyCode("" + e.getKeyCode());
	    }
	}

	public void keyTyped(KeyEvent e) {
		
	}

	public void keyReleased(KeyEvent e) {
		
	}
	
	public void gamestart(Graphics g) {
		g.drawRect(0, 0, 153, 153);
		g.drawString(points + " Punkte", 153 / 2, 15);
		g.drawString(counter + " s", 153 / 2, 25);
		//g.drawString("Balken Startposition y: " + balken_ypos, 25, 25);
		//g.drawString("Balken Endposition: " + (balken_ypos + balken_hoehe), 25, 35);
		g.fillRect(balken_xpos, balken_ypos, 15, balken_hoehe);
		g.fillOval(kugel_xpos, kugel_ypos, 15, 15);
		if(kugel_direction_xpos == true) {
			kugel_xpos += 1;
			if(kugel_xpos == 140) {
				kugel_direction_xpos = false;
			}
		} else {
			kugel_xpos -= 1;
			if(kugel_xpos == balken_xpos + 14) {
					kugel_direction_xpos = true;
			}
		}
		if(kugel_direction_ypos == true) {
			kugel_ypos += 1;
			if(kugel_ypos == 140) {
				kugel_direction_ypos = false;
			}
		} else {
			kugel_ypos -= 1;
			
			if(kugel_ypos == 0) {
				kugel_direction_ypos = true;
			}
		}
		if(kugel_ypos > balken_ypos && kugel_ypos < (balken_ypos + balken_hoehe) && kugel_xpos == balken_xpos + 14) {
			points++;
		} else if(kugel_ypos != balken_ypos) {
			
		}
	}
	
	public void gamestop(Graphics g) {
		g.drawString("Bitte Spiel mit Doppelklick starten!", 0, 153 / 2);
	}
	
	public void gameend(Graphics g) {
		g.drawString("Das Spiel ist zu Ende, ", 10, 75);
		g.drawString("ihre Punkte: " + points, 10, 90);
	}
}

Quelle.java
Java:
package pingpong;

public class Quelle {
	 private String gamestatus;
	 private int points;
	 private String key;
	 
	 public void setGameStatus(String gamestatus) {
	   this.gamestatus = gamestatus;
	 }
	 
	 public void setPoints(int points) {
	   this.points = points;
	 }
	 
	 public void setKeyCode(String key) {
		 this.key = key;
	 }
	 
	 public String getGameStatus() {
	   return gamestatus;
	 }
	 
	 public int getPoints() {
	   return points;
	 }
	 
	 public String getKeyCode() {
		 return key;
	 }
	 
	 public String toString() {
	   return "Quell-Bean";
	 }
	}

Updatable_Objects.java
Java:
package pingpong;

import java.beans.PropertyChangeSupport;

public class Updatable_Objects extends PropertyChangeSupport {
	 private Quelle quelle;
	 
	 public Updatable_Objects(Quelle quelle) {
	   super(quelle);
	   this.quelle = quelle;
	 }
	 
	 public void setGameStatus(String gamestatus) {
	   super.firePropertyChange("gamestatus", quelle.getGameStatus(), gamestatus);
	   quelle.setGameStatus(gamestatus);
	 }
	 
	 public void setPoints(int points) {
	   super.firePropertyChange("points", quelle.getPoints(), points);
	   quelle.setPoints(points);
	 }
	 
	 public void setKeyCode(String key) {
		   super.firePropertyChange("key", quelle.getKeyCode(), key);
		   quelle.setKeyCode(key);
		 }
	 
	 public String getGameStatus() {
	   return quelle.getGameStatus();
	 }
	 
	 public int getPoints() {
	   return quelle.getPoints();
	 }
	 
	 public String getKeyCode() {
		 return quelle.getKeyCode();
	 }
}

Save.java
Java:
package pingpong;

import javax.swing.JDialog;

public class Save {
	/**
	 * Datenbank Parameter
	 */
	String host = "xxx";
	int port = xxx;
	String username = "xxx";
	String password = "xxx";
	
	public Save(int punkte, String name, JDialog popup) {
		System.out.println("Folgende Werte wurden gespeichert:");
		System.out.print("Punkte: " + punkte);
		System.out.println(", Name: " + name);
		
		
		
		popup.dispose();
	}
}

Have Fun :toll:

LG Purzel 🙂
 
perfekt. Eine kleine Anmerkung zum schluss:
[c]balken_hoehe[/c][c]Updatable_Objects[/c]

in Java nutzt man CamelCase:

für Variablen/Referenzen lowerCamelCase: [c]balkenHoehe[/c]
für Klassen/Interfaces UpperCamelCase: [c]UpdatableObjects[/c]

MFG

Tomate_Salat
 
perfekt. Eine kleine Anmerkung zum schluss:
[c]balken_hoehe[/c][c]Updatable_Objects[/c]

in Java nutzt man CamelCase:

für Variablen/Referenzen lowerCamelCase: [c]balkenHoehe[/c]
für Klassen/Interfaces UpperCamelCase: [c]UpdatableObjects[/c]

MFG

Tomate_Salat

Danke für den Tipp, werde es versuchen zu berücksichtigen!

LG Purzel

Topic kann geschlossen werden!
 

Zurück
Oben