使用 .transform 更改 Path2D.Double 时遇到问题
Trouble with getting Path2D.Double to change with .transform
我的程序应该允许用户将矩阵的前六个数字输入文本字段,然后点击应用按钮,使用 .transform 方法更改 Path2D,参数是六个输入的数字。我的问题是,每当我输入内容并点击应用时,转换与应有的完全不同,并且原始箭头也留在后面。
真的很奇怪,我不知道问题出在哪里。一切都应该在仿射变换的正确位置,但变换完全错误。
它应该是这样的:
这就是我的样子:
我会把完整的代码放在下面,这样你就可以 运行 自己看看你是否能弄明白。谢谢!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import javax.swing.event.*;
public class Project3 extends JPanel implements ActionListener {
public static Project3 p = new Project3();
Path2D.Double arrow = new Path2D.Double();
public static JTextField
num1 = new JTextField("1"), num2 = new JTextField("0"),
num3 = new JTextField("0"), num4 = new JTextField("0"),
num5 = new JTextField("1"), num6 = new JTextField("0");
public Project3() {
setBackground(Color.WHITE);
}
public Path2D.Double drawArrow() {
arrow.setWindingRule(GeneralPath.WIND_EVEN_ODD);
arrow.moveTo(0, 0);
arrow.lineTo(0, -100);
arrow.moveTo(0, -200);
arrow.lineTo(100, -100);
arrow.lineTo(50, -100);
arrow.lineTo(50, 100);
arrow.quadTo(0, 0, -50, 100);
arrow.lineTo(-50, -100);
arrow.lineTo(-100, -100);
arrow.lineTo(0, -200);
arrow.closePath();
return arrow;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(250, 250);
GradientPaint gradient = new GradientPaint(0, 0, Color.LIGHT_GRAY, 15, 15, Color.BLACK, true);
g2.setPaint(gradient);
g2.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
g2.draw(drawArrow());
}
public static void main(String[] args) {
JFrame frame = new JFrame("Project 3");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
cp.add(panel, BorderLayout.CENTER);
panel.add(p, BorderLayout.CENTER);
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
cp.add(panel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(2, 3));
panel.add(textPanel);
textPanel.add(num1);
textPanel.add(num2);
textPanel.add(num3);
textPanel.add(num4);
textPanel.add(num5);
textPanel.add(num6);
JPanel btPanel = new JPanel();
btPanel.setLayout(new GridLayout(0, 1));
panel.add(btPanel);
JButton apply = new JButton("Apply");
apply.addActionListener(p);
btPanel.add(apply);
JButton reset = new JButton("Reset");
reset.addActionListener(p);
btPanel.add(reset);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "Apply":
double args1 = Double.parseDouble(num1.getText());
double args2 = Double.parseDouble(num2.getText());
double args3 = Double.parseDouble(num3.getText());
double args4 = Double.parseDouble(num4.getText());
double args5 = Double.parseDouble(num5.getText());
double args6 = Double.parseDouble(num6.getText());
arrow.transform(new AffineTransform(args1, args2, args3, args4, args5, args6));
repaint();
break;
case "Reset":
arrow.transform(new AffineTransform(1, 0, 0, 0, 1, 0));
repaint();
break;
}
}
}
每次您调用 paintComponent
,它都会创建一个新的 arrow
实例,丢弃您之前可能应用的任何转换。
相反,创建一次形状并使用您变换后的相同实例进行绘画。
您也可以考虑使用 createTransformedShape
,因为这将 return 原始路径的转换实例,而不是连续复合转换
您的部分问题似乎与 AffineTransform 参数的顺序有关。您可能假设您的 JTextFields 与 2x3 矩阵的形式相同,但事实并非如此。给 JTextFields 添加边框看看我的意思:
num00.setBorder(BorderFactory.createTitledBorder("m00"));
num10.setBorder(BorderFactory.createTitledBorder("m10"));
num01.setBorder(BorderFactory.createTitledBorder("m01"));
num11.setBorder(BorderFactory.createTitledBorder("m11"));
num02.setBorder(BorderFactory.createTitledBorder("m02"));
num12.setBorder(BorderFactory.createTitledBorder("m12"));
和:
double m00 = Double.parseDouble(num00.getText());
double m10 = Double.parseDouble(num10.getText());
double m01 = Double.parseDouble(num01.getText());
double m11 = Double.parseDouble(num11.getText());
double m02 = Double.parseDouble(num02.getText());
double m12 = Double.parseDouble(num12.getText());
AffineTransform transform = new AffineTransform(m00, m10, m01, m11,
m02, m12);
你会发现事情与你假设的顺序不一样。相反,添加您的 JTextfields 以便它们匹配矩阵顺序:
textPanel.add(num00);
textPanel.add(num10);
textPanel.add(num02);
textPanel.add(num01);
textPanel.add(num11);
textPanel.add(num12);
这将改善您的发现。
类似于:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Path2D;
import java.util.Deque;
import java.util.LinkedList;
import javax.swing.event.*;
public class Project3 extends JPanel implements ActionListener {
public static Project3 p = new Project3();
Path2D arrow; // !!
public static
JTextField
num00 = new JTextField("0"),
num10 = new JTextField("1"),
num01 = new JTextField("1"),
num11 = new JTextField("0"),
num02 = new JTextField("0"),
num12 = new JTextField("0");
private static Deque<AffineTransform> atStack = new LinkedList<>();
public Project3() {
setBackground(Color.WHITE);
arrow = drawArrow(); // !! create the arrow only once
}
public Path2D drawArrow() { // !!
arrow = new Path2D.Double(); // !!
arrow.setWindingRule(GeneralPath.WIND_EVEN_ODD);
arrow.moveTo(0, 0);
arrow.lineTo(0, -100);
arrow.moveTo(0, -200);
arrow.lineTo(100, -100);
arrow.lineTo(50, -100);
arrow.lineTo(50, 100);
arrow.quadTo(0, 0, -50, 100);
arrow.lineTo(-50, -100);
arrow.lineTo(-100, -100);
arrow.lineTo(0, -200);
arrow.closePath();
arrow.transform(AffineTransform.getTranslateInstance(250, 250)); // !! shift it here
return arrow;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // !!
// !! g2.translate(250, 250); // translate the arrow, not Graphics
GradientPaint gradient = new GradientPaint(0, 0, Color.LIGHT_GRAY, 15,
15, Color.BLACK, true);
g2.setPaint(gradient);
g2.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_MITER));
// g2.draw(drawArrow()); // !! don't re-create the arrow
g2.draw(arrow);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Project 3");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
cp.add(panel, BorderLayout.CENTER);
panel.add(p, BorderLayout.CENTER);
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
cp.add(panel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(2, 3));
panel.add(textPanel);
num00.setBorder(BorderFactory.createTitledBorder("m00"));
num10.setBorder(BorderFactory.createTitledBorder("m10"));
num01.setBorder(BorderFactory.createTitledBorder("m01"));
num11.setBorder(BorderFactory.createTitledBorder("m11"));
num02.setBorder(BorderFactory.createTitledBorder("m02"));
num12.setBorder(BorderFactory.createTitledBorder("m12"));
textPanel.add(num00);
textPanel.add(num10);
textPanel.add(num02);
textPanel.add(num01);
textPanel.add(num11);
textPanel.add(num12);
JPanel btPanel = new JPanel();
btPanel.setLayout(new GridLayout(0, 1));
panel.add(btPanel);
JButton apply = new JButton("Apply");
apply.addActionListener(p);
btPanel.add(apply);
JButton reset = new JButton("Reset");
reset.addActionListener(p);
btPanel.add(reset);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "Apply":
double m00 = Double.parseDouble(num00.getText());
double m10 = Double.parseDouble(num10.getText());
double m01 = Double.parseDouble(num01.getText());
double m11 = Double.parseDouble(num11.getText());
double m02 = Double.parseDouble(num02.getText());
double m12 = Double.parseDouble(num12.getText());
AffineTransform transform = new AffineTransform(m00, m10, m01, m11,
m02, m12);
arrow.transform(transform);
atStack.addFirst(transform); // save the transform
repaint();
break;
case "Reset":
// !! arrow.transform(new AffineTransform(1, 0, 0, 0, 1, 0));
while (atStack.size() > 0) {
AffineTransform at = atStack.removeFirst();
// inverse fails if determinant is 0
if (at.getDeterminant() == 0) {
return;
}
try {
arrow.transform(at.createInverse());
} catch (NoninvertibleTransformException e1) {
e1.printStackTrace();
}
}
repaint();
break;
}
}
}
我还使用队列作为堆栈来保存应用的转换,以允许它们在重置时按顺序取消完成。
我的程序应该允许用户将矩阵的前六个数字输入文本字段,然后点击应用按钮,使用 .transform 方法更改 Path2D,参数是六个输入的数字。我的问题是,每当我输入内容并点击应用时,转换与应有的完全不同,并且原始箭头也留在后面。
真的很奇怪,我不知道问题出在哪里。一切都应该在仿射变换的正确位置,但变换完全错误。
它应该是这样的:
这就是我的样子:
我会把完整的代码放在下面,这样你就可以 运行 自己看看你是否能弄明白。谢谢!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import javax.swing.event.*;
public class Project3 extends JPanel implements ActionListener {
public static Project3 p = new Project3();
Path2D.Double arrow = new Path2D.Double();
public static JTextField
num1 = new JTextField("1"), num2 = new JTextField("0"),
num3 = new JTextField("0"), num4 = new JTextField("0"),
num5 = new JTextField("1"), num6 = new JTextField("0");
public Project3() {
setBackground(Color.WHITE);
}
public Path2D.Double drawArrow() {
arrow.setWindingRule(GeneralPath.WIND_EVEN_ODD);
arrow.moveTo(0, 0);
arrow.lineTo(0, -100);
arrow.moveTo(0, -200);
arrow.lineTo(100, -100);
arrow.lineTo(50, -100);
arrow.lineTo(50, 100);
arrow.quadTo(0, 0, -50, 100);
arrow.lineTo(-50, -100);
arrow.lineTo(-100, -100);
arrow.lineTo(0, -200);
arrow.closePath();
return arrow;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.translate(250, 250);
GradientPaint gradient = new GradientPaint(0, 0, Color.LIGHT_GRAY, 15, 15, Color.BLACK, true);
g2.setPaint(gradient);
g2.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
g2.draw(drawArrow());
}
public static void main(String[] args) {
JFrame frame = new JFrame("Project 3");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
cp.add(panel, BorderLayout.CENTER);
panel.add(p, BorderLayout.CENTER);
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
cp.add(panel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(2, 3));
panel.add(textPanel);
textPanel.add(num1);
textPanel.add(num2);
textPanel.add(num3);
textPanel.add(num4);
textPanel.add(num5);
textPanel.add(num6);
JPanel btPanel = new JPanel();
btPanel.setLayout(new GridLayout(0, 1));
panel.add(btPanel);
JButton apply = new JButton("Apply");
apply.addActionListener(p);
btPanel.add(apply);
JButton reset = new JButton("Reset");
reset.addActionListener(p);
btPanel.add(reset);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "Apply":
double args1 = Double.parseDouble(num1.getText());
double args2 = Double.parseDouble(num2.getText());
double args3 = Double.parseDouble(num3.getText());
double args4 = Double.parseDouble(num4.getText());
double args5 = Double.parseDouble(num5.getText());
double args6 = Double.parseDouble(num6.getText());
arrow.transform(new AffineTransform(args1, args2, args3, args4, args5, args6));
repaint();
break;
case "Reset":
arrow.transform(new AffineTransform(1, 0, 0, 0, 1, 0));
repaint();
break;
}
}
}
每次您调用 paintComponent
,它都会创建一个新的 arrow
实例,丢弃您之前可能应用的任何转换。
相反,创建一次形状并使用您变换后的相同实例进行绘画。
您也可以考虑使用 createTransformedShape
,因为这将 return 原始路径的转换实例,而不是连续复合转换
您的部分问题似乎与 AffineTransform 参数的顺序有关。您可能假设您的 JTextFields 与 2x3 矩阵的形式相同,但事实并非如此。给 JTextFields 添加边框看看我的意思:
num00.setBorder(BorderFactory.createTitledBorder("m00"));
num10.setBorder(BorderFactory.createTitledBorder("m10"));
num01.setBorder(BorderFactory.createTitledBorder("m01"));
num11.setBorder(BorderFactory.createTitledBorder("m11"));
num02.setBorder(BorderFactory.createTitledBorder("m02"));
num12.setBorder(BorderFactory.createTitledBorder("m12"));
和:
double m00 = Double.parseDouble(num00.getText());
double m10 = Double.parseDouble(num10.getText());
double m01 = Double.parseDouble(num01.getText());
double m11 = Double.parseDouble(num11.getText());
double m02 = Double.parseDouble(num02.getText());
double m12 = Double.parseDouble(num12.getText());
AffineTransform transform = new AffineTransform(m00, m10, m01, m11,
m02, m12);
你会发现事情与你假设的顺序不一样。相反,添加您的 JTextfields 以便它们匹配矩阵顺序:
textPanel.add(num00);
textPanel.add(num10);
textPanel.add(num02);
textPanel.add(num01);
textPanel.add(num11);
textPanel.add(num12);
这将改善您的发现。
类似于:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Path2D;
import java.util.Deque;
import java.util.LinkedList;
import javax.swing.event.*;
public class Project3 extends JPanel implements ActionListener {
public static Project3 p = new Project3();
Path2D arrow; // !!
public static
JTextField
num00 = new JTextField("0"),
num10 = new JTextField("1"),
num01 = new JTextField("1"),
num11 = new JTextField("0"),
num02 = new JTextField("0"),
num12 = new JTextField("0");
private static Deque<AffineTransform> atStack = new LinkedList<>();
public Project3() {
setBackground(Color.WHITE);
arrow = drawArrow(); // !! create the arrow only once
}
public Path2D drawArrow() { // !!
arrow = new Path2D.Double(); // !!
arrow.setWindingRule(GeneralPath.WIND_EVEN_ODD);
arrow.moveTo(0, 0);
arrow.lineTo(0, -100);
arrow.moveTo(0, -200);
arrow.lineTo(100, -100);
arrow.lineTo(50, -100);
arrow.lineTo(50, 100);
arrow.quadTo(0, 0, -50, 100);
arrow.lineTo(-50, -100);
arrow.lineTo(-100, -100);
arrow.lineTo(0, -200);
arrow.closePath();
arrow.transform(AffineTransform.getTranslateInstance(250, 250)); // !! shift it here
return arrow;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // !!
// !! g2.translate(250, 250); // translate the arrow, not Graphics
GradientPaint gradient = new GradientPaint(0, 0, Color.LIGHT_GRAY, 15,
15, Color.BLACK, true);
g2.setPaint(gradient);
g2.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_MITER));
// g2.draw(drawArrow()); // !! don't re-create the arrow
g2.draw(arrow);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Project 3");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
cp.add(panel, BorderLayout.CENTER);
panel.add(p, BorderLayout.CENTER);
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
cp.add(panel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(2, 3));
panel.add(textPanel);
num00.setBorder(BorderFactory.createTitledBorder("m00"));
num10.setBorder(BorderFactory.createTitledBorder("m10"));
num01.setBorder(BorderFactory.createTitledBorder("m01"));
num11.setBorder(BorderFactory.createTitledBorder("m11"));
num02.setBorder(BorderFactory.createTitledBorder("m02"));
num12.setBorder(BorderFactory.createTitledBorder("m12"));
textPanel.add(num00);
textPanel.add(num10);
textPanel.add(num02);
textPanel.add(num01);
textPanel.add(num11);
textPanel.add(num12);
JPanel btPanel = new JPanel();
btPanel.setLayout(new GridLayout(0, 1));
panel.add(btPanel);
JButton apply = new JButton("Apply");
apply.addActionListener(p);
btPanel.add(apply);
JButton reset = new JButton("Reset");
reset.addActionListener(p);
btPanel.add(reset);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "Apply":
double m00 = Double.parseDouble(num00.getText());
double m10 = Double.parseDouble(num10.getText());
double m01 = Double.parseDouble(num01.getText());
double m11 = Double.parseDouble(num11.getText());
double m02 = Double.parseDouble(num02.getText());
double m12 = Double.parseDouble(num12.getText());
AffineTransform transform = new AffineTransform(m00, m10, m01, m11,
m02, m12);
arrow.transform(transform);
atStack.addFirst(transform); // save the transform
repaint();
break;
case "Reset":
// !! arrow.transform(new AffineTransform(1, 0, 0, 0, 1, 0));
while (atStack.size() > 0) {
AffineTransform at = atStack.removeFirst();
// inverse fails if determinant is 0
if (at.getDeterminant() == 0) {
return;
}
try {
arrow.transform(at.createInverse());
} catch (NoninvertibleTransformException e1) {
e1.printStackTrace();
}
}
repaint();
break;
}
}
}
我还使用队列作为堆栈来保存应用的转换,以允许它们在重置时按顺序取消完成。