Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package Frame4;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseMotionListener;
  7.  
  8. /**
  9. * Created by Иван on 02.07.2015.
  10. */
  11. public class AngryFriend extends JPanel {
  12. private int circleH;
  13. private int circleV;
  14. private int x = 330;
  15. private int y = 230;
  16. private Point p;
  17.  
  18. public AngryFriend() {
  19.  
  20. JFrame f = new JFrame ("Cunning circle");
  21. f.setBounds(300,100,800,600);
  22. f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  23. f.getContentPane().add(this);
  24.  
  25. f.setVisible(true);
  26. p = new Point(170,130);
  27.  
  28. MouseMotionListener mml = new MouseMotionListener() {
  29. @Override
  30. public void mouseDragged(MouseEvent e) {
  31.  
  32. }
  33.  
  34. @Override
  35. public void mouseMoved(MouseEvent e) {
  36. if (e.getX() >= x && e.getX() <= x +110 && e.getY() >= y && e.getY() <= y +110) {
  37. if (e.getX() < x+50) {
  38. x+=35;
  39. }
  40. if (e.getX() > x+50) {
  41. x-=35;
  42. }
  43. if (e.getY() > y+50) {
  44. y-=35;
  45. }
  46. if (e.getY() < y+50) {
  47. y+=35;
  48. }
  49. if (x > 699) {
  50. x-=150;
  51. } if ( x <= 0 ) {
  52. x += 150;
  53. }
  54. if (y > 499) {
  55. y -=150;
  56. }
  57. if ( y <=0) {
  58. y +=150;
  59. }
  60.  
  61.  
  62. System.out.println("Mouse: " + e.getX() + " " + e.getY() + " circle: " + x + " " + y);
  63. repaint();
  64. }
  65.  
  66. }
  67. };
  68.  
  69. f.addMouseMotionListener(mml);
  70.  
  71. }
  72.  
  73. @Override
  74. protected void paintComponent(Graphics g) {
  75. super.paintComponent(g);
  76.  
  77.  
  78.  
  79. g.setColor(Color.RED);
  80. g.fillOval(x,y,100,100);
  81.  
  82. g.setColor(Color.BLUE);
  83. g.setFont(new Font("Hobo Std",2,40));
  84. g.drawString("Click on the Circle", 220,100);
  85.  
  86. }
  87.  
  88.  
  89. public int getCircleH() {
  90. return circleH;
  91. }
  92.  
  93. public void setCircleH(int circleH) {
  94. this.circleH = circleH;
  95. }
  96.  
  97.  
  98. public int getcircleV() {
  99. return circleV;
  100. }
  101.  
  102. public void setCircleV(int circleV) {
  103. this.circleV = circleV;
  104. }
  105.  
  106. public static void main(String[] args) {
  107. AngryFriend af = new AngryFriend();
  108.  
  109.  
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement