19

我有一个现有的Java Swing应用程序。在应用程序的中间是一个JPanel我想要的单曲zoom in and out。上面有JPanel很多JComponents(主要是 otherJPanelsJLabels)。

此外mouse,位置也需要适当调整 - 因此即使在缩放mouseevents后也需要保持不变。JPanel因此,简单地改变paint每个方法component似乎并不合理。

编辑:

在此处输入图像描述

好的,我MagnifierUI class通过一些小的编辑让它工作了。然而,我创建的放大面板有错误mouseevents- 即面板被缩放,mouseevents不是。

4

2 回答 2

5

This is just a scetch:

  • in your JPanel keep track of an AffineTransform which represents the scale factor (see AffineTransform.scale(double,double),
  • override the paint method of your JPanel: before calling super.paint apply the affine transformation to your Graphics2D object (cast from the parameter of the paint method) by calling Graphics2D.setTransform(AffineTransform), call super.paint afterwards
  • override the methods processMouseEvent, processMouseMotionEvent and processMouseWheelEvent, apply the affine transformation to the coordinates of their mouse event parameter (AffineTransform.transform(java.awt.geom.Point2D,java.awt.geom.Point2D)), call respective super-method afterwards.

Hope this helps.

于 2013-02-27T11:43:57.010 回答
1

试试 SwingUtilities.convertPoint(source, point,destination);

于 2013-02-27T18:28:34.577 回答