Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.geom.Line2D;
  8. import java.util.Random;
  9. import javax.swing.Timer;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13.  
  14.  
  15. public class ScreenSaver extends JFrame {
  16.  
  17. public static void main(String[] args)
  18. {
  19.  
  20. ScreenSaver ss = new ScreenSaver();
  21. ss.setDefaultCloseOperation(EXIT_ON_CLOSE);
  22. }
  23.  
  24. public ScreenSaver()
  25. {
  26. super("Screen Saver");
  27. setSize(800,600);
  28. ReCanvas();
  29. setVisible(true);
  30. }
  31. public void ReCanvas()
  32. {
  33. SPane spane = new SPane();
  34.  
  35. setContentPane(spane);
  36. repaint();
  37. spane.repaint();
  38.  
  39. System.out.println("ReCanvas'd");
  40.  
  41. }
  42. public class SPane extends JPanel
  43. {
  44. Random r = new Random();
  45. int lineCount = 0;
  46. Timer t;
  47. public SPane()
  48. {
  49. super();
  50. t = new Timer(1000,new ActionListener() {
  51.  
  52. @Override
  53. public void actionPerformed(ActionEvent e) {
  54. // TODO Auto-generated method stub
  55. SPane.this.repaint();
  56. }
  57. });
  58. t.start();
  59.  
  60.  
  61.  
  62. }
  63.  
  64. public void paintComponent(Graphics g)
  65. {
  66. if (lineCount >= 100)
  67. {
  68. t.stop();
  69. ScreenSaver.this.ReCanvas();
  70. return;
  71. }
  72. System.out.println(lineCount);
  73. Graphics2D g2d = (Graphics2D)g;
  74.  
  75. Line2D.Double l2d = new Line2D.Double(
  76. r.nextInt(getWidth()+1),
  77. r.nextInt(getHeight()+1),
  78. r.nextInt(getWidth()+1),
  79. r.nextInt(getHeight()+1)
  80. );
  81. g2d.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
  82. g2d.setStroke(new BasicStroke(r.nextInt(20)+1));
  83. g2d.draw(l2d);
  84. ++lineCount;
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement