Kladdy

Untitled

Jan 11th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseMotionListener;
  8. import java.awt.geom.Ellipse2D;
  9.  
  10.  
  11. public class MouseMotionListenerTutorial
  12. extends Applet
  13. implements MouseMotionListener{
  14.    
  15.     private Graphics globalGraphics;
  16.    
  17.     public void init()
  18.     {
  19.         this.addMouseMotionListener(this);
  20.     }
  21.    
  22.     public void paint(Graphics g)
  23.     {
  24.         this.setSize(new Dimension(500, 500));
  25.        
  26.         globalGraphics = g.create();
  27.        
  28.        
  29.     }
  30.    
  31.     public void drawCircle(int x, int y, int width, int height)
  32.     {
  33.         globalGraphics.setColor(getRandomColor());
  34.         Ellipse2D circle = new Ellipse2D.Double((double)x, (double)y, (double)width, (double)height);
  35.         Graphics2D g2 = (Graphics2D) globalGraphics;
  36.         g2.fill(circle);
  37.        
  38.        
  39.     }
  40.     public Color getRandomColor()
  41.     {
  42.         int red = (int) (Math.random() * 256);
  43.         int green = (int) (Math.random() * 256);
  44.         int blue = (int) (Math.random() * 256);
  45.        
  46.         return new Color(red, green, blue);
  47.     }
  48.  
  49.     @Override
  50.     public void mouseDragged(MouseEvent e) {
  51.         int mouseX = e.getX();
  52.         int mouseY = e.getY();
  53.        
  54.         drawCircle(mouseX, mouseY, 20, 20);
  55.        
  56.     }
  57.  
  58.     @Override
  59.     public void mouseMoved(MouseEvent e) {
  60.        
  61.        
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment