Hallo,
da ja mein letztes Problem gut gelöst wurde hier gleich ein neues:
Im Voraus: Ich habe das Problem schon in einem anderen Forum gepostet, leider noch kein Ergebnis, hoffe hier kann mir jemand helfen!
Mein Problem ist das ich derzeit mein Applet immer wieder im AppletViewer angesehen habe und da hat es funktioniert, nun wollte ich es im Browser testen und siehe da, die Tastatur (keyPressed()) reagiert nicht!
Wo liegt mein Problem? Ich hoffe es kann mir jemand helfen. Danke schon mal!
LG Purzel
Hier der Quellcode:
PingPong.java
MainDrawing.java
Quelle.java
Updatable_Objects.java
Save.java
da ja mein letztes Problem gut gelöst wurde hier gleich ein neues:
Im Voraus: Ich habe das Problem schon in einem anderen Forum gepostet, leider noch kein Ergebnis, hoffe hier kann mir jemand helfen!
Mein Problem ist das ich derzeit mein Applet immer wieder im AppletViewer angesehen habe und da hat es funktioniert, nun wollte ich es im Browser testen und siehe da, die Tastatur (keyPressed()) reagiert nicht!
Wo liegt mein Problem? Ich hoffe es kann mir jemand helfen. Danke schon mal!
LG Purzel
Hier der 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();
}
}