如何翻转使用 Graphics 或 Graphics2D 创建的点相对于中心点的位置
How to flip the position of points created using Graphics or Graphics2D relative to the center point
您好,我正在使用 Java Swing
处理图形
我想取jpanel上的点然后以jpanel中心点为基准同时上下翻转
我寻找了一种方法来旋转 jpanel 或将其上下翻转,但我只能看到与 img 文件关联的数据。
如果没有别的办法,是不是要用变换矩阵把所有的点一个一个移动?
您可以使用 Graphics2D 和 AffineTransform。
这是代码示例。
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Panel Size = 400 X 400
g.drawLine(200, 0, 200, 400); // Draw Y Axis
g.drawLine(0, 200, 400, 200); // Draw X Axis
// Create Transform
AffineTransform at = new AffineTransform();
at.translate(200, 200); // Move Center Form (0, 0) To JPanel Center (200, 200)
// Change Transform - Test One Line At Once For Study :)
//at.translate(-200, 0); // Move Center
//at.rotate(Math.toRadians(90)); // Rotate
//at.scale(1, -1); // Scale, But Use For Flip
// Set Transform To Graphics2D
Graphics2D g2d = (Graphics2D) g;
g2d.setTransform(at);
// Draw Rectangle By Graphics2D
g2d.fillRect(100, 100, 100, 100);
}
您好,我正在使用 Java Swing
处理图形我想取jpanel上的点然后以jpanel中心点为基准同时上下翻转
我寻找了一种方法来旋转 jpanel 或将其上下翻转,但我只能看到与 img 文件关联的数据。
如果没有别的办法,是不是要用变换矩阵把所有的点一个一个移动?
您可以使用 Graphics2D 和 AffineTransform。
这是代码示例。
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Panel Size = 400 X 400
g.drawLine(200, 0, 200, 400); // Draw Y Axis
g.drawLine(0, 200, 400, 200); // Draw X Axis
// Create Transform
AffineTransform at = new AffineTransform();
at.translate(200, 200); // Move Center Form (0, 0) To JPanel Center (200, 200)
// Change Transform - Test One Line At Once For Study :)
//at.translate(-200, 0); // Move Center
//at.rotate(Math.toRadians(90)); // Rotate
//at.scale(1, -1); // Scale, But Use For Flip
// Set Transform To Graphics2D
Graphics2D g2d = (Graphics2D) g;
g2d.setTransform(at);
// Draw Rectangle By Graphics2D
g2d.fillRect(100, 100, 100, 100);
}