Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.Ellipse2D;
- public class MouseMotionListenerTutorial
- extends Applet
- implements MouseMotionListener{
- private Graphics globalGraphics;
- public void init()
- {
- this.addMouseMotionListener(this);
- }
- public void paint(Graphics g)
- {
- this.setSize(new Dimension(500, 500));
- globalGraphics = g.create();
- }
- public void drawCircle(int x, int y, int width, int height)
- {
- globalGraphics.setColor(getRandomColor());
- Ellipse2D circle = new Ellipse2D.Double((double)x, (double)y, (double)width, (double)height);
- Graphics2D g2 = (Graphics2D) globalGraphics;
- g2.fill(circle);
- }
- public Color getRandomColor()
- {
- int red = (int) (Math.random() * 256);
- int green = (int) (Math.random() * 256);
- int blue = (int) (Math.random() * 256);
- return new Color(red, green, blue);
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- int mouseX = e.getX();
- int mouseY = e.getY();
- drawCircle(mouseX, mouseY, 20, 20);
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment