Java Screensaver Program
Java Code:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class ScreenSaver extends JPanel implements ActionListener {
private JFrame frame = new JFrame("FullSize");
private final int DELAY = 9999999;
private Timer timer;
private Rectangle rectangle;
boolean full;
private Color color=Color.BLACK;
int i=0;
// constructor sets window's title bar
string and dimensions
public ScreenSaver()
{
frame.setUndecorated(true);
//
Add a Key Listener to the frame
frame.addKeyListener(new KeyHandler());
//
Add this panel object to the frame
frame.add(this);
//
Get the dimensions of the screen
rectangle =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
//
Set the size of the frame to the size of the screen
frame.setSize(rectangle.width, rectangle.height);
frame.setVisible(true);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
setBackground(color);
//
Remember that we are currently at full size
full = true;
timer = new Timer( 1000, this ); // create a new
timer
timer.start();
}
class KeyHandler extends KeyAdapter
{
public void keyPressed(KeyEvent
e)
{
// Terminate the program.
int keyCode= e.getKeyCode();
if(keyCode== KeyEvent.VK_E){
System.exit(0);
}
if(keyCode==KeyEvent.VK_S && i%2==0){
i++;
frame.setSize(rectangle.width/2,rectangle.height/2);
repaint();
}
else if(keyCode==KeyEvent.VK_S && i%2==1){
i++;
frame.setSize(rectangle.width,rectangle.height);
repaint();
}
if(keyCode==KeyEvent.VK_C){
setBackground( new Color( ( float ) Math.random(),
( float ) Math.random(), ( float ) Math.random()
));
repaint();
}
}
}
public void paintComponent (
Graphics g )
{
super.paintComponent ( g );
int shape;
for ( int i = 0; i < 25; i++ ) {
shape = ( int ) ( Math.random()
* 4 ); //
pick a random shape
switch( shape ) {
case 0:
makeLine( g );
break;
case 1:
makeRect( g );
break;
case 2:
makeOval( g );
break;
case 3:
makeRoundRect( g );
break;
}
for ( int q = 1; q < DELAY; q++ ) ;
}
}
public static void main( String args[]
)
{
ScreenSaver run = new ScreenSaver();
}
public void actionPerformed(
ActionEvent actionEvent )
{
repaint();
}
private void makeLine( Graphics g
) {
Graphics2D g2d = ( Graphics2D ) g;
double x = Math.random() * 1600;
double y = Math.random() * 300;
double x1 = Math.random()* 800;
double y1 = Math.random()
* 1500;
g2d.setPaint( new GradientPaint( (int)x, (int)y,
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), (int)x1, (int)y1,
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), true ) );
g2d.draw( new Line2D.Double( x, y,
x1, y1 ) );
}
private void makeRect( Graphics g
) {
Graphics2D g2d = ( Graphics2D ) g;
double x = Math.random() * 1500;
double y = Math.random() * 1600;
double width = Math.random() * 300;
double height = Math.random() * 300;
g2d.setPaint( new GradientPaint( (int)x, (int)y,
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), (int)(x + width), (int)(y + height),
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), true ) );
g2d.draw( new Rectangle2D.Double(
x, y, width, height ) );
}
private void makeOval( Graphics g
) {
Graphics2D g2d = ( Graphics2D ) g;
double x = Math.random() * 800;
double y = Math.random() * 1400;
double width = Math.random() * 300;
double height = Math.random() * 300;
g2d.setPaint( new GradientPaint( (int)x, (int)y,
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), (int)(x + width), (int)(y + height),
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), true ) );
g2d.draw( new Ellipse2D.Double( x,
y, width, height ) );
}
private void makeRoundRect(
Graphics g ) {
Graphics2D g2d = ( Graphics2D ) g;
double x = Math.random() * 1400;
double y = Math.random() * 800;
double width = Math.random() * 200;
double height = Math.random() * 200;
double arcWidth = Math.random() *
width;
double arcHeight = Math.random() *
height;
g2d.setPaint( new GradientPaint( (int)x, (int)y,
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), (int)(x + width), (int)(y + height),
new Color( ( float ) Math.random(),
( float ) Math.random(),
( float ) Math.random() ), true ) );
g2d.draw( new
RoundRectangle2D.Double( x, y, width, height,
arcWidth, arcHeight ) );
}
}
very nice java screensaver program
ReplyDelete