package screen.saver.project;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ScreenSaver extends JPanel implements Runnable
{
private Random random = new Random();
//Diameter of shape
int diameter = 50;
//Random Placement generator for shape1
private int x1 = random.nextInt(400);
private int y1 = random.nextInt(400);
//Random Placement generator for shape2
private int x2 = random.nextInt(400);
private int y2 = random.nextInt(400);
//Random Placement generator for shape3
private int x3 = random.nextInt(400);
private int y3 = random.nextInt(400);
//Random Placement generator for shape4
private int x4 = random.nextInt(400);
private int y4 = random.nextInt(400);
//Random Placement generator for shape5
private int x5 = random.nextInt(400);
private int y5 = random.nextInt(400);
//Random Movement generator for shape1
private int directionX1 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
private int directionY1 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
//Random Movement generator for shape2
private int directionX2 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
private int directionY2 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
//Random Movement generator for shape3
private int directionX3 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
private int directionY3 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
//Random Movement generator for shape4
private int directionX4 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
private int directionY4 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
//Random Movement generator for shape5
private int directionX5 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
private int directionY5 = ( ( ( random.nextInt(4) - 2 ) *2 ) +1 );
//Random color generator for shape1
int red1 = random.nextInt(256);
int blue1 = random.nextInt(256);
int green1 = random.nextInt(256);
Color Color1 = new Color( red1, blue1, green1 );
//Random color generator for shape2
int red2 = random.nextInt(256);
int blue2 = random.nextInt(256);
int green2 = random.nextInt(256);
Color Color2 = new Color( red2, blue2, green2 );
//Random color generator for shape3
int red3 = random.nextInt(256);
int blue3 = random.nextInt(256);
int green3 = random.nextInt(256);
Color Color3 = new Color( red3, blue3, green3 );
//Random color generator for shape4
int red4 = random.nextInt(256);
int blue4 = random.nextInt(256);
int green4 = random.nextInt(256);
Color Color4 = new Color( red4, blue4, green4 );
//Random color generator for shape5
int red5 = random.nextInt(256);
int blue5 = random.nextInt(256);
int green5 = random.nextInt(256);
Color Color5 = new Color( red5, blue5, green5 );
//Random shape generators
int Type_Of_Shape1 = 1 + random.nextInt( 2 );
int Type_Of_Shape2 = 1 + random.nextInt( 2 );
int Type_Of_Shape3 = 1 + random.nextInt( 2 );
int Type_Of_Shape4 = 1 + random.nextInt( 2 );
int Type_Of_Shape5 = 1 + random.nextInt( 2 );
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//Random Shape & Color1
g.setColor( Color1 );
if( Type_Of_Shape1 == 1 )
{
g.fillOval( x1, y1, 50, 50 );
}
if( Type_Of_Shape1 == 2 )
{
g.fillRect( x1, y1, 50, 50 );
}
//Random Shape & Color2
g.setColor( Color2 );
if( Type_Of_Shape2 == 1 )
{
g.fillOval( x2, y2, 50, 50 );
}
if( Type_Of_Shape2 == 2 )
{
g.fillRect( x2, y2, 50, 50 );
}
//Random Shape & Color3
g.setColor( Color3 );
if( Type_Of_Shape3 == 1 )
{
g.fillOval( x3, y3, 50, 50 );
}
if( Type_Of_Shape3 == 2 )
{
g.fillRect( x3, y3, 50, 50 );
}
//Random Shape & Color4
g.setColor( Color4 );
if( Type_Of_Shape4 == 1 )
{
g.fillOval( x4, y4, 50, 50 );
}
if( Type_Of_Shape4 == 2 )
{
g.fillRect( x4, y4, 50, 50 );
}
//Random Shape & Color5
g.setColor( Color5 );
if( Type_Of_Shape5 == 1 )
{
g.fillOval( x5, y5, 50, 50 );
}
if( Type_Of_Shape5 == 2 )
{
g.fillRect( x5, y5, 50, 50 );
}
}
public void run()
{
while(isVisible())
{
try
{
Thread.sleep(15);
} catch(InterruptedException e)
{
System.out.println("interrupted");
}
move();
repaint();
}
}
public void move()
{
//Move object1 & bounce off perimeter
if( x1 + directionX1 < 0 || x1 + diameter + directionX1 > getWidth())
{
directionX1 *= -1;
}
if( y1 + directionY1 < 0 || y1 + diameter + directionY1 > getHeight())
{
directionY1 *= -1;
}
x1 += directionX1;
y1 += directionY1;
//Move object2 & bounce off perimeter
if( x2 + directionX2 < 0 || x2 + diameter + directionX2 > getWidth())
{
directionX2 *= -1;
}
if( y2 + directionY2 < 0 || y2 + diameter + directionY2 > getHeight())
{
directionY2 *= -1;
}
x2 += directionX2;
y2 += directionY2;
//Move object3 & bounce off perimeter
if( x3 + directionX3 < 0 || x3 + diameter + directionX3 > getWidth())
{
directionX3 *= -1;
}
if( y3 + directionY3 < 0 || y3 + diameter + directionY3 > getHeight())
{
directionY3 *= -1;
}
x3 += directionX3;
y3 += directionY3;
//Move object4 & bounce off perimeter
if( x4 + directionX4 < 0 || x4 + diameter + directionX4 > getWidth())
{
directionX4 *= -1;
}
if( y4 + directionY4 < 0 || y4 + diameter + directionY4 > getHeight())
{
directionY4 *= -1;
}
x4 += directionX4;
y4 += directionY4;
//Move object5 & bounce off perimeter
if( x5 + directionX5 < 0 || x5 + diameter + directionX5 > getWidth())
{
directionX5 *= -1;
}
if( y5 + directionY5 < 0 || y5 + diameter + directionY5 > getHeight())
{
directionY5 *= -1;
}
x5 += directionX5;
y5 += directionY5;
}
private void start()
{
while(!isVisible())
{
try
{
Thread.sleep( 15 );
}
catch( InterruptedException e )
{
System.out.println( "Screen Saver Closed" );
}
}
Thread Thread = new Thread( this );
Thread.start();
}
public static void main(String[] args)
{
ScreenSaver test = new ScreenSaver();
JFrame frame = new JFrame( "Screen Saver" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( test );
frame.setSize( 500,500 );
frame.setVisible( true );
test.start();
}
}