Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Frame4;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- /**
- * Created by Иван on 02.07.2015.
- */
- public class AngryFriend extends JPanel {
- private int circleH;
- private int circleV;
- private int x = 330;
- private int y = 230;
- private Point p;
- public AngryFriend() {
- JFrame f = new JFrame ("Cunning circle");
- f.setBounds(300,100,800,600);
- f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- f.getContentPane().add(this);
- f.setVisible(true);
- p = new Point(170,130);
- MouseMotionListener mml = new MouseMotionListener() {
- @Override
- public void mouseDragged(MouseEvent e) {
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- if (e.getX() >= x && e.getX() <= x +110 && e.getY() >= y && e.getY() <= y +110) {
- if (e.getX() < x+50) {
- x+=35;
- }
- if (e.getX() > x+50) {
- x-=35;
- }
- if (e.getY() > y+50) {
- y-=35;
- }
- if (e.getY() < y+50) {
- y+=35;
- }
- if (x > 699) {
- x-=150;
- } if ( x <= 0 ) {
- x += 150;
- }
- if (y > 499) {
- y -=150;
- }
- if ( y <=0) {
- y +=150;
- }
- System.out.println("Mouse: " + e.getX() + " " + e.getY() + " circle: " + x + " " + y);
- repaint();
- }
- }
- };
- f.addMouseMotionListener(mml);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.setColor(Color.RED);
- g.fillOval(x,y,100,100);
- g.setColor(Color.BLUE);
- g.setFont(new Font("Hobo Std",2,40));
- g.drawString("Click on the Circle", 220,100);
- }
- public int getCircleH() {
- return circleH;
- }
- public void setCircleH(int circleH) {
- this.circleH = circleH;
- }
- public int getcircleV() {
- return circleV;
- }
- public void setCircleV(int circleV) {
- this.circleV = circleV;
- }
- public static void main(String[] args) {
- AngryFriend af = new AngryFriend();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement