Advertisement
ljukk

gula-trieda

May 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. import com.sun.swing.internal.plaf.metal.resources.metal;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import javax.swing.JComponent;
  9. import javax.swing.JFrame;
  10. import javax.swing.Timer;
  11.  
  12. /*
  13.  * To change this license header, choose License Headers in Project Properties.
  14.  * To change this template file, choose Tools | Templates
  15.  * and open the template in the editor.
  16.  */
  17. /**
  18.  *
  19.  * @author sused
  20.  */
  21. public class Gula extends JComponent implements ActionListener, MouseListener
  22.  {
  23.  
  24.     private int stav;
  25.     private Timer t;
  26.     private boolean smerDolava;
  27.     private JFrame mojKontajner;
  28.    
  29.     public Gula(int x, int y, JFrame mojKontajner) {
  30.         this.mojKontajner=mojKontajner;
  31.         setBounds(x, y, 50, 50);
  32.         stav = 1;
  33.         smerDolava = true;
  34.         t = new Timer(100, this);
  35.         addMouseListener(this);
  36.         //t.start();
  37.     }
  38.    
  39.     public void zmenaSmeru() {
  40.         smerDolava = !smerDolava;
  41.    
  42.     }
  43.  
  44.     public void dolava() {
  45.         setLocation(getX() - 10, getY());
  46.         if (getX()<-50) setLocation(mojKontajner.getWidth(), getY());
  47.         stav--;
  48.         if (stav == 0) {
  49.             stav = 4;
  50.         }
  51.     }
  52.  
  53.     public void doprava() {
  54.         setLocation(getX() + 10, getY());
  55.         if (getX() > mojKontajner.getWidth()) setLocation(-50, getY());
  56.         stav++;
  57.         if (stav == 5) {
  58.             stav = 1;
  59.         }
  60.  
  61.     }
  62.  
  63.     @Override
  64.     protected void paintComponent(Graphics g) {
  65.         super.paintComponent(g);
  66.         g.setColor(Color.red);
  67.         g.fillOval(0, 0, 50, 50);
  68.         g.setColor(Color.yellow);
  69.         switch (stav) {
  70.             case 1:
  71.                 g.fillOval(20, 5, 10, 10);
  72.                 break;
  73.             case 2:
  74.                 g.fillOval(35, 20, 10, 10);
  75.                 break;
  76.             case 3:
  77.                 g.fillOval(20, 35, 10, 10);
  78.                 break;
  79.             case 4:
  80.                 g.fillOval(5, 20, 10, 10);
  81.                 break;
  82.  
  83.         }
  84.  
  85.     }
  86.  
  87.     @Override
  88.     public void actionPerformed(ActionEvent e) {
  89.         if (smerDolava) {
  90.             dolava();
  91.  
  92.         } else {
  93.             doprava();
  94.         }
  95.  
  96.     }
  97.  
  98.     @Override
  99.     public void mouseClicked(MouseEvent e) {}
  100.  
  101.     @Override
  102.     public void mousePressed(MouseEvent e) {
  103.         if (e.getButton() == 1)
  104.              t.start();
  105.         if  (e.getButton() == 3)      
  106.              t.stop( );
  107.     }
  108.  
  109.     @Override
  110.     public void mouseReleased(MouseEvent e) {
  111.    
  112.     }
  113.  
  114.     @Override
  115.     public void mouseEntered(MouseEvent e) {
  116.    
  117.     }
  118.  
  119.     @Override
  120.     public void mouseExited(MouseEvent e) {}
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement