Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.geom.Line2D;
- import java.util.Random;
- import javax.swing.Timer;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class ScreenSaver extends JFrame {
- public static void main(String[] args)
- {
- ScreenSaver ss = new ScreenSaver();
- ss.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public ScreenSaver()
- {
- super("Screen Saver");
- setSize(800,600);
- ReCanvas();
- setVisible(true);
- }
- public void ReCanvas()
- {
- SPane spane = new SPane();
- setContentPane(spane);
- repaint();
- spane.repaint();
- System.out.println("ReCanvas'd");
- }
- public class SPane extends JPanel
- {
- Random r = new Random();
- int lineCount = 0;
- Timer t;
- public SPane()
- {
- super();
- t = new Timer(1000,new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- SPane.this.repaint();
- }
- });
- t.start();
- }
- public void paintComponent(Graphics g)
- {
- if (lineCount >= 100)
- {
- t.stop();
- ScreenSaver.this.ReCanvas();
- return;
- }
- System.out.println(lineCount);
- Graphics2D g2d = (Graphics2D)g;
- Line2D.Double l2d = new Line2D.Double(
- r.nextInt(getWidth()+1),
- r.nextInt(getHeight()+1),
- r.nextInt(getWidth()+1),
- r.nextInt(getHeight()+1)
- );
- g2d.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
- g2d.setStroke(new BasicStroke(r.nextInt(20)+1));
- g2d.draw(l2d);
- ++lineCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement