Java Graphics2D,缩放形状和鼠标

时间:2013-10-23 13:40:33

标签: java swing jpanel mouseevent paint

大家好我有一段代码可以将某些形状缩放到限制区域但是当缩放时我无法点击形状,我该怎么做?我可以点击原始位置的旧区域。

代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class ZoomDemo extends JPanel implements Runnable {

AffineTransform tx = new AffineTransform();
Rectangle2D.Double rect1 = new Rectangle2D.Double(100, 100, 30, 60);
Rectangle2D.Double rect2 = new Rectangle2D.Double(150, 250, 60, 40);

public ZoomDemo() {
    this.addMouseWheelListener(new ZoomHandler());
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;

    g2.drawRect(100, 100, 100, 100);
    g2.setClip(100, 100, 100, 100);
    g2.setColor(Color.RED);
    g2.draw(tx.createTransformedShape(rect1));
    g2.setColor(Color.BLUE);
    g2.draw(tx.createTransformedShape(rect2));
}

private class ZoomHandler implements MouseWheelListener, MouseListener {

    double scale = 1.0;

    public ZoomHandler() {
        addMouseListener(this);
    }



    public void mouseWheelMoved(MouseWheelEvent e) {
        if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {

            Point2D p1 = e.getPoint();
            Point2D p2 = null;
            try {
                p2 = tx.inverseTransform(p1, null);
            } catch (NoninvertibleTransformException ex) {

                ex.printStackTrace();
                return;
            }

            scale -= (0.1 * e.getWheelRotation());
            scale = Math.max(0.1, scale);

            tx.setToIdentity();
            tx.translate(p1.getX(), p1.getY());
            tx.scale(scale, scale);
            tx.translate(-p2.getX(), -p2.getY());

            ZoomDemo.this.revalidate();
            ZoomDemo.this.repaint();
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        System.err.println("click");
        if(rect1.contains(me.getPoint())){
            System.err.println("asdasdsd");
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {

    }

    @Override
    public void mouseReleased(MouseEvent me) {

    }

    @Override
    public void mouseEntered(MouseEvent me) {

    }

    @Override
    public void mouseExited(MouseEvent me) {
    }
}

public void run() {
    JFrame f = new JFrame("Zoom Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(this);
    f.setSize(600, 600);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new ZoomDemo());
}
}

0 个答案:

没有答案