Advertisement
Guest User

keus

a guest
Apr 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. package ballz;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.io.File;
  10. import java.io.IOException;
  11.  
  12. import javax.imageio.ImageIO;
  13. import javax.swing.*;
  14.  
  15. public class MyApplication {
  16.  
  17. MyFrame frame = new MyFrame();
  18.  
  19. public static void main(String[] args) {
  20. try {
  21. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. new MyApplication();
  26. }
  27.  
  28. }
  29.  
  30. class MyFrame extends JFrame {
  31.  
  32. MyPanel panel = new MyPanel();
  33.  
  34. public MyFrame() {
  35. this.setTitle("Test Application");
  36. this.setSize(800, 600);
  37. this.setLocationRelativeTo(null);
  38. this.setResizable(false);
  39. this.getContentPane().add(panel);
  40. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. this.setVisible(true);
  42. }
  43.  
  44. }
  45.  
  46. class MyPanel extends JPanel implements MouseListener, MouseMotionListener {
  47.  
  48. private final int IMAGE_WIDTH = 100;
  49. private final int IMAGE_HEIGHT = 100;
  50.  
  51. private Image ball;
  52. private int ballX = 0, ballY = 0;
  53. private int ballXDiff = 0, ballYDiff = 0;
  54. private Timer tm;
  55.  
  56. public MyPanel() {
  57. try {
  58. ball = ImageIO.read(new File("src/ballz/ball.png"));
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. this.addMouseListener(this);
  63. this.addMouseMotionListener(this);
  64. tm = new Timer(10, new ActionListener() {
  65.  
  66. @Override
  67. public void actionPerformed(ActionEvent e) {
  68. repaint();
  69. }
  70. });
  71. }
  72.  
  73. @Override
  74. protected void paintComponent(Graphics g) {
  75. super.paintComponent(g);
  76. g.drawImage(ball, ballX, ballY, null);
  77. }
  78.  
  79. @Override
  80. public void mouseDragged(MouseEvent e) {
  81. if (getCursor().getType() == Cursor.HAND_CURSOR) {
  82. ballX = e.getX() - ballXDiff;
  83. ballY = e.getY() - ballYDiff;
  84. }
  85. }
  86.  
  87. @Override
  88. public void mouseMoved(MouseEvent e) {
  89. if (e.getX() >= ballX && e.getX() <= ballX + 100 && e.getY() >= ballY && e.getY() <= ballY + 100) {
  90. setCursor(new Cursor(Cursor.HAND_CURSOR));
  91. } else {
  92. setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  93. }
  94. }
  95.  
  96. @Override
  97. public void mouseClicked(MouseEvent e) {
  98. }
  99.  
  100. @Override
  101. public void mouseEntered(MouseEvent e) {
  102. }
  103.  
  104. @Override
  105. public void mouseExited(MouseEvent e) {
  106. }
  107.  
  108. @Override
  109. public void mousePressed(MouseEvent e) {
  110. if (getCursor().getType() == Cursor.HAND_CURSOR) {
  111. ballXDiff = e.getX() - ballX;
  112. ballYDiff = e.getY() - ballY;
  113. tm.start();
  114. }
  115. }
  116.  
  117. @Override
  118. public void mouseReleased(MouseEvent e) {
  119. tm.stop();
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement