Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ImagePanel.java
- *
- * Created on 31 July 2009, 15:19
- */
- package gestureHandout;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.Vector;
- import javax.swing.JPanel;
- /**
- * The ImageSurface does most of the work in this app
- *
- * @author bpt
- */
- public class ImageSurface extends JPanel implements MouseListener,
- MouseMotionListener {
- Vector<AffineImage> images = new Vector<AffineImage>();
- boolean captured = false;
- int prevX, prevY, capturedImage;
- AffineImage image;
- int dx, dy, pivotx, pivoty;
- double newTheta, oldTheta;
- double newHyp, oldHyp;
- double x, y;
- double rotationAngle;
- double xnew, ynew;
- /** Creates new ImageSurface */
- public ImageSurface() {
- initComponents();
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- }
- /**
- * Set the images to display (called from the ImageViewer JFrame)
- *
- * @param bimages
- * the list of BufferedImages to show
- */
- public void setImages(ArrayList<BufferedImage> bimages) {
- images = new Vector<AffineImage>();
- for (BufferedImage bimg : bimages) {
- AffineImage aimg = new AffineImage(bimg);
- images.add(aimg);
- }
- repaint();
- }
- /**
- * Paint the images
- *
- * @param g
- * the Graphics context to paint to
- */
- @Override
- public void paintComponent(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- g2.clearRect(0, 0, getWidth(), getHeight());
- g2.rotate(rotationAngle, pivotx, pivoty);
- for (int i = images.size() - 1; i >= 0; i--) {
- image = images.get(i);
- g2.drawImage(image.getImage(), image.getTransform(), this);
- }
- g2.rotate(-rotationAngle, pivotx, pivoty);
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed"
- // desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
- this.setLayout(layout);
- layout.setHorizontalGroup(layout.createParallelGroup(
- javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400,
- Short.MAX_VALUE));
- layout.setVerticalGroup(layout.createParallelGroup(
- javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300,
- Short.MAX_VALUE));
- }// </editor-fold>//GEN-END:initComponents
- // MouseLister methods
- public void mouseClicked(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- prevX = e.getX();
- prevY = e.getY();
- pivotx = e.getX();
- pivoty = e.getY();
- for (int i = 0; i < images.size(); i++) {
- if (images.get(i).contains(e.getX(), e.getY())) {
- capturedImage = i;
- if (!captured) {
- resortList();
- }
- captured = true;
- return;
- }
- }
- }
- public void resortList() {
- Vector<AffineImage> newImages = new Vector<AffineImage>();
- newImages.add(images.get(capturedImage));
- for (int i = 0; i < images.size(); i++) {
- if (i != capturedImage) {
- newImages.add(images.get(i));
- }
- }
- images = newImages;
- }
- public void mouseReleased(MouseEvent e) {
- captured = false;
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- // MouseMotionListener methods
- public void mouseDragged(MouseEvent e) {
- if (!captured) {
- if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
- dx = e.getX() - prevX;
- dy = e.getY() - prevY;
- for(int i = 0; i < images.size(); i++){
- images.get(i).translate(-dx, -dy);
- }
- prevX = e.getX();
- prevY = e.getY();
- repaint();
- return;
- }
- if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
- oldTheta = Math.atan2(prevY - pivoty, prevX - pivotx);
- newTheta = Math.atan2(e.getY() - pivoty, e.getX() - pivotx);
- newTheta-= oldTheta;
- rotationAngle += newTheta;
- prevX = e.getX();
- prevY = e.getY();
- repaint();
- return;
- }
- }
- if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
- dx = e.getX() - prevX;
- dy = e.getY() - prevY;
- images.get(0).translate(dx, dy);
- prevX = e.getX();
- prevY = e.getY();
- repaint();
- return;
- } else if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
- oldTheta = Math.atan2(prevY - images.get(0).getCentre().getY(),
- prevX - images.get(0).getCentre().getX());
- newTheta = Math.atan2(e.getY() - images.get(0).getCentre().getY(),
- e.getX() - images.get(0).getCentre().getX());
- images.get(0).rotate(newTheta - oldTheta);
- x = Math.pow((images.get(0).getCentre().getX() - prevX), 2);
- y = Math.pow((images.get(0).getCentre().getY() - prevY), 2);
- oldHyp = Math.sqrt(x + y);
- x = Math.pow((images.get(0).getCentre().getX() - e.getX()), 2);
- y = Math.pow((images.get(0).getCentre().getY() - e.getY()), 2);
- newHyp = Math.sqrt(x + y);
- images.get(0).scale(newHyp / oldHyp);
- prevX = e.getX();
- prevY = e.getY();
- repaint();
- return;
- }
- }
- public void mouseMoved(MouseEvent e) {
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- // End of variables declaration//GEN-END:variables
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement