Mattie

Mattie

Jan 5th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package magicBubble;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.util.Random;
  9.  
  10. import javax.swing.JPanel;
  11.  
  12. public class MagicBubble extends JPanel implements Runnable, MouseListener
  13. {
  14.     private int     breedte, hoogte;
  15.     private boolean bewegen;
  16.     private Thread  beweging;
  17.     Color           bubble  = new Color(47, 110, 176);
  18.     Random          gen;
  19.  
  20.     public MagicBubble(int breedte, int hoogte)
  21.     {
  22.         this.breedte = breedte;
  23.         this.hoogte = hoogte;
  24.         setLayout(null);
  25.         setOpaque(false);
  26.         setSize(breedte, hoogte);
  27.         bewegen = true;
  28.         addMouseListener(this);
  29.         gen = new Random();
  30.     }
  31.  
  32.     public void paintComponent(Graphics g)
  33.     {
  34.         super.paintComponent(g);
  35.  
  36.         g.setColor(bubble);
  37.         g.fillOval(0, 0, breedte - 1, hoogte - 1);
  38.     }
  39.  
  40.     public void setColour(Color c)
  41.     {
  42.         bubble = c;
  43.         repaint();
  44.     }
  45.  
  46.     public void run()
  47.     {
  48.         int snelheid = 4;
  49.  
  50.         while (bewegen)
  51.         {
  52.             Point locatie = getLocation();
  53.             locatie.x += snelheid;
  54.  
  55.             setLocation(locatie.x, locatie.y);
  56.  
  57.             if (locatie.x + getWidth() >= getParent().getWidth() || locatie.x <= 0)
  58.             {
  59.  
  60.                 snelheid = -snelheid;
  61.                 System.out.println(snelheid);
  62.             }
  63.  
  64.             wacht();
  65.         }
  66.     }
  67.  
  68.     public void start()
  69.     {
  70.         beweging = new Thread(this);
  71.         beweging.start();
  72.     }
  73.  
  74.     private void wacht()
  75.     {
  76.         try
  77.         {
  78.             Thread.sleep(50);
  79.         } catch (InterruptedException e)
  80.         {
  81.         }
  82.     }
  83.  
  84.     @Override
  85.     public void mouseClicked(MouseEvent arg0)
  86.     {
  87.         // TODO Auto-generated method stub
  88.  
  89.     }
  90.  
  91.     @Override
  92.     public void mouseEntered(MouseEvent arg0)
  93.     {
  94.         // TODO Auto-generated method stub
  95.  
  96.     }
  97.  
  98.     @Override
  99.     public void mouseExited(MouseEvent arg0)
  100.     {
  101.         // TODO Auto-generated method stub
  102.  
  103.     }
  104.  
  105.     @Override
  106.     public void mousePressed(MouseEvent arg0)
  107.     {
  108.         int r = gen.nextInt(250) + 1;
  109.         int g = gen.nextInt(250) + 1;
  110.         int b = gen.nextInt(250) + 1;
  111.  
  112.         Color c = new Color(r, g, b);
  113.         setColour(c);
  114.  
  115.     }
  116.  
  117.     @Override
  118.     public void mouseReleased(MouseEvent arg0)
  119.     {
  120.         // TODO Auto-generated method stub
  121.  
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment