Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6. import static java.lang.Math.*;
  7.  
  8. public class Geom extends JFrame {
  9. private class PaintPanel extends JPanel {
  10. double minX = -5;
  11. double minY = -5;
  12. double maxX = 5;
  13. double maxY = 5;
  14. int state = 0;
  15. double[][] cord = new double[3][3];
  16.  
  17. public PaintPanel() {
  18. addKeyListener(new KeyListener() {
  19. public void keyPressed(KeyEvent arg0) {
  20. }
  21.  
  22. public void keyReleased(KeyEvent arg0) {
  23. }
  24.  
  25. public void keyTyped(KeyEvent key) {
  26. if (key.getKeyChar() == ' ') {
  27. state = (state + 1) % 7;
  28. repaint();
  29. }
  30. System.err.println(state);
  31. }
  32. });
  33. setFocusable(true);
  34. }
  35.  
  36. public void paintComponent(Graphics g) {
  37. super.paintComponents(g);
  38. Graphics2D g2 = (Graphics2D) g;
  39. g2.setColor(Color.black);
  40. g2.fill(new Rectangle(getSize()));
  41. paintAxis(g2);
  42. g2.setColor(Color.yellow);
  43. if (state == 0) {
  44. cord = createNewFigure();
  45. } else {
  46. for (int i = 0; i < cord.length; ++i) {
  47. cord[i] = rotate(cord[i], Math.PI / 2, Math.PI / 2);
  48. }
  49. }
  50. drawPolygon(cord, g2);
  51. }
  52.  
  53. private double[][] createNewFigure() {
  54. return new double[][] { {-1, -1, 1},
  55. {-1, 2, 1},
  56. {-3, 3, 1},
  57. {-3, -1, 1}};
  58. }
  59.  
  60. private double[] scale(double[] a, double kx, double ky) {
  61. double[][] m = new double[][] { {kx, 0, 0},
  62. {0, ky, 0},
  63. {0, 0, 1} };
  64. return multiply(a, m)[0];
  65. }
  66.  
  67. private void drawPolygon(double[][] cord, Graphics2D g2) {
  68. for (int i = 0; i < cord.length; ++i) {
  69. drawLine(cord[i][0], cord[i][1], cord[(i + 1) % cord.length][0], cord[(i + 1) % cord.length][1], g2);
  70. }
  71. }
  72.  
  73. private double[][] createNewTriangle() {
  74. double[][] cord = new double[3][];
  75. Random rm = new Random();
  76. double x = (rm.nextDouble() + 0.5) * maxX / 3;
  77. double y = (rm.nextDouble() + 0.5) * maxY / 3;
  78. cord[0] = new double[] {0, 0, 1};
  79. cord[1] = new double[] {0, y, 1};
  80. cord[2] = new double[] {x, 0, 1};
  81. double dx = (rm.nextDouble() + 0.0) * maxX / 3 + maxX / 5;
  82. double dy = (rm.nextDouble() + 0.0) * maxY / 3 + maxY / 5;
  83. for (int i = 0; i < 3; ++i) {
  84. cord[i] = move(cord[i], dx, dy);
  85. }
  86. double angle = Math.PI * rm.nextDouble();
  87. for (int i = 0; i < 3; ++i) {
  88. cord[i] = rotate(cord[i], angle);
  89. }
  90. return cord;
  91. }
  92.  
  93. private double[] rotate(double[] a, double angle) {
  94. double[][] m = new double[][] { {cos(angle), sin(angle), 0},
  95. {-sin(angle), cos(angle), 0},
  96. {0, 0, 1}};
  97. return multiply(a, m)[0];
  98. }
  99.  
  100. private double[] move(double[] a, double dx, double dy) {
  101. double[][] m = new double[][] { {1, 0, 0},
  102. {0, 1, 0},
  103. {dx, dy, 1}};
  104. return multiply(a, m)[0];
  105. }
  106.  
  107. private double[][] multiply(double[] cord, double[][] b) {
  108. double[][] a = new double[][] {cord};
  109. double[][] res = new double[a.length][b[0].length];
  110. for (int i = 0; i < res.length; ++i) {
  111. for (int j = 0; j < res[i].length; ++j) {
  112. for (int k = 0; k < a[i].length; ++k) {
  113. res[i][j] += a[i][k] * b[k][j];
  114. }
  115. }
  116. }
  117. return res;
  118. }
  119.  
  120. private void paintAxis(Graphics2D g2) {
  121. g2.setColor(Color.green);
  122. drawLine(minX, 0, maxX, 0, g2);
  123. drawLine(0, minY, 0, maxY, g2);
  124. }
  125.  
  126. private void drawLine(double x, double y, double x2, double y2, Graphics2D g2) {
  127. int ix = screenX(x);
  128. int iy = screenY(y);
  129. int ix2 = screenX(x2);
  130. int iy2 = screenY(y2);
  131. g2.drawLine(ix, iy, ix2, iy2);
  132. }
  133.  
  134. private int screenY(double y) {
  135. Dimension dim = getSize();
  136. return (int) (dim.height * (1 - (y - minX) / (maxX - minX)));
  137. }
  138.  
  139. private int screenX(double x) {
  140. Dimension dim = getSize();
  141. return (int) (dim.width * (x - minX) / (maxX - minX));
  142. }
  143. }
  144.  
  145. public Geom() {
  146. Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  147. setBounds(dim.width / 4, dim.height / 4, dim.width / 2, dim.height / 2);
  148. add(new PaintPanel());
  149. setDefaultCloseOperation(EXIT_ON_CLOSE);
  150. setVisible(true);
  151. }
  152.  
  153. public static void main(String[] args) {
  154. new Geom();
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement