Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. /*
  2. * Created on 2005-06-15
  3. *
  4. * TODO To change the template for this generated file go to
  5. * Window - Preferences - Java - Code Style - Code Templates
  6. */
  7. package rw.animacja;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Cursor;
  11. import java.awt.Graphics;
  12. import java.awt.GraphicsDevice;
  13. import java.awt.GraphicsEnvironment;
  14. import java.awt.Image;
  15. import java.awt.Point;
  16. import java.awt.Rectangle;
  17. import java.awt.Toolkit;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.event.KeyEvent;
  21. import java.awt.event.KeyListener;
  22. import java.awt.image.BufferStrategy;
  23. import java.awt.image.MemoryImageSource;
  24.  
  25. import javax.swing.JFrame;
  26. import javax.swing.Timer;
  27.  
  28. /**
  29. * Animacja 3D - kula (model punktowy)
  30. *
  31. * @author Rafał Wileczek
  32. * @created 25 lipca 2006
  33. */
  34. public class Animacja3D extends JFrame implements KeyListener, ActionListener {
  35.  
  36. private static final long serialVersionUID = 2596382981127077399L;
  37.  
  38. // Zmienne związane z przechodzeniem na pełny ekran
  39. private GraphicsDevice device;
  40.  
  41. private GraphicsEnvironment environment;
  42.  
  43. private BufferStrategy strategy;
  44.  
  45. private Integer[][] kula = null;
  46.  
  47. private Integer[][] kula_pers = null;
  48.  
  49. Integer odl_ekr = 1700;
  50.  
  51. Integer xo = 0;
  52.  
  53. Integer yo = 1;
  54.  
  55. Integer zo = 30;
  56.  
  57. Double rad_incr = 2 * Math.PI / 720;
  58.  
  59. Double kat = 0.0;
  60.  
  61. Integer dx;
  62.  
  63. Integer dy;
  64.  
  65. Integer punkty_length;
  66.  
  67. private Timer timer = null;
  68.  
  69. private int czas = 10;
  70.  
  71. /**
  72. * Konstruktor klasy Animacja3D
  73. *
  74. */
  75. public Animacja3D() {
  76. // Ustawienia okna
  77. setUndecorated(true);
  78. setResizable(false);
  79.  
  80. // Okno odbiera zdarzenia z klawiszy
  81. setFocusable(true);
  82. addKeyListener(this);
  83.  
  84. // Anulowanie domyślnego odświeżania ekranu
  85. setIgnoreRepaint(true);
  86.  
  87. // Pobranie "kontekstu urządzenia"
  88. environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  89. device = environment.getDefaultScreenDevice();
  90.  
  91. punkty_length = Kula.kula_punkty.length;
  92.  
  93. System.out.println(punkty_length);
  94.  
  95. kula = new Integer[punkty_length][3];
  96. kula_pers = new Integer[punkty_length][2];
  97.  
  98. timer = new Timer(czas, this);
  99. }
  100.  
  101. private void kopiujWzor() {
  102. for (int i = 0; i < punkty_length; i++) {
  103. for (int j = 0; j < 3; j++) {
  104. kula[i][j] = Kula.kula_punkty[i][j];
  105. }
  106. }
  107. }
  108.  
  109. // Przekształcenia afiniczne
  110. private void kula_obrotY(double alfa) {
  111. for (int i = 0; i < punkty_length; i++) {
  112. Double a = kula[i][0] * Math.cos(alfa) - kula[i][2]
  113. * Math.sin(alfa);
  114. Double b = kula[i][0] * Math.sin(alfa) + kula[i][2]
  115. * Math.cos(alfa);
  116. kula[i][0] = a.intValue();
  117. kula[i][2] = b.intValue();
  118. }
  119. }
  120.  
  121. private void kula_obrotX(double alfa) {
  122. for (int i = 0; i < punkty_length; i++) {
  123. Double a = kula[i][1] * Math.cos(alfa) - kula[i][2]
  124. * Math.sin(alfa);
  125. Double b = kula[i][1] * Math.sin(alfa) + kula[i][2]
  126. * Math.cos(alfa);
  127. kula[i][1] = a.intValue();
  128. kula[i][2] = b.intValue();
  129. }
  130. }
  131.  
  132. private void kula_obrotZ(double alfa) {
  133. for (int i = 0; i < punkty_length; i++) {
  134. Double a = kula[i][0] * Math.cos(alfa) - kula[i][1]
  135. * Math.sin(alfa);
  136. Double b = kula[i][0] * Math.sin(alfa) + kula[i][1]
  137. * Math.cos(alfa);
  138. kula[i][0] = a.intValue();
  139. kula[i][1] = b.intValue();
  140. }
  141. }
  142.  
  143. private void kula_pers() {
  144. for (int i = 0; i < punkty_length; i++) {
  145. kula_pers[i][0] = (kula[i][0] * 400) / (odl_ekr + kula[i][2])
  146. + dx;
  147. kula_pers[i][1] = (kula[i][1] * 400) / (odl_ekr + kula[i][2])
  148. + dy;
  149. }
  150. }
  151.  
  152. private void paintCzarny() {
  153. Graphics g = strategy.getDrawGraphics();
  154.  
  155. Rectangle rect = this.getBounds();
  156. g.setColor(Color.black);
  157. g.fillRect(rect.x, rect.y, rect.width, rect.height);
  158.  
  159. strategy.show();
  160. }
  161.  
  162. /**
  163. * Tworzy niewidoczny kursor
  164. *
  165. * @return niewidoczny kursor
  166. */
  167. private Cursor createTransparentCursor() {
  168. int[] pixels = new int[16 * 16];
  169. Image image = Toolkit.getDefaultToolkit().createImage(
  170. new MemoryImageSource(16, 16, pixels, 0, 16));
  171. Cursor transparentCursor = Toolkit.getDefaultToolkit()
  172. .createCustomCursor(image, new Point(0, 0), "invisiblecursor");
  173. return transparentCursor;
  174. }
  175.  
  176. /**
  177. * Wejście w tryb pełnoekranowy
  178. */
  179. private void setFullScreen() {
  180. device.setFullScreenWindow(this);
  181. // Double buffering
  182. createBufferStrategy(2);
  183. strategy = getBufferStrategy();
  184. Rectangle r = this.getBounds();
  185. dx = (int) (r.getMaxX() / 2);
  186. dy = (int) (r.getMaxY() / 2);
  187. }
  188.  
  189. /**
  190. * Opuszczenie trybu pełnoekranowego
  191. */
  192. private void exitFullScreen() {
  193. device.setFullScreenWindow(null);
  194. strategy = null;
  195. }
  196.  
  197. /**
  198. * Otwarcie okna i rozpoczęcie wyświetlania
  199. */
  200. public void rozpocznijWyswietlanie() {
  201. // Wejście w tryb pełnoekranowy
  202. setFullScreen();
  203. paintCzarny();
  204. // Prezentacja okna
  205. setCursor(createTransparentCursor());
  206. setVisible(true);
  207.  
  208. timer.start();
  209. }
  210.  
  211. private void kresl() {
  212. // Przygotowujemy kulę
  213. kula_pers();
  214.  
  215. Graphics g = strategy.getDrawGraphics();
  216. g.setColor(Color.black);
  217. Rectangle r = this.getBounds();
  218. g.fillRect(0, 0, r.width, r.height);
  219. g.setColor(Color.GREEN);
  220. // Rysujemy kulę
  221. for (int i = 0; i < punkty_length; i++) {
  222. Integer x = kula_pers[i][0];
  223. Integer y = kula_pers[i][1];
  224.  
  225. g.drawLine(x, y, x, y);
  226. }
  227. strategy.show();
  228. }
  229.  
  230. /**
  231. * Zamknięcie okna i przywrócenie wartości początkowych klasy
  232. */
  233. public void zakonczWyswietlanie() {
  234. timer.stop();
  235. setCursor(null);
  236. exitFullScreen();
  237. setVisible(false);
  238. System.exit(0);
  239. }
  240.  
  241. // Akcja timera
  242. public void actionPerformed(ActionEvent e) {
  243. kopiujWzor();
  244. kula_obrotX(2 * kat);
  245. kula_obrotY(2 * kat / 3);
  246. kula_obrotZ(kat / 2);
  247. kresl();
  248. kat += rad_incr;
  249. }
  250.  
  251. // Interfejs KeyListener - implementacja
  252. public void keyTyped(KeyEvent e) {
  253. }
  254.  
  255. public void keyPressed(KeyEvent e) {
  256. if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  257. zakonczWyswietlanie();
  258. } else if (e.getKeyCode() == KeyEvent.VK_UP) {
  259. if (odl_ekr < 10000)
  260. odl_ekr += 50;
  261. } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  262. if (odl_ekr > 1000)
  263. odl_ekr -= 50;
  264. }
  265. }
  266.  
  267. public void keyReleased(KeyEvent e) {
  268. }
  269. }
Add Comment
Please, Sign In to add comment