Crenox

Moving and Displaying an Image in Java

Apr 27th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. /* REMEMBER, IF YOU WANT THE IMAGE TO APPEAR YOU MUST PUT THE PICTURE INSIDE OF ECLIPSE WHICH MEANS YOU DRAG
  2. THE IMAGE INTO THE SRC OF THE ECLIPSE. */
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. public class Sam extends JApplet implements MouseMotionListener, MouseListener, KeyListener
  9. {
  10. int xPos = 0;
  11. int yPos = 0; // Position of Icon
  12. int width, height; // Dimensions of Icon
  13. Image pic;
  14. int xDis = 0;
  15. int yDis = 0; // Displacement of Icon while dragged
  16. int mousex = 0;
  17. int mousey = 0;
  18.  
  19. int x = 5;
  20. int y = 5;
  21.  
  22. Point mousePosition;
  23.  
  24. public void init()
  25. {
  26. width = 1500; // Where the width is from where you can drag the image
  27. height = 1500; // Where the height is from where you can drag the image
  28. setContentPane(new DrawingPanel());
  29.  
  30. addMouseMotionListener(this);
  31. addMouseListener(this);
  32. addKeyListener(this);
  33. setFocusable(true);
  34.  
  35. setSize(900, 900);
  36.  
  37. pic = getImage(getDocumentBase(), "images.jpg");
  38.  
  39. }
  40.  
  41. public void mouseMoved(MouseEvent e){}
  42.  
  43. public void mousePressed(MouseEvent e)
  44. {
  45. mousePosition = e.getPoint();
  46. mousex = (int)mousePosition.getX();
  47. mousey = (int)mousePosition.getY();
  48. System.out.println(mousex);
  49. System.out.println(mousey);
  50. }
  51.  
  52.  
  53. public void mouseDragged(MouseEvent e)
  54. {
  55.  
  56. if(mousex > xPos
  57. && mousex < xPos + width
  58. && mousey > yPos
  59. && mousey < yPos + height)
  60. {
  61.  
  62. xDis = e.getX()-mousex;
  63. yDis = e.getY()-mousey;
  64. xPos = xPos + xDis;
  65. yPos = yPos + yDis;
  66. repaint();
  67. }
  68.  
  69. mousePosition = e.getPoint();
  70. mousex = (int)mousePosition.getX();
  71. mousey = (int)mousePosition.getY();
  72. }
  73.  
  74. public void mouseClicked(MouseEvent e){}
  75.  
  76. public void mouseReleased(MouseEvent e){}
  77.  
  78. public void mouseEntered (MouseEvent e){}
  79.  
  80. public void mouseExited (MouseEvent e){}
  81.  
  82. public void keyTyped(KeyEvent e){}
  83.  
  84.  
  85. public void keyPressed(KeyEvent e)
  86. {
  87. if (e.getKeyCode() == KeyEvent.VK_UP)
  88. {
  89. if(yPos != 0) // Wall Barriers
  90. {
  91. yPos = yPos - y;
  92. repaint();
  93. }
  94. }
  95. if (e.getKeyCode() == KeyEvent.VK_DOWN)
  96. {
  97. if(yPos != 100) // Wall Barriers
  98. {
  99. yPos = yPos + y;
  100. repaint();
  101. }
  102. }
  103. if (e.getKeyCode() == KeyEvent.VK_LEFT)
  104. {
  105. if(xPos != 0) // Wall Barriers
  106. {
  107. xPos = xPos - x;
  108. repaint();
  109. }
  110. }
  111. if (e.getKeyCode() == KeyEvent.VK_RIGHT)
  112. {
  113. if(xPos != 100) // Wall Barriers
  114. {
  115. xPos = xPos + x;
  116. repaint();
  117. }
  118. }
  119. }
  120. public void keyReleased(KeyEvent e){}
  121.  
  122. @SuppressWarnings("serial")
  123. public class DrawingPanel extends JPanel
  124. {
  125. public void paintComponent(Graphics g)
  126. {
  127. super.paintComponent(g);
  128. g.drawImage(pic, xPos, yPos,this);
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment