Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.swing.internal.plaf.metal.resources.metal;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.Timer;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author sused
- */
- public class Gula extends JComponent implements ActionListener, MouseListener
- {
- private int stav;
- private Timer t;
- private boolean smerDolava;
- private JFrame mojKontajner;
- public Gula(int x, int y, JFrame mojKontajner) {
- this.mojKontajner=mojKontajner;
- setBounds(x, y, 50, 50);
- stav = 1;
- smerDolava = true;
- t = new Timer(100, this);
- addMouseListener(this);
- //t.start();
- }
- public void zmenaSmeru() {
- smerDolava = !smerDolava;
- }
- public void dolava() {
- setLocation(getX() - 10, getY());
- if (getX()<-50) setLocation(mojKontajner.getWidth(), getY());
- stav--;
- if (stav == 0) {
- stav = 4;
- }
- }
- public void doprava() {
- setLocation(getX() + 10, getY());
- if (getX() > mojKontajner.getWidth()) setLocation(-50, getY());
- stav++;
- if (stav == 5) {
- stav = 1;
- }
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.red);
- g.fillOval(0, 0, 50, 50);
- g.setColor(Color.yellow);
- switch (stav) {
- case 1:
- g.fillOval(20, 5, 10, 10);
- break;
- case 2:
- g.fillOval(35, 20, 10, 10);
- break;
- case 3:
- g.fillOval(20, 35, 10, 10);
- break;
- case 4:
- g.fillOval(5, 20, 10, 10);
- break;
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (smerDolava) {
- dolava();
- } else {
- doprava();
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {}
- @Override
- public void mousePressed(MouseEvent e) {
- if (e.getButton() == 1)
- t.start();
- if (e.getButton() == 3)
- t.stop( );
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement