Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created on 2005-06-15
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package rw.animacja;
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Graphics;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.Image;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferStrategy;
- import java.awt.image.MemoryImageSource;
- import javax.swing.JFrame;
- import javax.swing.Timer;
- /**
- * Animacja 3D - kula (model punktowy)
- *
- * @author Rafał Wileczek
- * @created 25 lipca 2006
- */
- public class Animacja3D extends JFrame implements KeyListener, ActionListener {
- private static final long serialVersionUID = 2596382981127077399L;
- // Zmienne związane z przechodzeniem na pełny ekran
- private GraphicsDevice device;
- private GraphicsEnvironment environment;
- private BufferStrategy strategy;
- private Integer[][] kula = null;
- private Integer[][] kula_pers = null;
- Integer odl_ekr = 1700;
- Integer xo = 0;
- Integer yo = 1;
- Integer zo = 30;
- Double rad_incr = 2 * Math.PI / 720;
- Double kat = 0.0;
- Integer dx;
- Integer dy;
- Integer punkty_length;
- private Timer timer = null;
- private int czas = 10;
- /**
- * Konstruktor klasy Animacja3D
- *
- */
- public Animacja3D() {
- // Ustawienia okna
- setUndecorated(true);
- setResizable(false);
- // Okno odbiera zdarzenia z klawiszy
- setFocusable(true);
- addKeyListener(this);
- // Anulowanie domyślnego odświeżania ekranu
- setIgnoreRepaint(true);
- // Pobranie "kontekstu urządzenia"
- environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
- device = environment.getDefaultScreenDevice();
- punkty_length = Kula.kula_punkty.length;
- System.out.println(punkty_length);
- kula = new Integer[punkty_length][3];
- kula_pers = new Integer[punkty_length][2];
- timer = new Timer(czas, this);
- }
- private void kopiujWzor() {
- for (int i = 0; i < punkty_length; i++) {
- for (int j = 0; j < 3; j++) {
- kula[i][j] = Kula.kula_punkty[i][j];
- }
- }
- }
- // Przekształcenia afiniczne
- private void kula_obrotY(double alfa) {
- for (int i = 0; i < punkty_length; i++) {
- Double a = kula[i][0] * Math.cos(alfa) - kula[i][2]
- * Math.sin(alfa);
- Double b = kula[i][0] * Math.sin(alfa) + kula[i][2]
- * Math.cos(alfa);
- kula[i][0] = a.intValue();
- kula[i][2] = b.intValue();
- }
- }
- private void kula_obrotX(double alfa) {
- for (int i = 0; i < punkty_length; i++) {
- Double a = kula[i][1] * Math.cos(alfa) - kula[i][2]
- * Math.sin(alfa);
- Double b = kula[i][1] * Math.sin(alfa) + kula[i][2]
- * Math.cos(alfa);
- kula[i][1] = a.intValue();
- kula[i][2] = b.intValue();
- }
- }
- private void kula_obrotZ(double alfa) {
- for (int i = 0; i < punkty_length; i++) {
- Double a = kula[i][0] * Math.cos(alfa) - kula[i][1]
- * Math.sin(alfa);
- Double b = kula[i][0] * Math.sin(alfa) + kula[i][1]
- * Math.cos(alfa);
- kula[i][0] = a.intValue();
- kula[i][1] = b.intValue();
- }
- }
- private void kula_pers() {
- for (int i = 0; i < punkty_length; i++) {
- kula_pers[i][0] = (kula[i][0] * 400) / (odl_ekr + kula[i][2])
- + dx;
- kula_pers[i][1] = (kula[i][1] * 400) / (odl_ekr + kula[i][2])
- + dy;
- }
- }
- private void paintCzarny() {
- Graphics g = strategy.getDrawGraphics();
- Rectangle rect = this.getBounds();
- g.setColor(Color.black);
- g.fillRect(rect.x, rect.y, rect.width, rect.height);
- strategy.show();
- }
- /**
- * Tworzy niewidoczny kursor
- *
- * @return niewidoczny kursor
- */
- private Cursor createTransparentCursor() {
- int[] pixels = new int[16 * 16];
- Image image = Toolkit.getDefaultToolkit().createImage(
- new MemoryImageSource(16, 16, pixels, 0, 16));
- Cursor transparentCursor = Toolkit.getDefaultToolkit()
- .createCustomCursor(image, new Point(0, 0), "invisiblecursor");
- return transparentCursor;
- }
- /**
- * Wejście w tryb pełnoekranowy
- */
- private void setFullScreen() {
- device.setFullScreenWindow(this);
- // Double buffering
- createBufferStrategy(2);
- strategy = getBufferStrategy();
- Rectangle r = this.getBounds();
- dx = (int) (r.getMaxX() / 2);
- dy = (int) (r.getMaxY() / 2);
- }
- /**
- * Opuszczenie trybu pełnoekranowego
- */
- private void exitFullScreen() {
- device.setFullScreenWindow(null);
- strategy = null;
- }
- /**
- * Otwarcie okna i rozpoczęcie wyświetlania
- */
- public void rozpocznijWyswietlanie() {
- // Wejście w tryb pełnoekranowy
- setFullScreen();
- paintCzarny();
- // Prezentacja okna
- setCursor(createTransparentCursor());
- setVisible(true);
- timer.start();
- }
- private void kresl() {
- // Przygotowujemy kulę
- kula_pers();
- Graphics g = strategy.getDrawGraphics();
- g.setColor(Color.black);
- Rectangle r = this.getBounds();
- g.fillRect(0, 0, r.width, r.height);
- g.setColor(Color.GREEN);
- // Rysujemy kulę
- for (int i = 0; i < punkty_length; i++) {
- Integer x = kula_pers[i][0];
- Integer y = kula_pers[i][1];
- g.drawLine(x, y, x, y);
- }
- strategy.show();
- }
- /**
- * Zamknięcie okna i przywrócenie wartości początkowych klasy
- */
- public void zakonczWyswietlanie() {
- timer.stop();
- setCursor(null);
- exitFullScreen();
- setVisible(false);
- System.exit(0);
- }
- // Akcja timera
- public void actionPerformed(ActionEvent e) {
- kopiujWzor();
- kula_obrotX(2 * kat);
- kula_obrotY(2 * kat / 3);
- kula_obrotZ(kat / 2);
- kresl();
- kat += rad_incr;
- }
- // Interfejs KeyListener - implementacja
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- zakonczWyswietlanie();
- } else if (e.getKeyCode() == KeyEvent.VK_UP) {
- if (odl_ekr < 10000)
- odl_ekr += 50;
- } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
- if (odl_ekr > 1000)
- odl_ekr -= 50;
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- }
Add Comment
Please, Sign In to add comment