Advertisement
ljukk

svetls

May 28th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. public class Svetluska extends JComponent implements MouseListener {
  7.  
  8.     Timer timer;
  9.     protected int seconds = 1;
  10.     protected int stav;
  11.     protected boolean broken = false;
  12.     protected boolean let = false;
  13.     int rychlost = 2;
  14.     int posx;
  15.     int posy;
  16.     private javax.swing.JFrame mojKontajner;
  17.     public Color color = Color.green;
  18.  
  19.     public Svetluska() {
  20.  
  21.         addMouseListener(this);
  22.         setBounds(125, 50, 50, 50);
  23.         timer = new Timer(100, new ActionListener() {
  24.             @Override
  25.             public void actionPerformed(ActionEvent e) {
  26.                 if (seconds % 2 == 1) {
  27.                     stav = 1;
  28.                 } else {
  29.                     stav = 2;
  30.                 }
  31.                 int x = (int) (Math.random() * 2);
  32.                 int y = (int) (Math.random() * 2);
  33.  
  34.                 if (x == 1 && y == 1) {
  35.                     posx = getX() + rychlost;
  36.                     posy = getY() - rychlost;
  37.                 } else if (x == 0 && y == 1) {
  38.                     posx = getX() - rychlost;
  39.                     posy = getY() + rychlost;
  40.                 } else if (x == 1 && y == 0) {
  41.                     posx = getX() + rychlost;
  42.                     posy = getY() + rychlost;
  43.                 } else if (x == 0 && y == 0) {
  44.                     posx = getX() - rychlost;
  45.                     posy = getY() - rychlost;
  46.                 }
  47.                 if (posx > 0 && posx < 300 && posy > 0 && posy < 300) {
  48.                     setBounds(posx, posy, 30, 30);
  49.                 } else {
  50.                     setBounds(getX(), getY(), 30, 30);
  51.                 }
  52.                 repaint();
  53.                 seconds++;
  54.  
  55.             }
  56.         });
  57.         timer.start();
  58.     }
  59.  
  60.     @Override
  61.     public void paintComponent(Graphics g) {
  62.         super.paintComponent(g);
  63.         g.setColor(Color.black);
  64.         g.fillRect(0, 0, 10, 20);
  65.         g.setColor(Color.black);
  66.         g.fillOval(0, 18, 10, 10);
  67.         if (broken) {
  68.             if (stav == 1) {
  69.                 g.setColor(Color.yellow);
  70.                 g.fillRect(0, 0, 10, 20);
  71.                 g.setColor(Color.yellow);
  72.                 g.fillOval(0, 18, 10, 10);
  73.             } else {
  74.                 g.setColor(Color.black);
  75.                 g.fillRect(0, 0, 10, 20);
  76.                 g.setColor(Color.black);
  77.                 g.fillOval(0, 18, 10, 10);
  78.             }
  79.         } else if (let) {
  80.             if (stav == 1) {
  81.                 g.setColor(Color.yellow);
  82.                 g.fillRect(0, 0, 10, 20);
  83.                 g.setColor(Color.black);
  84.                 g.fillOval(0, 18, 10, 10);
  85.             } else {
  86.                 g.setColor(color);
  87.                 g.fillRect(0, 0, 10, 20);
  88.                 g.setColor(color);
  89.                 g.fillOval(0, 18, 10, 10);
  90.             }
  91.         } else {
  92.             if (stav == 1) {
  93.                 g.setColor(Color.yellow);
  94.                 g.fillRect(0, 0, 10, 20);
  95.                 g.setColor(Color.black);
  96.                 g.fillOval(0, 18, 10, 10);
  97.             } else {
  98.                 g.setColor(Color.black);
  99.                 g.fillRect(0, 0, 10, 20);
  100.                 g.setColor(Color.black);
  101.                 g.fillOval(0, 18, 10, 10);
  102.             }
  103.         }
  104.  
  105.     }
  106.  
  107.     @Override
  108.     public void mouseClicked(MouseEvent e) {
  109.         if (e.getButton() == MouseEvent.BUTTON1) {
  110.             if (!broken) {
  111.                 broken = true;
  112.                 repaint();
  113.             } else {
  114.                 broken = false;
  115.                 repaint();
  116.             }
  117.         } else if (e.getButton() == MouseEvent.BUTTON3) {
  118.             timer.stop();
  119.             setBounds(0, 0, 0, 0);
  120.         }
  121.     }
  122.  
  123.     @Override
  124.     public void mousePressed(MouseEvent e) {
  125.     }
  126.  
  127.     @Override
  128.     public void mouseReleased(MouseEvent e) {
  129.     }
  130.  
  131.     @Override
  132.     public void mouseEntered(MouseEvent e) {
  133.         let = true;
  134.     }
  135.  
  136.     @Override
  137.     public void mouseExited(MouseEvent e) {
  138.         let = false;
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement