Skip to content

Instantly share code, notes, and snippets.

@AnnaBoro
Created January 21, 2016 22:27
Show Gist options
  • Save AnnaBoro/8e99b974cb7e58df1d7a to your computer and use it in GitHub Desktop.
Save AnnaBoro/8e99b974cb7e58df1d7a to your computer and use it in GitHub Desktop.
package lesson5_8.Homework;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class AngryCircle extends JPanel {
private int xCircle;
private int yCircle;
private final int radius = 50;
MyCircle circle = new MyCircle();
public AngryCircle() {
JFrame frame = new JFrame("Angry Circle");
frame.setMinimumSize(new Dimension(500, 500));
frame.setLocation(300, 300);
frame.getContentPane().add(circle);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new AngryCircle();
}
private class MyCircle extends JComponent implements MouseMotionListener {
public MyCircle() {
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
xCircle = (int) (Math.random() * 300);
yCircle = (int) (Math.random() * 300);
g.fillOval(xCircle, yCircle, radius, radius);
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
if (e.getX() > xCircle - 1 && e.getX() < xCircle + radius + 1 &&
e.getY() > yCircle - 1 && e.getY() < yCircle + radius + 1 ) {
repaint();
}
// System.out.println(e.getX());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment