Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- public class Tube extends JPanel implements Runnable{
- private int x = 540;
- private int dx = 1;
- private int bottom_line;
- @Override
- public void run() {
- while(x!=-81){
- x -= dx;
- repaint();
- try {
- Thread.sleep(2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- x = 540;
- start();
- }
- public void start(){
- bottom_line = (int)(100+Math.random()*370);
- Thread thread = new Thread(this);
- thread.start();
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawRect(x,0,80,bottom_line);
- g.drawRect(x,bottom_line+150,80,600-bottom_line);
- }
- }
- import javax.swing.*;
- public class Window extends JFrame{
- public Window(){
- Tube tube = new Tube();
- add(tube);
- tube.start();
- setVisible(true);
- setSize(540,720);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setResizable(false);
- setLocationRelativeTo(null);
- }
- public static void main(String[] args) {
- Window window = new Window();
- }
- }
Add Comment
Please, Sign In to add comment