import java.awt.* ;
import java.awt.event.KeyAdapter ;
import java.awt.event.KeyEvent ;
import java.util.Random ;
import javax.swing.JFrame ;
/**
* Full-screen mode example window.
*/
public class CWindow extends JFrame
{
private static final long serialVersionUID = 1L ;
//
private GraphicsDevice myDisplay ;
private Thread myDrawThread ;
// ------------------------------------------------------------------------
public static void main( String[] args )
{
CWindow myWnd ;
myWnd = new CWindow() ;
myWnd.startDrawing() ;
}
public CWindow()
{
this.init_graphics() ;
registerKeyListener() ;
}
// ------------------------------------------------------------------------
/**
*
*/
private void init_graphics()
{
this.setUndecorated( true ) ;
this.setResizable( false ) ;
//
// Only this window itself can process input events (keyboard, mouse), the
// "consume stack" is disabled. This means that JButtons, JTextFields,
// ...,
// are useless, because input events can not be passed to them.
// This behaviour MUST BE SET in full-screen mode. (See {@link
// java.awt.GraphicsDevice#setFullScreenWindow(Window)}.)
this.enableInputMethods( false ) ;
switchToFullScreen() ;
}
/**
* All keyboard input MUST BE HANDLED by this window itself; no JTextField,
* JButton, ... objects are possible.
*/
private void registerKeyListener()
{
this.addKeyListener( new KeyAdapter() {
@Override
public void keyTyped( KeyEvent e )
{
char letter ;
letter = e.getKeyChar() ;
switch( letter )
{
case 'q' :
case 'Q' :
myDrawThread.interrupt() ;
setVisible( false ) ;
System.exit( 0 ) ;
break ;
default :
break ;
} // end switch( letter )
}
// @Override
// public void keyReleased( KeyEvent e )
// {
// }
//
// @Override
// public void keyPressed( KeyEvent e )
// {
// }
} ) ;
}
/**
* @throws NotSupportedException .. if full-screen mode is not supported.
* @throws IllegalStateException .. if switching is not possible (though was
* reported "is supported")
*/
private void switchToFullScreen()
{
myDisplay = getGraphicDevice() ;
if( !myDisplay.isFullScreenSupported() )
throw(new NotSupportedException(
"Graphics device does not support 'full screen' mode." )) ;
// else: continue
try
{
myDisplay.setFullScreenWindow( this ) ;
}
catch( Throwable e )
{
throw(new IllegalStateException(
"Unable to switch to 'full screen' mode, but was told to be supported." ,
e )) ;
}
}
/**
* If mode==false, first unregisters this window from full-screen handler.
*/
@Override
public void setVisible( boolean mode )
{
// to close window, we first must unregister it at full-screen handler
if( !mode ) myDisplay.setFullScreenWindow( null ) ;
super.setVisible( mode ) ;
}
/**
* Find/get the GraphicsDevice that becomes out full-screen handler.
*/
private static GraphicsDevice getGraphicDevice()
{
final int device_to_use = 0 ;
GraphicsEnvironment g_env ;
GraphicsDevice[] ar_available_devices ;
GraphicsDevice myDisplay ;
g_env = GraphicsEnvironment.getLocalGraphicsEnvironment() ;
ar_available_devices = g_env.getScreenDevices() ;
if( ar_available_devices.length == 1 )
myDisplay = ar_available_devices[ 0 ] ;
else
myDisplay = ar_available_devices[ device_to_use ] ;
// end if
return myDisplay ;
}
/**
* Start an autonomous drawing thread, that continues drawing things.
*/
public void startDrawing()
{
final Graphics g = this.getGraphics() ;
final Dimension wndSize = this.getSize() ;
myDrawThread = new Thread() {
private Random rand = new Random() ;
@Override
public void run()
{
int x , y ;
@SuppressWarnings( "unused" )
int width , height ;
// "clear screen" with black
g.setColor( Color.BLACK ) ;
g.fillRect( 0 , 0 , wndSize.width , wndSize.height ) ;
//
while( !interrupted() )
{
// draw "Exit with 'q'" at top left
g.setColor( Color.BLACK ) ;
g.fillRect( x = 0 , y = 0 , width = 64 , height = 14 ) ;
g.setColor( Color.WHITE ) ;
g.drawString( "Exit with \"q\"" , x = 0 , y = 10 ) ;
//
// draw random point
g.setColor( getRandomColor() ) ;
x = rand.nextInt( wndSize.width ) ;
y = rand.nextInt( wndSize.height ) ;
g.drawLine( x , y , x , y ) ; // .setPoint(x,y)
} // end while
}
private Color getRandomColor()
{
int r , g , b ;
r = rand.nextInt( 256 ) ;
g = rand.nextInt( 256 ) ;
b = rand.nextInt( 256 ) ;
return (new Color( r , g , b )) ;
}
} ;
myDrawThread.start() ;
}
// ------------------------------------------------------------------------
public static class NotSupportedException extends RuntimeException
{
private static final long serialVersionUID = 1L ;
public NotSupportedException( String descr )
{
super( descr ) ;
}
}
}