Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class Svetluska extends JComponent implements MouseListener {
- Timer timer;
- protected int seconds = 1;
- protected int stav;
- protected boolean broken = false;
- protected boolean let = false;
- int rychlost = 2;
- int posx;
- int posy;
- private javax.swing.JFrame mojKontajner;
- public Color color = Color.green;
- public Svetluska() {
- addMouseListener(this);
- setBounds(125, 50, 50, 50);
- timer = new Timer(100, new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (seconds % 2 == 1) {
- stav = 1;
- } else {
- stav = 2;
- }
- int x = (int) (Math.random() * 2);
- int y = (int) (Math.random() * 2);
- if (x == 1 && y == 1) {
- posx = getX() + rychlost;
- posy = getY() - rychlost;
- } else if (x == 0 && y == 1) {
- posx = getX() - rychlost;
- posy = getY() + rychlost;
- } else if (x == 1 && y == 0) {
- posx = getX() + rychlost;
- posy = getY() + rychlost;
- } else if (x == 0 && y == 0) {
- posx = getX() - rychlost;
- posy = getY() - rychlost;
- }
- if (posx > 0 && posx < 300 && posy > 0 && posy < 300) {
- setBounds(posx, posy, 30, 30);
- } else {
- setBounds(getX(), getY(), 30, 30);
- }
- repaint();
- seconds++;
- }
- });
- timer.start();
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.black);
- g.fillRect(0, 0, 10, 20);
- g.setColor(Color.black);
- g.fillOval(0, 18, 10, 10);
- if (broken) {
- if (stav == 1) {
- g.setColor(Color.yellow);
- g.fillRect(0, 0, 10, 20);
- g.setColor(Color.yellow);
- g.fillOval(0, 18, 10, 10);
- } else {
- g.setColor(Color.black);
- g.fillRect(0, 0, 10, 20);
- g.setColor(Color.black);
- g.fillOval(0, 18, 10, 10);
- }
- } else if (let) {
- if (stav == 1) {
- g.setColor(Color.yellow);
- g.fillRect(0, 0, 10, 20);
- g.setColor(Color.black);
- g.fillOval(0, 18, 10, 10);
- } else {
- g.setColor(color);
- g.fillRect(0, 0, 10, 20);
- g.setColor(color);
- g.fillOval(0, 18, 10, 10);
- }
- } else {
- if (stav == 1) {
- g.setColor(Color.yellow);
- g.fillRect(0, 0, 10, 20);
- g.setColor(Color.black);
- g.fillOval(0, 18, 10, 10);
- } else {
- g.setColor(Color.black);
- g.fillRect(0, 0, 10, 20);
- g.setColor(Color.black);
- g.fillOval(0, 18, 10, 10);
- }
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getButton() == MouseEvent.BUTTON1) {
- if (!broken) {
- broken = true;
- repaint();
- } else {
- broken = false;
- repaint();
- }
- } else if (e.getButton() == MouseEvent.BUTTON3) {
- timer.stop();
- setBounds(0, 0, 0, 0);
- }
- }
- @Override
- public void mousePressed(MouseEvent e) {
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- let = true;
- }
- @Override
- public void mouseExited(MouseEvent e) {
- let = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement