Java Graphics2D平移和缩放/转换和缩放
我正在画一个JPanel,我正试图得到它,这样你就可以使用鼠标滚轮通过修改可变比例来“放大”和“缩小”。现在发生的是,可以放大或缩小,但放大时所有图形都会向右和向下移动,然后缩小时会向上和向左移动 我想让它调整,就像你在放大JPanel中心的点一样,但我不知道如何计算要平移的正确偏移量 有没有人知道如何做到这一点,或者有没有更好的方法来实现整个平移和缩放功能 我基本上绘制了一组坐标,这些坐标来自一个系统,其中0,0位于左下角,落在边界之间:Java Graphics2D平移和缩放/转换和缩放,java,graphics,scale,translate,Java,Graphics,Scale,Translate,我正在画一个JPanel,我正试图得到它,这样你就可以使用鼠标滚轮通过修改可变比例来“放大”和“缩小”。现在发生的是,可以放大或缩小,但放大时所有图形都会向右和向下移动,然后缩小时会向上和向左移动 我想让它调整,就像你在放大JPanel中心的点一样,但我不知道如何计算要平移的正确偏移量 有没有人知道如何做到这一点,或者有没有更好的方法来实现整个平移和缩放功能 我基本上绘制了一组坐标,这些坐标来自一个系统,其中0,0位于左下角,落在边界之间: xMin = 661208 xMax = 662618
xMin = 661208
xMax = 662618
yMin = 4291657
yMax = 4293285
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
double scale = details.getScale();
double xOffset = details.getxOffset();
double yOffset = details.getyOffset();
g2.scale(scale, -scale);
// Dividing offset by scale makes panning 1:1 with the cursor still. yMax to account for the
// fact we needed to flip around the y axis to make it right-side up.
g2.translate((-xMin + xOffset / scale), (-yMax + yOffset / scale));
// Code to draw stuff goes here. It uses coordinates between xMin-xMax and yMin-yMax to draw.
.
.
.
}
以下是车轮的示例:
public class GraphicsOnly extends JPanel implements MouseWheelListener {
Shape[] shapes;
Dimension size;
double scale = 1.0;
private static int source = 100;
public GraphicsOnly() {
addMouseWheelListener(this);
size = new Dimension(10,10);
setBackground(new Color(240,200,200));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if(shapes == null) initShapes();
// Keep shapes centered on panel.
double x = (getWidth() - scale*size.width)/2;
double y = (getHeight() - scale*size.height)/2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.scale(scale, scale);
g2.setPaint(Color.blue);
g2.draw(at.createTransformedShape(shapes[0]));
g2.setPaint(Color.green.darker());
g2.draw(at.createTransformedShape(shapes[1]));
g2.setPaint(new Color(240,240,200));
g2.fill(at.createTransformedShape(shapes[2]));
g2.setPaint(Color.red);
g2.draw(at.createTransformedShape(shapes[2]));
}
public Dimension getPreferredSize() {
int w = (int)(scale*size.width);
int h = (int)(scale*size.height);
return new Dimension(w, h);
}
private void initShapes() {
shapes = new Shape[3];
int w = getWidth();
int h = getHeight();
shapes[0] = new Rectangle2D.Double(w/16, h/16, w*7/8, h*7/8);
shapes[1] = new Line2D.Double(w/16, h*15/16, w*15/16, h/16);
shapes[2] = new Ellipse2D.Double(w/4, h/4, w/2, h/2);
size.width = w;
size.height = h;
}
public static void main(String[] args) {
GraphicsOnly app = new GraphicsOnly();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(app));
f.setSize(400, 400);
f.setLocation(200,200);
f.setVisible(true);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
// TODO Auto-generated method stub
if(e.getWheelRotation() >0){
source = source-e.getScrollAmount();
}else{
source = source+e.getScrollAmount();
}
scale = source/100.0;
System.out.println(scale);
repaint();
revalidate();
}
}
这个问题可能对你有帮助