Файл Animation.java
import java.awt.AWTException; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Robot; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Line2D;
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer;
import java.lang.Math;
public class Animation extends JPanel implements ActionListener { Timer timer; Matrix matrix = new Matrix(); int r; int g; int b; Color color = new Color(r,g,b); private static int dispWidth = 640; private static int dispHeight = 480; private double angle = 0.0; private int x1 = dispWidth/2 + 70; private int y1 = dispHeight/2 + 70; private int x2 = dispWidth/2 + 30; private int y2 = dispHeight/2 + 30; private int x3 = dispWidth/2 + 30; private int y3 = dispHeight/2 - 70; //Находим координаты центра фигуры private int x = (x1 + x2 + x3) / 3; private int y = (y1 + y2 + y3) / 3; //Массив координат точки центра вращения начальный private double circlePoint[][]= { {x,y,1} }; //Моссив координат точки центра вращения целевой private double circleTargetPoint[][] = new double [1][3]; //Массив координат точек фигуры начальный private double circlePoint2 [][] = { {x1,y1,1}, {x2,y2,1}, {x3,y3,1} }; //Массив координат точек фигуры целевой private double circleTargetPoint2[][] = new double[3][3]; public double dx = 0; public double dy = 0; private int ddx = 1; private int ddy = 1; //Смещение центра координат //Целевая матрица трансформации 3х3 private double Mmult[][] = new double[3][3]; private double sx = 1; private double sy = 1; private double ds = 0.01; //массивы для координат полигона private int posx[] = new int [3]; private int posy[] = new int [3]; public Animation() { timer = new Timer(10, this); timer.start(); }
public void paint(Graphics g) { //Установливает цвет рисования в цвет фона g.setColor(getBackground()); //Перекрашивает окно фоновым цветом g.fillRect(0, 0, getWidth(), getHeight()); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); //Устанавливает цвет рисования g2d.setColor(Color.PINK); g2d.fillRect(100, 100, 100, 100); g2d.setColor(Color.BLUE); for (int i = 0; i < 3; i++) { for(int j =0; j < 2; j++) { if (j == 0) { posx[i]= (int) circleTargetPoint[i][j]; } else if (j == 1) { posy[i]= (int) circleTargetPoint[i][j]; } } } g2d.fillPolygon(posx,posy, 3); try { Robot robot = new Robot(); color = robot.getPixelColor(150, 150); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void main(String[] args) {
JFrame frame = new JFrame("Moving star"); frame.add(new Animation()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(dispWidth, dispHeight); frame.setLocationRelativeTo(null); frame.setVisible(true); }
public void actionPerformed(ActionEvent e) { int h = getHeight(); int w = getWidth(); //Находим результирующую матрицу трансформации для фигуры Mmult = matrix.MultypleMatrix(3,3,3,3,matrix.matrixTransfer(-(x + sx),-(y + sy)),matrix.matrixRotate(angle)); Mmult = matrix.MultypleMatrix (3,3,3,3, Mmult, matrix.matrixScale(sx,sy)); Mmult = matrix. MultypleMatrix (3,3,3,3, Mmult, matrix.matrixTransfer(x + dx,y + dy)); //Применяем результирующую матрицу трансформации ко всем точкам фигуры circleTargetPoint = matrix.MultypleMatrix(3,3,3,3, circlePoint2,Mmult); if (angle >= 2 * Math.PI) { angle = 0.0; } angle += Math.PI/180; if (sx >=2) { ds = -0.01; } else if (sx <= 0.1) { ds = 0.01; } sx += ds; sy += ds; for (int i = 0; i < 3; i++) { for(int j =0; j < 3; j++) { if (j == 1) { if (circleTargetPoint[i][j-1] >= w - 5) { ddx = -1; } else if (circleTargetPoint[i][j-1] <= 0 + 5) { ddx = 1; } if (circleTargetPoint[i][j] >= h - 5) { ddy = -1; } else if (circleTargetPoint[i][j] <= 0 + 5) { ddy = 1; } } } } dx += ddx; dy += ddy; repaint(); //Перерисовывает окно } }
Файл Matrix.java
public class Matrix { public Matrix() { } //Метод перемещения, матрица перемещения public double[][] matrixTransfer(double dx, double dy) { double matrixRez[][] = { {1, 0, 0}, {0, 1, 0}, {dx, dy, 1} }; return matrixRez; }
//Метод вращения, матрица вращения public double[][] matrixRotate(double angle) { double mRotate [][] = { {Math.cos(angle), Math.sin(angle), 0}, {-Math.sin(angle), Math.cos(angle), 0}, {0, 0, 1} }; return mRotate; } //Метод масштабирования, матрица масштабирования public double[][] matrixScale(double sx, double sy) { double matrixRez[][] = { {sx, 0, 0}, {0, sy, 0}, {0, 0, 1} }; return matrixRez; } //Метод произведения двух матриц //|m n| * |n k| //Результирующая матрица |m k| public double [][] MultypleMatrix (int m1str, int m1col, int m2str, int m2col, double m1[][], double m2[][]) { double mResult[][] = new double [m1col][m2str]; for (int i = 0; i < m1str; i++) { for (int j = 0; j < m2col; j++) { mResult[i][j] = 0; for (int q = 0; q < m1col; q++) { mResult[i][j] += m1[i][q] * m2[q][j]; } } } return mResult; } }
|