我试图翻转任何对象的2d对象。是否有可能在Java中这样做?如果他们在单独的象限中,我可以改变他们的象限吗?它与我们在Paint中翻转图像的方式类似。我试图用Java执行相同的实用程序。我听说过Affine变换,它使用了一个名为TYPE_FLIP的位,但我不知道如何使用它。任何小例子都会有很大的帮助。注意:我不想翻转图像,而是实际的2D对象。 以这种方式使用仿射变换。
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
public class TestRotate extends JFrame
{
public void paint( Graphics g )
{
super.paintComponents(g);
AffineTransform saveTransform;
int[] HouseX = {100,150,200,150,100,50};
int[] HouseY = {100,100,(int)(100+(40*(Math.sqrt(3))/2)),(int)(100+(40*(Math.sqrt(3)))),(int)(100+(40*(Math.sqrt(3)))),(int)(100+(40*(Math.sqrt(3))/2))};
Graphics2D g2 = ( Graphics2D ) g;
g.setColor( Color.BLACK );
g.drawPolygon(HouseX, HouseY, 6);
saveTransform = g2.getTransform();
AffineTransform transform = new AffineTransform();
transform.scale( 1.0, -1.0 );
g2.setTransform( transform );
g2.setColor( Color.BLUE );
g.drawPolygon(HouseX, HouseY, 6);
transform.rotate( Math.toRadians( 45 ) );
g2.setTransform( transform );
g2.setColor( Color.GREEN );
g.drawPolygon(HouseX, HouseY, 6);
}
public static void main(String args[])
{
TestRotate frame = new TestRotate();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 600, 500 );
frame.setVisible( true );
}}
答案 0 :(得分:3)
此代码不提供完整的答案,但旨在让您思考。您应该考虑的是“钻石形状在哪里?”。尝试调整变换缩放中使用的数字,看看是否可以解决 A 。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
public class TestRotate
{
public static void main(String args[])
{
JFrame frame = new JFrame("Test Rotate");
JPanel gui = new JPanel(new BorderLayout());
final HousePanel housePanel = new HousePanel();
gui.add(housePanel, BorderLayout.CENTER);
final JCheckBox transform = new JCheckBox("Transform");
transform.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
housePanel.setTransform(transform.isSelected());
housePanel.repaint();
}
});
gui.add(transform, BorderLayout.NORTH);
frame.add( gui );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
gui.setPreferredSize( new Dimension(300, 200) );
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible( true );
}
}
class HousePanel extends JPanel {
public int[] HouseX = {
100,105,110,105,100,95
};
public int[] HouseY = {
100,
100,
(int)(100+(5*(Math.sqrt(3))/2)),
(int)(100+(5*(Math.sqrt(3)))),
(int)(100+(5*(Math.sqrt(3)))),
(int)(100+(5*(Math.sqrt(3))/2))
};
boolean transform = false;
public void setTransform(boolean transform) {
this.transform = transform;
}
public void paintComponent( Graphics g )
{
final Graphics2D g2 = (Graphics2D) g;
g2.clearRect( 0, 0, this.getWidth(), this.getHeight() );
g2.setColor( Color.BLACK );
if (transform) {
AffineTransform transform = new AffineTransform();
transform.scale( -1.0, 1.0 );
g2.setTransform( transform );
}
g2.drawPolygon(HouseX, HouseY, 6);
}
}
A)OK ..基本问题是,对于您正在进行的变换类型,通常需要将缩放和&翻译。
原因是。
使用缩放后的'翻转'翻译后,concatenate()使用translate()变换将形状移回可查看区域。
如果您知道如何,这些组合变换很容易。我不知道怎么做。