绕 x 轴垂直旋转形状
我有一个带有 x 和 y 轴的二维图,我试图绕轴旋转形状(一系列点)。此旋转需要包含缩放函数。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import javax.swing.*;
import java.lang.reflect.Array;
public class test extends JPanel implements ActionListener {
int[] p1x = {200, 200, 240, 240, 220, 220, 200};
int[] p1y = {200, 260, 260, 240, 240, 200, 200};
int[] p2x = {600, 600, 620, 620, 640, 640, 660, 660, 600};
int[] p2y = {400, 420, 420, 460, 460, 420, 420, 400, 400};
int[] p3x = {400, 400, 460, 460, 440, 440, 420, 420, 400};
int[] p3y = {400, 460, 460, 400, 400, 440, 440, 400, 400};
int delay = 1000;
int dx = 0;
int dy = 5;
int steps = 121;
Polygon t;
Timer tim = new Timer(delay, this);
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < Array.getLength(p2x); i++) {
//p2x[i] = (int) (p2x[i]*Math.cos(Math.toRadians(1))- p2y[i]*Math.sin(Math.toRadians(1)));
//p2y[i] = (int) (p2x[i]*Math.sin(Math.toRadians(1))+ p2y[i]*Math.cos(Math.toRadians(1)));;
Point2D original = new Point2D.Double(p2x[i], p2y[i]);
AffineTransform at = new AffineTransform();
//at.setToRotation(.02, 250, 250);
at.scale(1, -1);
Point2D rotated = at.transform(original, null);
p2x[i] = (int) rotated.getX();
p2y[i] = (int) rotated.getY();
}
repaint();
if (--steps == 0) {
tim.stop();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
g.drawLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getWidth());
g.drawLine(0, this.getHeight() / 2, this.getHeight(), this.getHeight() / 2);
Polygon t = new Polygon(p2x, p2y, 9);
g.drawPolygon(t);
Letters u = new Letters(p3x, p3y, 9);
u.draw(g);
Letters l = new Letters(p1x, p1y, 7);
l.draw(g);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing line and a moving polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test sl = new test();
frame.getContentPane().add(sl);
frame.setSize(700, 700);
frame.setVisible(true);
sl.tim.start();
}
}
I have a 2d graph with an x and y axis and im trying to rotate a shape (series of points) around an axis. This rotation will need to include a scale function.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import javax.swing.*;
import java.lang.reflect.Array;
public class test extends JPanel implements ActionListener {
int[] p1x = {200, 200, 240, 240, 220, 220, 200};
int[] p1y = {200, 260, 260, 240, 240, 200, 200};
int[] p2x = {600, 600, 620, 620, 640, 640, 660, 660, 600};
int[] p2y = {400, 420, 420, 460, 460, 420, 420, 400, 400};
int[] p3x = {400, 400, 460, 460, 440, 440, 420, 420, 400};
int[] p3y = {400, 460, 460, 400, 400, 440, 440, 400, 400};
int delay = 1000;
int dx = 0;
int dy = 5;
int steps = 121;
Polygon t;
Timer tim = new Timer(delay, this);
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < Array.getLength(p2x); i++) {
//p2x[i] = (int) (p2x[i]*Math.cos(Math.toRadians(1))- p2y[i]*Math.sin(Math.toRadians(1)));
//p2y[i] = (int) (p2x[i]*Math.sin(Math.toRadians(1))+ p2y[i]*Math.cos(Math.toRadians(1)));;
Point2D original = new Point2D.Double(p2x[i], p2y[i]);
AffineTransform at = new AffineTransform();
//at.setToRotation(.02, 250, 250);
at.scale(1, -1);
Point2D rotated = at.transform(original, null);
p2x[i] = (int) rotated.getX();
p2y[i] = (int) rotated.getY();
}
repaint();
if (--steps == 0) {
tim.stop();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.white);
g.drawLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getWidth());
g.drawLine(0, this.getHeight() / 2, this.getHeight(), this.getHeight() / 2);
Polygon t = new Polygon(p2x, p2y, 9);
g.drawPolygon(t);
Letters u = new Letters(p3x, p3y, 9);
u.draw(g);
Letters l = new Letters(p1x, p1y, 7);
l.draw(g);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing line and a moving polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test sl = new test();
frame.getContentPane().add(sl);
frame.setSize(700, 700);
frame.setVisible(true);
sl.tim.start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有明确的问题,下面将显示使用坐标数组的简单动画。一般来说,您可以变换图形上下文 (
g2d
) 或多边形Shape
本身 (p3
);该示例显示了两者。调整窗口大小以查看每个窗口的效果。请注意
at
中的转换顺序。首先,将p3
上的合适点平移到原点,然后缩放p3
,然后将p3
平移到面板中心。应用于 p3 的 + 10 模糊因子是没有对称旋转点的产物。相对于原点定义多边形可能会更容易,如示例所示。Absent a clear question, a simple animation using your coordinate arrays is shown below. In general you can transform the graphics context (
g2d
) or the polygonalShape
itself (p3
); the example shows both. Resize the window to see the effect of each.Note the last-specified-first-applied order of the transformations in
at
. First, a suitable point onp3
is translated to the origin, thenp3
is scaled, and thenp3
is translated to the center of the panel. The+ 10
fudge factor applied top3
is an artifact of having no symmetric rotation point. It may be easier to define your polygons relative to the origin, as shown in this example.