Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. /*
  2. * ImagePanel.java
  3. *
  4. * Created on 31 July 2009, 15:19
  5. */
  6.  
  7. package gestureHandout;
  8.  
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.RenderingHints;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.awt.geom.AffineTransform;
  16. import java.awt.image.BufferedImage;
  17. import java.util.ArrayList;
  18. import java.util.Vector;
  19.  
  20. import javax.swing.JPanel;
  21.  
  22. /**
  23. * The ImageSurface does most of the work in this app
  24. *
  25. * @author bpt
  26. */
  27. public class ImageSurface extends JPanel implements MouseListener,
  28. MouseMotionListener {
  29. Vector<AffineImage> images = new Vector<AffineImage>();
  30.  
  31. boolean captured = false;
  32. int prevX, prevY, capturedImage;
  33. AffineImage image;
  34. int dx, dy, pivotx, pivoty;
  35. double newTheta, oldTheta;
  36. double newHyp, oldHyp;
  37. double x, y;
  38. double rotationAngle;
  39. double xnew, ynew;
  40.  
  41. /** Creates new ImageSurface */
  42. public ImageSurface() {
  43. initComponents();
  44. this.addMouseListener(this);
  45. this.addMouseMotionListener(this);
  46. }
  47.  
  48. /**
  49. * Set the images to display (called from the ImageViewer JFrame)
  50. *
  51. * @param bimages
  52. * the list of BufferedImages to show
  53. */
  54. public void setImages(ArrayList<BufferedImage> bimages) {
  55. images = new Vector<AffineImage>();
  56. for (BufferedImage bimg : bimages) {
  57. AffineImage aimg = new AffineImage(bimg);
  58. images.add(aimg);
  59. }
  60. repaint();
  61. }
  62.  
  63. /**
  64. * Paint the images
  65. *
  66. * @param g
  67. * the Graphics context to paint to
  68. */
  69. @Override
  70. public void paintComponent(Graphics g) {
  71. Graphics2D g2 = (Graphics2D) g;
  72. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  73. RenderingHints.VALUE_ANTIALIAS_ON);
  74. g2.clearRect(0, 0, getWidth(), getHeight());
  75. g2.rotate(rotationAngle, pivotx, pivoty);
  76. for (int i = images.size() - 1; i >= 0; i--) {
  77. image = images.get(i);
  78. g2.drawImage(image.getImage(), image.getTransform(), this);
  79.  
  80. }
  81. g2.rotate(-rotationAngle, pivotx, pivoty);
  82. }
  83.  
  84. /**
  85. * This method is called from within the constructor to initialize the form.
  86. * WARNING: Do NOT modify this code. The content of this method is always
  87. * regenerated by the Form Editor.
  88. */
  89. @SuppressWarnings("unchecked")
  90. // <editor-fold defaultstate="collapsed"
  91. // desc="Generated Code">//GEN-BEGIN:initComponents
  92. private void initComponents() {
  93.  
  94. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  95. this.setLayout(layout);
  96. layout.setHorizontalGroup(layout.createParallelGroup(
  97. javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400,
  98. Short.MAX_VALUE));
  99. layout.setVerticalGroup(layout.createParallelGroup(
  100. javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300,
  101. Short.MAX_VALUE));
  102. }// </editor-fold>//GEN-END:initComponents
  103.  
  104. // MouseLister methods
  105.  
  106. public void mouseClicked(MouseEvent e) {
  107.  
  108. }
  109.  
  110. public void mousePressed(MouseEvent e) {
  111. prevX = e.getX();
  112. prevY = e.getY();
  113. pivotx = e.getX();
  114. pivoty = e.getY();
  115. for (int i = 0; i < images.size(); i++) {
  116. if (images.get(i).contains(e.getX(), e.getY())) {
  117.  
  118. capturedImage = i;
  119. if (!captured) {
  120. resortList();
  121. }
  122. captured = true;
  123.  
  124.  
  125. return;
  126. }
  127. }
  128.  
  129. }
  130.  
  131. public void resortList() {
  132. Vector<AffineImage> newImages = new Vector<AffineImage>();
  133. newImages.add(images.get(capturedImage));
  134. for (int i = 0; i < images.size(); i++) {
  135. if (i != capturedImage) {
  136. newImages.add(images.get(i));
  137. }
  138. }
  139. images = newImages;
  140. }
  141.  
  142. public void mouseReleased(MouseEvent e) {
  143. captured = false;
  144. }
  145.  
  146. public void mouseEntered(MouseEvent e) {
  147. }
  148.  
  149. public void mouseExited(MouseEvent e) {
  150. }
  151.  
  152. // MouseMotionListener methods
  153.  
  154. public void mouseDragged(MouseEvent e) {
  155.  
  156. if (!captured) {
  157. if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
  158. dx = e.getX() - prevX;
  159. dy = e.getY() - prevY;
  160.  
  161. for(int i = 0; i < images.size(); i++){
  162. images.get(i).translate(-dx, -dy);
  163. }
  164. prevX = e.getX();
  165. prevY = e.getY();
  166. repaint();
  167. return;
  168. }
  169.  
  170. if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
  171. oldTheta = Math.atan2(prevY - pivoty, prevX - pivotx);
  172. newTheta = Math.atan2(e.getY() - pivoty, e.getX() - pivotx);
  173.  
  174.  
  175. newTheta-= oldTheta;
  176. rotationAngle += newTheta;
  177.  
  178. prevX = e.getX();
  179. prevY = e.getY();
  180. repaint();
  181.  
  182. return;
  183. }
  184. }
  185.  
  186. if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
  187. dx = e.getX() - prevX;
  188. dy = e.getY() - prevY;
  189.  
  190. images.get(0).translate(dx, dy);
  191.  
  192.  
  193.  
  194. prevX = e.getX();
  195. prevY = e.getY();
  196. repaint();
  197. return;
  198. } else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
  199. oldTheta = Math.atan2(prevY - images.get(0).getCentre().getY(),
  200. prevX - images.get(0).getCentre().getX());
  201. newTheta = Math.atan2(e.getY() - images.get(0).getCentre().getY(),
  202. e.getX() - images.get(0).getCentre().getX());
  203.  
  204. images.get(0).rotate(newTheta - oldTheta);
  205.  
  206. x = Math.pow((images.get(0).getCentre().getX() - prevX), 2);
  207. y = Math.pow((images.get(0).getCentre().getY() - prevY), 2);
  208. oldHyp = Math.sqrt(x + y);
  209.  
  210. x = Math.pow((images.get(0).getCentre().getX() - e.getX()), 2);
  211. y = Math.pow((images.get(0).getCentre().getY() - e.getY()), 2);
  212. newHyp = Math.sqrt(x + y);
  213.  
  214. images.get(0).scale(newHyp / oldHyp);
  215.  
  216. prevX = e.getX();
  217. prevY = e.getY();
  218. repaint();
  219. return;
  220. }
  221. }
  222.  
  223. public void mouseMoved(MouseEvent e) {
  224.  
  225. }
  226.  
  227. // Variables declaration - do not modify//GEN-BEGIN:variables
  228. // End of variables declaration//GEN-END:variables
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement