Guest User

Untitled

a guest
Nov 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class Tube extends JPanel implements Runnable{
  5.  
  6. private int x = 540;
  7. private int dx = 1;
  8. private int bottom_line;
  9.  
  10. @Override
  11. public void run() {
  12. while(x!=-81){
  13. x -= dx;
  14. repaint();
  15. try {
  16. Thread.sleep(2);
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. x = 540;
  22. start();
  23. }
  24.  
  25. public void start(){
  26. bottom_line = (int)(100+Math.random()*370);
  27. Thread thread = new Thread(this);
  28. thread.start();
  29. }
  30.  
  31. @Override
  32. protected void paintComponent(Graphics g) {
  33. super.paintComponent(g);
  34. g.drawRect(x,0,80,bottom_line);
  35. g.drawRect(x,bottom_line+150,80,600-bottom_line);
  36. }
  37.  
  38. }
  39.  
  40. import javax.swing.*;
  41.  
  42. public class Window extends JFrame{
  43.  
  44. public Window(){
  45. Tube tube = new Tube();
  46. add(tube);
  47. tube.start();
  48. setVisible(true);
  49. setSize(540,720);
  50. setDefaultCloseOperation(EXIT_ON_CLOSE);
  51. setResizable(false);
  52. setLocationRelativeTo(null);
  53.  
  54. }
  55.  
  56. public static void main(String[] args) {
  57. Window window = new Window();
  58. }
  59. }
Add Comment
Please, Sign In to add comment