import java.awt.* ;
import java.awt.event.KeyAdapter ;
import java.awt.event.KeyEvent ;
import java.util.Random ;
import javax.swing.JFrame ;
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 ) ;
this.enableInputMethods( false ) ;
switchToFullScreen() ;
}
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 ;
}
}
} ) ;
}
private void switchToFullScreen()
{
myDisplay = getGraphicDevice() ;
if( !myDisplay.isFullScreenSupported() )
throw(new NotSupportedException(
"Graphics device does not support 'full screen' mode." )) ;
try
{
myDisplay.setFullScreenWindow( this ) ;
}
catch( Throwable e )
{
throw(new IllegalStateException(
"Unable to switch to 'full screen' mode, but was told to be supported." ,
e )) ;
}
}
@Override
public void setVisible( boolean mode )
{
if( !mode ) myDisplay.setFullScreenWindow( null ) ;
super.setVisible( mode ) ;
}
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 ] ;
return myDisplay ;
}
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 ;
g.setColor( Color.BLACK ) ;
g.fillRect( 0 , 0 , wndSize.width , wndSize.height ) ;
while( !interrupted() )
{
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 ) ;
g.setColor( getRandomColor() ) ;
x = rand.nextInt( wndSize.width ) ;
y = rand.nextInt( wndSize.height ) ;
g.drawLine( x , y , x , y ) ;
}
}
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 ) ;
}
}
}