private void createTetrisFrame ( final WebDesktopPane desktopPane )
{
final Tetris tetris = new Tetris ();
tetris.setUseInternalHotkeys ( false );
final WebInternalFrame tetrisFrame = new WebInternalFrame ( "Tetris frame", false, true, false, true )
{
@Override
public void setVisible ( boolean aFlag )
{
if ( !aFlag )
{
tetris.pauseGame ();
}
super.setVisible ( aFlag );
}
};
tetrisFrame.setFrameIcon ( loadIcon ( "game.png" ) );
tetrisFrame.add ( tetris );
WebMenuBar tetrisMenu = new WebMenuBar ();
tetrisMenu.add ( new JMenu ( "Game" )
{
{
add ( new JMenuItem ( "New game", loadIcon ( "tetris/new.png" ) )
{
{
setAccelerator ( KeyStroke.getKeyStroke ( KeyEvent.VK_F2, 0 ) );
addActionListener ( new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
tetris.newGame ();
}
} );
}
} );
add ( new JMenuItem ( "Unpause game", loadIcon ( "tetris/unpause.png" ) )
{
{
tetris.addTetrisListener ( new TetrisListener ()
{
@Override
public void newGameStarted ()
{
setEnabled ( true );
setIcon ( loadIcon ( "tetris/pause.png" ) );
setText ( "Pause game" );
}
@Override
public void gamePaused ()
{
setIcon ( loadIcon ( "tetris/unpause.png" ) );
setText ( "Unpause game" );
}
@Override
public void gameUnpaused ()
{
setIcon ( loadIcon ( "tetris/pause.png" ) );
setText ( "Pause game" );
}
@Override
public void gameOver ()
{
setEnabled ( false );
setIcon ( loadIcon ( "tetris/pause.png" ) );
setText ( "Pause game" );
}
} );
setAccelerator ( KeyStroke.getKeyStroke ( KeyEvent.VK_P, 0 ) );
addActionListener ( new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
if ( tetris.isPaused () )
{
tetris.unpauseGame ();
}
else
{
tetris.pauseGame ();
}
}
} );
}
} );
addSeparator ();
add ( new JMenuItem ( "Close", loadIcon ( "tetris/exit.png" ) )
{
{
setAccelerator ( KeyStroke.getKeyStroke ( KeyEvent.VK_F4, KeyEvent.SHIFT_MASK ) );
addActionListener ( new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
tetris.pauseGame ();
tetrisFrame.close ();
}
} );
}
} );
}
} );
tetrisMenu.add ( new JMenu ( "About" ) );
tetrisFrame.setJMenuBar ( tetrisMenu );
final WebButton tetrisFrameIcon = new WebButton ( "Tetris", loadIcon ( "tetris.png" ) );
tetrisFrameIcon.setRolloverDecoratedOnly ( true );
tetrisFrameIcon.setHorizontalTextPosition ( WebButton.CENTER );
tetrisFrameIcon.setVerticalTextPosition ( WebButton.BOTTOM );
tetrisFrameIcon.addActionListener ( new ActionListener ()
{
private boolean loading = false;
private boolean firstLoad = true;
@Override
public void actionPerformed ( ActionEvent e )
{
if ( loading || tetrisFrameIcon.getClientProperty ( DesktopPaneIconMoveAdapter.DRAGGED_MARK ) != null )
{
return;
}
tetrisFrameIcon.setIcon ( loadIcon ( "loader.gif" ) );
loading = true;
new Thread ( new Runnable ()
{
@Override
public void run ()
{
if ( firstLoad )
{
firstLoad = false;
ThreadUtils.sleepSafely ( 1000 );
}
SwingUtilities.invokeLater ( new Runnable ()
{
@Override
public void run ()
{
if ( tetrisFrame.isClosed () )
{
if ( tetrisFrame.getParent () == null )
{
desktopPane.add ( tetrisFrame );
}
tetrisFrame.open ();
tetrisFrame.setIcon ( false );
}
else
{
tetrisFrame.setIcon ( !tetrisFrame.isIcon () );
}
tetrisFrameIcon.setIcon ( loadIcon ( "tetris.png" ) );
loading = false;
}
} );
}
} ).start ();
}
} );
DesktopPaneIconMoveAdapter ma2 = new DesktopPaneIconMoveAdapter ();
tetrisFrameIcon.addMouseListener ( ma2 );
tetrisFrameIcon.addMouseMotionListener ( ma2 );
tetrisFrameIcon.setBounds ( 25, 25, 100, 75 );
desktopPane.add ( tetrisFrameIcon );
tetrisFrame.pack ();
tetrisFrame.setLocation ( 25 + 100 + 25, 25 );
tetrisFrame.close ();
}