import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyCanvas extends JComponent {
Shape shape;
public MyCanvas() {
shape = create();
}
protected Shape create() {
float cm = 30f;
return new Rectangle2D.Float(2*cm, 2*cm, cm, cm);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.draw(shape);
AffineTransform trans = new AffineTransform();
trans.scale(-1, 1);
trans.translate(-120, 0);
g2.transform(trans);
g2.setPaint(Color.red);
g2.draw(shape);
}
}
这种效果就是翻转之后再往后移120,但如果这样又为何会如此:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyCanvas extends JComponent {
Shape shape;
public MyCanvas() {
shape = create();
}
protected Shape create() {
float cm = 30f;
return new Rectangle2D.Float(2*cm, 2*cm, cm, cm);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.draw(shape);
AffineTransform trans = new AffineTransform();
trans.scale(-1, 1);
g2.transform(trans);
trans.translate(-30, 0);
g2.transform(trans);
g2.setPaint(Color.red);
g2.draw(shape);
}
}
结果与上图一样,怎么会这样呢,按理往后移30应该是看不到,原因是这里多了句
g2.transform(trans);
这句是什么作用呢,看解释:
Composes an AffineTransform
object with the
Transform
in this Graphics2D
according to the rule
last-specified-first-applied. If the current Transform
is Cx, the
result of composition with Tx is a new Transform
Cx'. Cx' becomes
the current Transform
for this Graphics2D
.
Transforming a point p by the updated Transform
Cx' is equivalent
to first transforming p by Tx and then transforming the result by the original
Transform
Cx. In other words, Cx'(p) = Cx(Tx(p)). A copy of the Tx
is made, if necessary, so further modifications to Tx do not affect rendering.
Parameters:
Tx
- the AffineTransform
object to be composed
with the current Transform
这里貌似是有问题的,Cx'怎么感觉就没翻转过呢,就感觉scale和transform两句没有发生作用一样,这是怎么回事呢?原因就是负负得正了,翻转两次,效果取消,看下文:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyCanvas extends JComponent {
Shape shape;
public MyCanvas() {
shape = create();
}
protected Shape create() {
float cm = 30f;
return new Rectangle2D.Float(2*cm, 2*cm, cm, cm);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.translate(90, 0);
g2.setPaint(Color.black);
g2.draw(shape);
g2.drawLine(0, 0, 0, 5);
g2.drawLine(-30, 0, -30, 5);
g2.drawLine(-60, 0, -60, 5);
g2.drawLine(-90, 0, -90, 5);
g2.drawLine(-120, 0, -120, 5);
g2.drawLine(30, 0, 30, 5);
g2.drawLine(60, 0, 60, 5);
g2.drawLine(90, 0, 90, 5);
g2.drawLine(120, 0, 120, 5);
g2.drawLine(150, 0, 150, 5);
g2.drawLine(180, 0, 180, 5);
AffineTransform trans = new AffineTransform();
trans.scale(-1, 1);
g2.transform(trans);
g2.transform(trans);
g2.setPaint(Color.red);
g2.draw(shape);
}
}
这里有完备的教程:
http://www.glyphic.com/transform/applet/1intro.html
记住一点,这个对象AffineTransform是面向坐标系操作的,也就是说它的操作对象是坐标系。
- 大小: 7.5 KB
- 大小: 7.7 KB
分享到:
相关推荐
在计算机图形学中,仿射变换(AffineTransform)是一种强大的工具,用于改变图像的形状和位置,而保持其直线性质和平行性。这种变换是通过一个2x3的矩阵来实现的,它可以执行平移、旋转、缩放以及倾斜等操作。在C++...
"AffineTransform - MetaTrader 5脚本.zip" 是一个与MT5相关的资源包,包含了将价格数据进行坐标系统变换的脚本,即"affinetransform.mq5"。 `AffineTransform` 是一种数学变换方法,通常用于图像处理或几何图形...
在Java中,`AffineTransform`是`java.awt.geom`包下的一个类,主要用于处理二维图形的仿射变换。仿射变换是一种特殊的线性变换,它可以保持图形的直线性和平行性,这意味着变换后的图形仍然保持原有的角度和相对位置...
目 录 欢迎使用... . . . . . . . ....MapInfo MapX 的新增功能 ....关于 MapInfo ....第 1 章: MapX 简介 ....地图绘制简介 ....使 MapX 适合您工作 ....主要功能概述 ....学习 MapX....第 2 章: MapX 基础 ....Map 对象 ....
开发者可以使用`AffineTransform`类来创建和应用这些变换。例如,`getRotateInstance()`用于旋转图形,`getScaleInstance()`用于缩放,`getTranslateInstance()`用于平移,而`getShearInstance()`则用于实现斜切变换...
import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...
这些功能的实现主要依赖于`java.awt.geom`包中的`AffineTransform`类,这是一个强大的几何变换类,可以用于对二维坐标系统进行平移、旋转、缩放等多种操作。 `AffineTransform`类是Java 2D API的核心部分,它提供了...
AffineTransform rotateTransform = AffineTransform.getRotateInstance(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2); Graphics2D g2d = (Graphics2D) graphics; g2d.drawImage(image,...
缩放通常通过调整图像的宽度和高度来实现,可以使用`AffineTransform`类创建一个变换对象,然后应用到图像上。这个过程涉及到坐标系统的缩放操作,可能包括等比例放大和缩小。翻转则可以沿着水平或垂直轴进行,同样...
Java的`Graphics2D`类提供了`transform`属性,它是一个`AffineTransform`对象,可以用来执行这些变换操作。`AffineTransform`类提供了多种方法来设置变换属性,例如: 1. `rotate(double arc, double x, double y)`...
AffineTransform transform = new AffineTransform(); // 翻转图像 // 如果想要垂直翻转,可以使用:transform.scale(-1, 1); transform.scale(1, -1); // 水平翻转 // 创建一个新的BufferedImage用于存储...
AffineTransform affineTransform = new AffineTransform(); affineTransform.translate(x, y); affineTransform.rotate(Math.toRadians(random.nextInt(10) - 5)); g.setTransform(affineTransform); g.drawString...
- 创建`AffineTransform`对象:通过`AffineTransform.getRotateInstance()`方法创建一个旋转变换,传入旋转中心点的坐标和旋转角度(以弧度为单位)。 - 应用变换:在`Graphics2D`上下文中,调用`drawImage()`方法...
在这个实例中,我们可能会用到`java.awt.Image`类来代表图片,`java.awt.Graphics2D`类用于绘制和操作图像,以及`java.awt.geom`包下的`AffineTransform`类来进行图像的翻转操作。 1. **Image类**:这是Java中表示...
AffineTransform transform = new AffineTransform(); transform.translate(250, 50); // 移动到新的位置 transform.scale(-1, 1); // 沿X轴翻转 g2d.setTransform(transform); g2d.fillRect(50, 50, 100, 100)...
AffineTransform transform = new AffineTransform(); transform.concatenate(AffineTransform.getScaleInstance(-1, 1)); Graphics2D g2d = (Graphics2D) image.getGraphics(); g2d.setTransform(transform); ...
AffineTransform at = AffineTransform.getScaleInstance(scaleFactor, scaleFactor); g2d.transform(at); int x = 0; int y = 100; g2d.drawImage(scaledImage, x, y, this); }}} Java 图像操作...
AffineTransform rotateTransform = new AffineTransform(); rotateTransform.rotate(Math.toRadians(angle), this.getWidth() / 2, this.getHeight() / 2); g2d.transform(rotateTransform); // 在这里绘制你...
- 可以使用`AffineTransform.getTranslateInstance()`、`AffineTransform.getRotateInstance()`和`AffineTransform.getScaleInstance()`创建相应变换对象,然后通过`Graphics2D`对象的`setTransform...