package shiyan4;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//
// enum TransformType {//选择操作的方式
// translation,scaling,rotation ; //平移,缩放,旋转
// }
public class Beijixing extends JFrame implements ActionListener {
TextField tf1,tf2,tf3,tf4,tf5,tf6,tf7;
JPanel pane1,pane2,paintPanel;
private Matrix m;
private double[][] xym;
private int x0,y0; //将图形移到屏幕中间
public Beijixing(){
this.setLayout(null);
pane1=new JPanel(new GridLayout(5, 6));
//平移
JLabel lab1 = new JLabel(" x轴平移量 ");
tf1 = new TextField(10);
lab1.setLabelFor(tf1);
JLabel lab2 = new JLabel(" y轴平移量 ");
tf2 = new TextField(10);
lab2.setLabelFor(tf2);
JButton translation = new JButton("平移");
translation.addActionListener(this);
pane1.add(lab1);
pane1.add(tf1);
pane1.add(lab2);
pane1.add(tf2);
pane1.add(translation);
pane1.add(new JLabel(" 单位pixel,整型"));
//缩放
JLabel lab3 = new JLabel(" x轴缩放倍数 ");
tf3 = new TextField(10);
lab3.setLabelFor(tf3);
JLabel lab4 = new JLabel(" y轴缩放倍数 ");
tf4 = new TextField(10);
lab4.setLabelFor(tf4);
JButton scaling = new JButton("缩放");
scaling.addActionListener(this);
pane1.add(lab3);
pane1.add(tf3);
pane1.add(lab4);
pane1.add(tf4);
pane1.add(scaling);
pane1.add(new JLabel("小数,正放大,负缩小"));
//旋转
JLabel lab5 = new JLabel(" 旋转角度 ");
tf5 = new TextField(10);
lab5.setLabelFor(tf5);
JButton rotation_a = new JButton("顺时针旋转");
rotation_a.addActionListener(this);
JButton rotation_b = new JButton("逆时针旋转");
rotation_b.addActionListener(this);
JLabel lab6 = new JLabel("");
pane1.add(lab5);
pane1.add(tf5);
pane1.add(rotation_a);
pane1.add(rotation_b);
pane1.add(lab6);
pane1.add(new JLabel(" 角度制"));
//错切
JLabel lab7 = new JLabel(" x方向错切量 ");
tf6 = new TextField(10);
lab7.setLabelFor(tf6);
JLabel lab8 = new JLabel(" y方向错切量 ");
tf7 = new TextField(10);
lab8.setLabelFor(tf7);
JButton shear = new JButton("错切");
shear.addActionListener(this);
pane1.add(lab7);
pane1.add(tf6);
pane1.add(lab8);
pane1.add(tf7);
pane1.add(shear);
pane1.add(new JLabel(" double型"));
//对称
JButton duichenx= new JButton("x轴对称");
duichenx.addActionListener(this);
JButton duicheny= new JButton("y轴对称");
duicheny.addActionListener(this);
JButton duichenxy= new JButton("原点对称");
duichenxy.addActionListener(this);
pane1.add(duichenx);
pane1.add(new JLabel(""));
pane1.add(duicheny);
pane1.add(new JLabel(""));
pane1.add(duichenxy);
pane1.add(new JLabel(""));
paintPanel=new JPanel();
paintPanel.setBackground(Color.white);
pane1.setBounds(20, 1, 600, 120);
this.add(pane1);
JButton reset = new JButton("重置");
reset.addActionListener(this);
reset.setBounds(630, 100, 100, 30);
this.add(reset);
paintPanel.setBounds(0,150,1000,550);
this.add(paintPanel);
this.setBackground(Color.white);
x0 = 500;
y0 = 200;
//北极星的坐标
// x=new int[]{0,0,50, 0,50,140, 0,140,80, 0,80,180,
// 0,180,80, 0,80,140, 0,140,50, 0,50,0,
// 0,0,-50, 0,-50,-140, 0,-140,-80, 0,-80,-180,
// 0,-180,-80, 0,-80,-140, 0,-140,-50, 0,-50,0};
// y=new int[]{0,200,110, 0,110,130, 0,130,30, 0,30,0,
// 0,0,-30, 0,-30,-130, 0,-130,-110,0,-110,-200,
// 0,-200,-110, 0,-110,-130,0,-130,-30, 0,-30,0,
// 0,0,30, 0,30,130, 0,130,110, 0,110,200};
xym=new double[][]{
{0,0,1},{0,100,1},{25,55,1},
{0,0,1},{25,55,1},{70,65,1},
{0,0,1},{70,65,1},{40,15,1},
{0,0,1},{40,15,1},{90,0,1},
{0,0,1},{90,0,1},{40,-15,1},
{0,0,1},{40,-15,1},{70,-65,1},
{0,0,1},{70,-65,1},{25,-55,1},
{0,0,1},{25,-55,1},{0,-100,1},
{0,0,1},{0,-100,1},{-25,-55,1},
{0,0,1},{-25,-55,1},{-70,-65,1},
{0,0,1},{-70,-65,1},{-40,-15,1},
{0,0,1},{-40,-15,1},{-90,0,1},
{0,0,1},{-90,0,1},{-40,15,1},
{0,0,1},{-40,15,1},{-70,65,1},
{0,0,1},{-70,65,1},{-25,55,1},
{0,0,1},{-25,55,1},{0,100,1}};
}
public static void main(String args[])
{
Beijixing mainFrame = new Beijixing();
mainFrame.setSize(1010, 700);
mainFrame.setTitle("北极星变换");
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
Graphics g=paintPanel.getGraphics();
String arg=e.getActionCommand();
int i,j;
m=new Matrix(xym);
/**
* 重置北极星的位置
*/
if(arg.equals("重置")){
axes(g);
drawStar(g,m.getX(),m.getY());
}
/**
* 平移,先沿x轴方向平移,然后沿y轴方向移动,因为觉得做斜线方向移动比较麻烦
*/
else if(arg.equals("平移")){
int sx,sy;
sx = Integer.parseInt(tf1.getText());
sy = Integer.parseInt(tf2.getText());
Matrix mOut = Transform.Translation(1,0);
for(i=0;i<sx;i++){
m = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,m.getX(),m.getY());
delay();//延时
}
mOut = Transform.Translation(0,1);
for(i=0;i<sy;i++){
m = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,m.getX(),m.getY());
delay();//延时
}
}
/**
* 缩放,为了表示动态,所以分了10次阶段放大或缩小
* 缩放中心是原点,但是会有所偏离,我认为可能是因为double转int所出现的偏差集合
*/
else if(arg.equals("缩放")){
double sx,sy;
sx=Double.parseDouble(tf3.getText());
sy=Double.parseDouble(tf4.getText());
for(i=1;i<=10;i++){
Matrix mOut = Transform.Scaling(((sx-1)/10)*i+1,((sy-1)/10)*i+1);
Matrix mNew = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,mNew.getX(),mNew.getY());
for(j=0;j<5;j++){
delay();
}
}
}
/**
* 以每次1度的角度顺时针慢慢旋转北极星
*/
else if(arg.equals("顺时针旋转")){
double theta;
int w=Integer.parseInt(tf5.getText());
theta = (double)1/180*Math.PI;
Matrix mOut = Transform.Rotation(theta);
for(i=0;i<w;i++){
m = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,m.getX(),m.getY());
delay();//延时
}
}
/**
* 以每次1度的角度逆时针慢慢旋转北极星
*/
else if(arg.equals("逆时针旋转")){
double theta;
int w=Integer.parseInt(tf5.getText());
theta = -(double)1/180*Math.PI;
Matrix mOut = Transform.Rotation(theta);
for(i=0;i<w;i++){
m = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,m.getX(),m.getY());
delay();//延时
}
}
/**
* 错切,x,y方向分10次变换
*/
else if(arg.equals("错切")){
double sx,sy;
sx=Double.parseDouble(tf6.getText());
sy=Double.parseDouble(tf7.getText());
for(i=1;i<=10;i++){
Matrix mOut = Transform.Shear((sx/10)*i,(sy/10)*i);
Matrix mNew = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,mNew.getX(),mNew.getY());
delay();//延时
}
}
/**
* 先移动偏离中心位置,然后x轴对称
*/
else if(arg.equals("x轴对称")){
//先平移
Matrix mOut = Transform.Translation(0, 1);
for(i=0;i<100;i++){
m = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,m.getX(),m.getY());
delay();//延时
}
for(i=0;i<50;i++){
delay();
}
mOut = Transform.Symmetry(true,false);
m=Matrix.MatrixMul(m,mOut);
drawStar(g,m.getX(),m.getY());
}
/**
* 先移动偏离中心位置,然后y轴对称
*/
else if(arg.equals("y轴对称")){
Matrix mOut = Transform.Translation(1, 0);
for(i=0;i<100;i++){
m = Matrix.MatrixMul(m,mOut);
axes(g);
drawStar(g,m.getX(),m.getY());
delay();//延时
}
for(i=0;i<50;i++){
delay();
}
mOut = Transform.Symmetry(false,true);
m=Matrix.MatrixMul(m,mOut);
drawStar(g,m.getX(),m.getY());
}
/**
* 先移动偏离中心位置,然后原点对称
*/
else if(arg.equals("原点对称")){
Matrix mOut = Transform.Translation(1, 1);
JAVA 北极星二维变换 矩阵做法
5星 · 超过95%的资源 需积分: 9 5 浏览量
2012-01-08
14:46:16
上传
评论 3
收藏 15KB RAR 举报
zsdt345a780rfajwet
- 粉丝: 17
- 资源: 11