/Users/petercappello/NetBeansProjects/56-2014/56-2014-3-Zapem/src/ControlPanel.java |
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
The controller for the Zap 'em game.
@author
public class ControlPanel extends JPanel implements ActionListener, MouseListener
{
private final static int PAUSE = 150;
private final View view;
private final Game game;
private final JButton newGameButton = new JButton( "New Game" );
private final JLabel gameDurationLabel = new JLabel( " Game duraion in seconds:" );
private final JTextField gameDurationTextField = new JTextField();
private final Timer animationTimer;
private long gameStartTime;
ControlPanel( View view, Game game )
{
this.view = view;
this.game = game;
setLayout( new GridLayout( 1, 3 ) );
add( newGameButton );
add( gameDurationLabel );
add( gameDurationTextField );
animationTimer = new Timer( PAUSE, this );
gameDurationTextField.setEditable( false );
addEventListener();
}
private void addEventListener()
{
newGameButton.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent actionEvent )
{
newGameButtonActionPerformed( actionEvent );
}
});
view.addMouseListener( this );
}
private void newGameButtonActionPerformed( ActionEvent actionEvent )
{
gameDurationTextField.setText("");
gameStartTime = System.currentTimeMillis();
animationTimer.restart();
game.start();
}
Implementation of ActionListener of Timer
@param
@Override
public void actionPerformed( ActionEvent e)
{
game.draw();
view.repaint();
if ( game.isOver() )
{
animationTimer.stop();
Long gameDuration = System.currentTimeMillis() - gameStartTime;
Long seconds = gameDuration / 1000;
Long fraction = gameDuration % 1000;
gameDurationTextField.setText( seconds + "." + fraction );
}
}
Process mouse clicked event
@param
@Override
public void mouseClicked( MouseEvent event )
{
if ( game.isOver() )
{
return;
}
animationTimer.stop();
game.processClick( event.getX(), event.getY() );
animationTimer.start();
view.repaint();
}
Unimplemented.
@param
@Override
public void mousePressed(MouseEvent e) {}
Unimplemented.
@param
@Override
public void mouseReleased(MouseEvent e) {}
Unimplemented.
@param
@Override
public void mouseEntered(MouseEvent e) {}
Unimplemented.
@param
@Override
public void mouseExited(MouseEvent e) {}
}