Java中利用AffineTransform中的scale函数对图像进行对称变换

package Frame;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Polygon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

public class GameFrame 
{

    public GameFrame()
    {
        Frame frame=new Frame("进击的信息科大");
        MyPanel mp=new MyPanel();
//      frame.setLocation(300,200);
        frame.setSize(800,600);
        frame.add(mp);
        frame.setVisible(true);
        //设置点击红叉关闭frame
        frame.addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }

    public static void main(String args[])
    {
        new GameFrame();
    }

    class MyPanel extends Panel
    {
        public void paint(Graphics g)
        {
            Rectangle2D rectangle2d=new Rectangle2D.Double(10,10,40,30);
            int x[]={10,20,30};
            int y[]={10,20,20};
            //设置三角形
            Polygon polygon=new Polygon(x, y, 3);
            Graphics2D graphics2d=(Graphics2D) g;
            AffineTransform aTransform=new AffineTransform();
//          aTransform.translate(tx, ty);
            aTransform.setToTranslation(100, 100);
//          aTransform.rotate(Math.PI/8.0);
//          aTransform.setToShear(2, 2);
            //沿X轴方向轴对称(y轴方向为(1,-1))
            aTransform.scale(-1, 1);
            // 连接变换
            graphics2d.setTransform(aTransform);
            graphics2d.setColor(Color.blue);
            //绘制
            graphics2d.draw(polygon);

        }
    }
}

你可能感兴趣的:(Java)