Java2d:平移轴
我正在使用Java2d开发一个应用程序。我注意到一件奇怪的事情,原点在左上角,正x向右移动,正y向下增加 有没有办法将原点移到左下角Java2d:平移轴,java,graphics,java-2d,Java,Graphics,Java 2d,我正在使用Java2d开发一个应用程序。我注意到一件奇怪的事情,原点在左上角,正x向右移动,正y向下增加 有没有办法将原点移到左下角 谢谢。你试过了吗?你试过了吗?你需要进行缩放和平移 在paintComponent方法中,可以执行以下操作: public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.translate(0, -height); g2d.scale(1.0,
谢谢。你试过了吗?你试过了吗?你需要进行缩放和平移 在paintComponent方法中,可以执行以下操作:
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.translate(0, -height);
g2d.scale(1.0, -1.0);
//draw your component with the new coordinates
//you may want to reset the transforms at the end to prevent
//other controls from making incorrect assumptions
g2d.scale(1.0, -1.0);
g2d.translate(0, height);
}
我的秋千有点生锈,但这应该可以完成任务。你需要做一个缩放和平移 在paintComponent方法中,可以执行以下操作:
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.translate(0, -height);
g2d.scale(1.0, -1.0);
//draw your component with the new coordinates
//you may want to reset the transforms at the end to prevent
//other controls from making incorrect assumptions
g2d.scale(1.0, -1.0);
g2d.translate(0, height);
}
我的秋千有点生锈了,但这应该可以完成任务。你会想习惯的。正如luke提到的,从技术上讲,您可以对图形实例应用转换,但这最终会对性能产生负面影响
仅仅做一次平移就可以将0,0的位置移动到左下角,但是沿正轴的移动仍然是在x方向上向右移动,在y方向上向下移动,因此您唯一能完成的事情就是将所有内容绘制到屏幕外。你需要做一个旋转来完成你的要求,这将增加弧度计算的开销到图形实例的变换矩阵。这不是一个好的折衷办法。你会想习惯的。正如luke提到的,从技术上讲,您可以对图形实例应用转换,但这最终会对性能产生负面影响
仅仅做一次平移就可以将0,0的位置移动到左下角,但是沿正轴的移动仍然是在x方向上向右移动,在y方向上向下移动,因此您唯一能完成的事情就是将所有内容绘制到屏幕外。你需要做一个旋转来完成你的要求,这将增加弧度计算的开销到图形实例的变换矩阵。这不是一个好的折衷办法。我们可以使用以下方法轻松解决问题
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Flip the sign of the coordinate system
g2d.translate(0.0, getHeight());
g2d.scale(1.0, -1.0);
......
}
我们可以用下面的方法轻松解决这个问题
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Flip the sign of the coordinate system
g2d.translate(0.0, getHeight());
g2d.scale(1.0, -1.0);
......
}
为了以后的参考,我不得不在代码中交换调用的顺序以进行缩放和转换。也许这会对将来的人有所帮助:
@Test
public void bottomLeftOriginTest() throws IOException {
int width = 256;
int height = 512;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D ig = bi.createGraphics();
// save the "old" transform
AffineTransform old = ig.getTransform();
// origin is top left:
// update graphics object with the inverted y-transform
if (true) { /* order ok */
ig.scale(1.0, -1.0);
ig.translate(0, -bi.getHeight());
} else {
ig.translate(0, -bi.getHeight());
ig.scale(1.0, -1.0);
}
int xPoints[] = new int[] { 0, width, width };
int yPoints[] = new int[] { 0, height, 0 };
int nPoints = xPoints.length;
ig.setColor(Color.BLUE);
ig.fillRect(0, 0, bi.getWidth(), bi.getHeight());
ig.setColor(Color.RED);
ig.fillPolygon(xPoints, yPoints, nPoints);
// restore the old transform
ig.setTransform(old);
// Export the result to a file
ImageIO.write(bi, "PNG", new File("origin.png"));
}
为了以后的参考,我不得不在代码中交换调用的顺序以进行缩放和转换。也许这会对将来的人有所帮助:
@Test
public void bottomLeftOriginTest() throws IOException {
int width = 256;
int height = 512;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics2D ig = bi.createGraphics();
// save the "old" transform
AffineTransform old = ig.getTransform();
// origin is top left:
// update graphics object with the inverted y-transform
if (true) { /* order ok */
ig.scale(1.0, -1.0);
ig.translate(0, -bi.getHeight());
} else {
ig.translate(0, -bi.getHeight());
ig.scale(1.0, -1.0);
}
int xPoints[] = new int[] { 0, width, width };
int yPoints[] = new int[] { 0, height, 0 };
int nPoints = xPoints.length;
ig.setColor(Color.BLUE);
ig.fillRect(0, 0, bi.getWidth(), bi.getHeight());
ig.setColor(Color.RED);
ig.fillPolygon(xPoints, yPoints, nPoints);
// restore the old transform
ig.setTransform(old);
// Export the result to a file
ImageIO.write(bi, "PNG", new File("origin.png"));
}
不幸的是,这是大多数gui系统的标准轴布局,它起源于一切都基于文本的时代,所以左上角作为文本的原点是有意义的。大多数GUI工具包都允许您翻转原点和轴,但我的建议是要习惯它:不幸的是,这是大多数GUI系统的标准轴布局,它来自于一切都是基于文本的时候,所以左上角作为文本的原点是有意义的。大多数GUI工具包都允许您翻转原点和轴,但我的建议是习惯它:我不喜欢您设置缩放/平移的方式。在这种特殊情况下应该可以,但一般来说,在堆叠转换时会累积浮点错误。最好先调用getTransform,然后再调用setTransform来还原原始代码。这很奇怪,我必须交换translate和scale调用的顺序,才能让代码真正工作……我不喜欢你设置scale/translate的方式。在这种特殊情况下应该可以,但一般来说,在堆叠转换时会累积浮点错误。最好先调用getTransform,然后再调用setTransform来还原原始代码。奇怪的是,我不得不交换translate和scale调用的顺序,以使代码实际工作。。。