package crazyOrb.gui;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import crazyOrb.mechanics.Ball;
import crazyOrb.mechanics.MechanicsFacade;
import crazyOrb.mechanics.NotifiedObject;
import crazyOrb.mechanics.RepaintComponent;
import crazyOrb.musikplayer.PlayerThread;
import crazyOrb.musikplayer.SoundThread;
/**
* @author Claudia, Marlene, Ragna-Diana
*/
public class SpielGUI extends JFrame implements ActionListener, NotifiedObject {
private static final long serialVersionUID = 1L;
private EinstellungenGUI props = new EinstellungenGUI(this);
private JPanel contentPane;
private JMenuBar menue = new JMenuBar();
JLabel lblLeben_1 = new JLabel();
JLabel lblLeben_2 = new JLabel();
JLabel lblLeben_3 = new JLabel();
JLabel lblStufe = new JLabel("0");
JLabel lblPoints = new JLabel("0");
JLabel lblSpecial = new JLabel("Special");
JLabel actualSpecial = new JLabel("Kein Special");
private JMenuItem neu = new JMenuItem("Neues Spiel");
private JMenuItem highscore = new JMenuItem("Highscore");
private JMenuItem beenden = new JMenuItem("Beenden");
private JMenuItem properties = new JMenuItem("Einstellungen");
private JMenuItem ueber = new JMenuItem("Über");
private JToggleButton btnMusik = new JToggleButton("");
private JButton btnPause = new JButton("Pause");
private MechanicsFacade mechanics = new MechanicsFacade(this);
private static boolean spiel = false;
static PlayerThread musicThread;
static PlayCanvas canvas;
private boolean laserPickedUp = false;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SpielGUI frame = new SpielGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SpielGUI() {
setTitle("CrazyOrb");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
canvas = new PlayCanvas(mechanics);
setBounds(100, 100, 470, 520);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 102));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
canvas.setBounds(105, 10, 250, 450);
contentPane.add(canvas);
btnPause.setBounds(10, 73, 89, 23);
contentPane.add(btnPause);
JLabel lblPunkte = new JLabel("Punkte");
lblPunkte.setBounds(369, 344, 86, 14);
contentPane.add(lblPunkte);
lblPoints.setFont(new Font("Tahoma", Font.BOLD, 15));
lblPoints.setForeground(new Color(255, 0, 0));
lblPoints.setHorizontalAlignment(SwingConstants.RIGHT);
lblPoints.setBounds(369, 359, 86, 23);
contentPane.add(lblPoints);
JLabel lblMusik = new JLabel("Musik");
lblMusik.setHorizontalAlignment(SwingConstants.CENTER);
lblMusik.setBounds(10, 176, 89, 14);
contentPane.add(lblMusik);
JLabel lblLeben = new JLabel("Leben");
lblLeben.setBounds(369, 302, 46, 14);
contentPane.add(lblLeben);
btnMusik.setSelectedIcon(new ImageIcon(SpielGUI.class.getResource("/crazyOrb/gui/audio-volume-muted.png")));
btnMusik.setIcon(new ImageIcon(SpielGUI.class.getResource("/crazyOrb/gui/audio-volume-high.png")));
btnMusik.setBounds(20, 201, 65, 31);
contentPane.add(btnMusik);
JLabel lblLevel = new JLabel("Level");
lblLevel.setBounds(369, 277, 46, 14);
contentPane.add(lblLevel);
lblStufe.setFont(new Font("Tahoma", Font.BOLD, 15));
lblStufe.setForeground(new Color(255, 0, 0));
lblStufe.setHorizontalAlignment(SwingConstants.RIGHT);
lblStufe.setBounds(409, 277, 46, 14);
contentPane.add(lblStufe);
lblLeben_1.setIcon(new ImageIcon(SpielGUI.class.getResource("/crazyOrb/gui/emblem-favorite.png")));
lblLeben_1.setHorizontalAlignment(SwingConstants.CENTER);
lblLeben_1.setBounds(431, 319, 21, 14);
contentPane.add(lblLeben_1);
lblLeben_3.setHorizontalAlignment(SwingConstants.CENTER);
lblLeben_3.setIcon(new ImageIcon(SpielGUI.class.getResource("/crazyOrb/gui/emblem-favorite.png")));
lblLeben_3.setBounds(389, 319, 24, 14);
contentPane.add(lblLeben_3);
lblLeben_2.setHorizontalAlignment(SwingConstants.CENTER);
lblLeben_2.setIcon(new ImageIcon(SpielGUI.class.getResource("/crazyOrb/gui/emblem-favorite.png")));
lblLeben_2.setBounds(409, 319, 24, 14);
contentPane.add(lblLeben_2);
lblSpecial.setBounds(369, 394, 61, 15);
contentPane.add(lblSpecial);
actualSpecial.setBounds(369, 421, 86, 14);
contentPane.add(actualSpecial);
setJMenuBar(menue);
JMenu spiel = new JMenu("Spiel");
JMenu hilfe = new JMenu("?");
musicThread = new PlayerThread();
neu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.getKeyText(KeyEvent.VK_F2)));
spiel.add(neu);
spiel.add(highscore);
spiel.addSeparator();
spiel.add(beenden);
hilfe.add(properties);
hilfe.addSeparator();
hilfe.add(ueber);
menue.add(spiel);
menue.add(hilfe);
beenden.addActionListener(this);
ueber.addActionListener(this);
properties.addActionListener(this);
highscore.addActionListener(this);
neu.addActionListener(this);
btnMusik.addActionListener(this);
btnPause.addActionListener(this);
setResizable(false);
}
public void setSpiel(boolean spiel) {
SpielGUI.spiel = spiel;
}
public static boolean getSpiel() {
return spiel;
}
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
Object quelle = e.getSource();
if (quelle == beenden) {
System.exit(0);
}
if (quelle == ueber) {
AboutGUI about = new AboutGUI();
about.setVisible(true);
}
if (quelle == properties) {
props.setVisible(true);
}
if (quelle == highscore) {
EintragGUI high = new EintragGUI();
high.setVisible(true);
}
if (quelle == neu) {
mechanics.restart();
btnMusik.setSelected(false);
musicThread.stop();
musicThread = new PlayerThread();
PlayerThread.setLied(props.getSelectedTrack());
musicThread.start();
canvas.showString("Zum Starten P drücken!");
setSpiel(true);
repaint();
}
if (quelle == btnPause) {
if (mechanics.isPaused()) {
canvas.showString("");
mechanics.resumeMovement();
} else {
canvas.showString("Fortsetzen? P drücken!");
mechanics.pauseMovement();
}
}
if (quelle == btnMusik) {
if (spiel) {
if (musicThread.isAlive()) {
if (btnMusik.isSelected()) {
musicThread.stop();
}
}
if (btnMusik.isSelected() == false) {
musicThread = new PlayerThread();
musicThread.start();
}
}
}
canvas.requestFocus();
}
@SuppressWarnings("deprecation")
@Override
public void dispose() {
mechanics.terminate();
musicThread.stop();
super.dispose();
System.exit(0);
}
@SuppressWarnings("deprecation")
@Override
public void lostAllLifes() {
musicThread.stop();
new SoundThread("DanielGameOver");
canvas.showString(" GAME OVER");
this.repaint();
mechanics.terminate();
HighscoreGUI high = new HighscoreGUI(mechanics.getBall().getHighscore());
high.setVisible(true);
setSpiel(false);
}
@Override
public void ballCrashedWithBlock() {
new SoundThread("DanielLostLife");
canvas.showString("Fortsetzen? P drücken!");
this.repaint();
}
public void repaint() {
Ball ball = mechanics.getBall();
lblPoints.setText(ball.getHighscore() + "");
lblStufe.setText(ball.getLevel() + "");
int leben = ball.getLifes();
lblLeben_1.setVisible(true);
lblLeben_2.setVisible(true);
lblLeben_3.setVisible(true);
if (ball.getSpecial() == null) {
actualSpecial.setText("Kein Special");
} else {
actualSpecial.setText(ball.getSpecial().getDescription());
if (actualSpecial.getText().equals("Shoot Laser") && !laserPickedUp) {
new SoundThread("DanielNewLaser");
laserPickedUp = true;
}
}
switch (leben) {
case 0:
lblLeben_1.setVisible(false);
lblLeben_2.setVisible(false);
lblLeben_3.setVisible(false);
break;
case 1:
lblLeben_2.setVisible(false);
lblLeben_3.setVisible(false);
break;
case 2:
lblLeben_3.setVisible(false);
break;
}
canvas.repaint();
}
@Override
public void repaint(RepaintComponent event) {
if(event.equals(RepaintComponent.LASER)){
new SoundThread("DanielLaser");
canvas.setLaser(true);
laserPickedUp = false;
}if(event.equals(RepaintComponent.LIFE)){
new SoundThread("DanielNewLife");
}
repaint();
}
public void setBtnMusik() {
btnMusik.setSelected(false);
}
}