import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*Panel für die Ballspiel-Applikation .
*
* @author Martin S
* @version 2011-02-24
*/
public class BallspielPanel extends JPanel {
private Button east, west, south, north;
private JLabel ball;
private ImageIcon icon;
private JPanel panel;
private int x, y, width, height;
/**
* Standard-Konstruktor zum deklarieren,initaliserien und hinzufügen der
* GUI-Elemente.
*/
public BallspielPanel() {
//Ball Int-Variablen init.
x = 5;
y = 30;
width = 75;
height = 75;
//Layout setzen
setLayout(new BorderLayout());
//Spielfigur init.
/**
* Methoden aus der Java-API
*/
icon = new ImageIcon(BallspielPanel.class.getResource("fball.png"));
ball = new JLabel(icon);
ball.setBounds(x, y, width, height);
//Buttons init. und hinzufügen
east = new Button("O");
west = new Button("W");
south = new Button("S");
north = new Button("N");
add(east, BorderLayout.EAST);
add(west, BorderLayout.WEST);
add(south, BorderLayout.SOUTH);
add(north, BorderLayout.NORTH);
//Panel init. und hinzufügen
//Panel
panel = new JPanel();
panel.setLayout(null);
panel.add(ball);
add(panel);
////ActionListener Objekt erzeugen
BallspielAction h = new BallspielAction();
east.addActionListener(h);
west.addActionListener(h);
south.addActionListener(h);
north.addActionListener(h);
}
/**
*Innere Klasse, für die einzelnen Aktionen.
*
*/
private class BallspielAction implements ActionListener {
/**
* Methode,für die einzelen Buttonklicks,usw... .
*
* @param e
*/
public void actionPerformed(ActionEvent e) {
//Ballsteuereung erstellen
if (e.getSource() == east) {
//Begrenzung rechts
if (x + 50 < panel.getWidth() - 75) {
ball.setBounds(x = x + 75, y, width, height);
}
}
if (e.getSource() == west) {
//Begrenzung links
if (x > 75) {
ball.setBounds(x = x - 50, y, width, height);
} else {
ball.setBounds(0, y, width, height);
x = 0;
}
}
if (e.getSource() == north) {
//Begrenzung oben
if (y > 50) {
ball.setBounds(x, y = y - 50, width, height);
} else {
ball.setBounds(x, 0, width, height);
y = 0;
}
}
//Begrenzung unten
if (e.getSource() == south) {
if (y + 50 < panel.getHeight() - 75) {
ball.setBounds(x, y = y + 50, width, height);
} else {
ball.setBounds(x, panel.getHeight() - 75, width, height);
y = panel.getHeight() - 75;
}
}
}
}
}