Created
June 7, 2010 19:27
-
-
Save rafalw/429061 to your computer and use it in GitHub Desktop.
Animowany, szkieletowy, trójwymiarowy sześcian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package rw.animacja; | |
import javax.swing.*; | |
/** | |
* @author Rafał Wileczek | |
* | |
*/ | |
public class Szescian3D { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
// Wygląd "uniwersalny" Java SWING | |
try { | |
UIManager.put("swing.boldMetal", Boolean.FALSE); | |
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); | |
} catch (Exception e) { | |
} | |
WyswietlaczFrame frame = new WyswietlaczFrame(); | |
frame.rozpocznijWyswietlanie(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
/** | |
* Wyświetlacz | |
* | |
* @author Rafał Wileczek | |
* @created 4 maj 2005 | |
*/ | |
public class WyswietlaczFrame extends JFrame implements KeyListener, | |
ActionListener { | |
static final long serialVersionUID = 1; | |
// Zmienne związane z przechodzeniem na pełny ekran | |
private GraphicsDevice device; | |
private GraphicsEnvironment environment; | |
private BufferStrategy strategy; | |
private Integer[][] kwadrat_wzor = { { 1000, 1000, -1000 }, // e | |
{ 1000, -1000, -1000 }, // f | |
{ -1000, -1000, -1000 }, // g | |
{ -1000, 1000, -1000 }, // h | |
{ 1000, 1000, 1000 }, // a | |
{ 1000, -1000, 1000 }, // b | |
{ -1000, -1000, 1000 }, // c | |
{ -1000, 1000, 1000 } // d | |
}; | |
private Integer[][] kwadrat = null; | |
private Integer[][] kwadrat_pers = null; | |
private Integer[][] siatka = { | |
// kwadrat z tyłu | |
{ 4, 5 }, // e-f | |
{ 5, 6 }, // f-g | |
{ 6, 7 }, // g-h | |
{ 7, 4 }, // h-e | |
// połączenia | |
{ 0, 4 }, // a-e | |
{ 1, 5 }, // b-f | |
{ 2, 6 }, // c-g | |
{ 3, 7 }, // d-h | |
// kwadrat z przodu | |
{ 0, 1 }, // a-b | |
{ 1, 2 }, // b-c | |
{ 2, 3 }, // c-d | |
{ 3, 0 } // d-a | |
}; | |
Integer odl_ekr = 6000; | |
Integer xo = 0; | |
Integer yo = 1; | |
Integer zo = 30; | |
Double rad_incr = 2 * Math.PI / 720; | |
Double kat = 0.0; | |
Integer dx; | |
Integer dy; | |
private Timer timer = null; | |
private int czas = 10; | |
/** | |
* Konstruktor klasy WyswietlaczFrame | |
* | |
*/ | |
public WyswietlaczFrame() { | |
// 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(); | |
kwadrat = new Integer[8][3]; | |
kwadrat_pers = new Integer[8][2]; | |
timer = new Timer(czas, this); | |
} | |
private void kopiujWzor() { | |
for (int i = 0; i < 8; i++) { | |
for (int j = 0; j < 3; j++) { | |
kwadrat[i][j] = kwadrat_wzor[i][j]; | |
} | |
} | |
} | |
// Przekształcenia afiniczne | |
private void kwadrat_obrotY(double alfa) { | |
for (int i = 0; i < 8; i++) { | |
Double a = kwadrat[i][0] * Math.cos(alfa) - kwadrat[i][2] | |
* Math.sin(alfa); | |
Double b = kwadrat[i][0] * Math.sin(alfa) + kwadrat[i][2] | |
* Math.cos(alfa); | |
kwadrat[i][0] = a.intValue(); | |
kwadrat[i][2] = b.intValue(); | |
} | |
} | |
private void kwadrat_obrotX(double alfa) { | |
for (int i = 0; i < 8; i++) { | |
Double a = kwadrat[i][1] * Math.cos(alfa) - kwadrat[i][2] | |
* Math.sin(alfa); | |
Double b = kwadrat[i][1] * Math.sin(alfa) + kwadrat[i][2] | |
* Math.cos(alfa); | |
kwadrat[i][1] = a.intValue(); | |
kwadrat[i][2] = b.intValue(); | |
} | |
} | |
private void kwadrat_obrotZ(double alfa) { | |
for (int i = 0; i < 8; i++) { | |
Double a = kwadrat[i][0] * Math.cos(alfa) - kwadrat[i][1] | |
* Math.sin(alfa); | |
Double b = kwadrat[i][0] * Math.sin(alfa) + kwadrat[i][1] | |
* Math.cos(alfa); | |
kwadrat[i][0] = a.intValue(); | |
kwadrat[i][1] = b.intValue(); | |
} | |
} | |
private void kwadrat_pers() { | |
for (int i = 0; i < 8; i++) { | |
kwadrat_pers[i][0] = (kwadrat[i][0] * 768) | |
/ (odl_ekr + kwadrat[i][2]) + dx; | |
kwadrat_pers[i][1] = (kwadrat[i][1] * 768) | |
/ (odl_ekr + kwadrat[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 kwadrat | |
kwadrat_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 kwadrat | |
for (int i = 0; i < 12; i++) { | |
Integer p1 = siatka[i][0]; | |
Integer p2 = siatka[i][1]; | |
Integer x1 = kwadrat_pers[p1][0]; | |
Integer x2 = kwadrat_pers[p2][0]; | |
Integer y1 = kwadrat_pers[p1][1]; | |
Integer y2 = kwadrat_pers[p2][1]; | |
g.drawLine(x1, y1, x2, y2); | |
} | |
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(); | |
kwadrat_obrotX(2 * kat); | |
kwadrat_obrotY(2 * kat / 3); | |
kwadrat_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 < 20000) | |
odl_ekr += 200; | |
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) { | |
if (odl_ekr > 400) | |
odl_ekr -= 200; | |
} | |
} | |
public void keyReleased(KeyEvent e) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment