/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.drthum.simon.gui;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import net.drthum.simon.model.Color;
import net.drthum.simon.model.Game;
import net.drthum.simon.model.Sequence;
/**
*
* @author DrThum
*/
public class MainGUI extends javax.swing.JFrame {
private Game game;
/**
* Creates new form MainGUI
*/
public MainGUI() {
/*game = new Game();
game.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pce) {
if (pce.getPropertyName().equals(Game.GAMESEQ_CHANGED))
playSequence(game.getGameSequence());
}
});*/
initComponents();
pack();
}
private void playSequence(final Sequence sequence) {
lbStatus.setText("Showing sequence.");
Runnable run = new Runnable() {
@Override
public void run() {
Iterator<Color> it = sequence.iterator();
Color color;
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
System.err.println("Caught InterruptedException: " + ex.getMessage());
}
while (it.hasNext()) {
color = it.next();
try {
gamePanel.toggleColor(color, true);
Thread.sleep(500);
gamePanel.toggleColor(color, false);
Thread.sleep(500);
} catch (InterruptedException ex) {
System.err.println("Caught InterruptedException: " + ex.getMessage());
}
}
lbStatus.setText("Your turn! (level " + game.getLevel() + ")");
}
};
Thread th = new Thread(run);
th.start();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
gamePanel = new net.drthum.simon.gui.GamePanel(game);
lbStatus = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
mGame = new javax.swing.JMenu();
miNew = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator();
miQuit = new javax.swing.JMenuItem();
mHelp = new javax.swing.JMenu();
miRules = new javax.swing.JMenuItem();
miAbout = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Simon's Game");
setResizable(false);
lbStatus.setText("Ready.");
lbStatus.setBorder(javax.swing.BorderFactory.createEtchedBorder());
mGame.setText("Game");
miNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
miNew.setText("New");
miNew.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
miNewActionPerformed(evt);
}
});
mGame.add(miNew);
mGame.add(jSeparator2);
miQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
miQuit.setText("Quit");
miQuit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
miQuitActionPerformed(evt);
}
});
mGame.add(miQuit);
jMenuBar1.add(mGame);
mHelp.setText("?");
miRules.setText("Rules");
miRules.setEnabled(false);
mHelp.add(miRules);
miAbout.setText("About");
miAbout.setEnabled(false);
mHelp.add(miAbout);
jMenuBar1.add(mHelp);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(gamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(gamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbStatus))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void miQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miQuitActionPerformed
System.exit(0);
}//GEN-LAST:event_miQuitActionPerformed
private void miNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miNewActionPerformed
game = new Game();
game.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pce) {
if (pce.getPropertyName().equals(Game.GAMESEQ_CHANGED))
playSequence(game.getGameSequence());
else if (pce.getPropertyName().equals(Game.GAMESTATE_LOST)) {
lbStatus.setText("You lost at level " + game.getLevel() + ".");
gamePanel.setGame(null);
}
}
});
gamePanel.setGame(game);
startingAnim();
}//GEN-LAST:event_miNewActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private net.drthum.simon.gui.GamePanel gamePanel;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu.Separator jSeparator2;
private javax.swing.JLabel lbStatus;
private javax.swing.JMenu mGame;
private javax.swing.JMenu mHelp;
private javax.swing.JMenuItem miAbout;
private javax.swing.JMenuItem miNew;
private javax.swing.JMenuItem miQuit;
private javax.swing.JMenuItem miRules;
// End of variables declaration//GEN-END:variables
private void startingAnim() {
Runnable run = new Runnable() {
@Override
public void run() {
try {
lbStatus.setText("Starting in 3...");
Thread.sleep(1000);
lbStatus.setText("Starting in 2...");
Thread.sleep(1000);
lbStatus.setText("Starting in 1...");
Thread.sleep(1000);
lbStatus.setText("Showing sequence.");
game.nextLevel();
} catch (InterruptedException ex) {
System.err.println("Caught InterruptedException: " + ex.getMessage());
}
}
};
Thread th = new Thread(run);
th.start();
}
}