在java中翻转2d组件

发布于 2024-12-16 21:27:16 字数 1555 浏览 2 评论 0原文

我正在尝试翻转任何对象的二维对象。在Java中可以这样做吗?如果他们在不同的象限,我可以改变他们的象限吗?这与我们在画图中翻转图像的操作类似。我试图在 Java 中执行相同的实用程序。我听说过仿射变换,其中它使用了一个名为 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 );
     }}

i am trying to flip a 2d object any object. Is it possible to do so in Java? if they r in separate quadrant can I change their quadrant? It is similar to what we do to flip images in Paint. The same utility I am trying to perform in Java. I have heard about Affine transform wherein it makes use of a bit called TYPE_FLIP, but I am not sure how to use it. Any small example would be of great help. Note: I do not want to flip images, but actual 2D objects.
In this way with Affine Transform.

   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 );
     }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2024-12-23 21:27:16

此代码并未提供完整的答案,但旨在让您思考。您应该考虑的是“菱形去哪里了?”。尝试调整变换缩放中使用的数字,看看是否可以解决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) 好的.. 基本问题是,对于您正在执行的变换类型,通常需要结合比例 和翻译。

原因是这样的。

  1. 当形状水平翻转时,它会被绘制到组件可视(可见)区域的左侧
  2. 当形状垂直翻转时,它会绘制在可视区域的上方

使用比例完成“翻转”翻译后,concatenate()翻译() 变换将形状移回到可视区域。

如果您知道如何操作,这些组合变换就会很容易。我不知道怎么办。

This code does not provide a complete answer, but is designed to get you thinking. What you should be thinking about, is "Where does the diamond shape go to?". Try adjusting the numbers used in the scaling of the transform, and see if you can figure it outA.

Diamond Here Diamond Gone

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.. The basic problem is that for the type of transform that you are doing, it is usually necessary to combine both a scale & a translate.

The reason is that.

  1. When a shape is flipped horizontally, it gets drawn to the left of the viewable (visible) area of the component.
  2. When a shape is flipped vertically, it gets drawn above the viewable area.

After the 'flip' translate is done using scale, concatenate() that with a translate() transform to move the shape back into the viewable area.

These combined transforms are easy if you know how. I don't know how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文