/* Der umstuelpbare Wuerfel * Wuerfel.java * Author, Copyright: David Obermann Berlin 2000/2002 * * * */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.text.*; public class Wuerfel extends Applet implements Runnable{ // fuer die 4 phasen: private int phase; private int steps; private int allsteps; private int stepangle; private String[][] info={ {"","no parameters yet","",""}, {"","press mouse to stop or restart the applet","",""}}; // fuer die Berechnung der Punkte: private Math m; //mathe klasse private double sqrt3;//haeufig gebrauchte konstante private double a; //Seitenlaenge des Wuerfels private double sinalph; private double cosalph; private double cosvphi; private double sinvphi; //Schrift-Positionierung: private int writePos; //Projektion private int xTrans; private int yTrans; private double yScale; private double ProjAngle; //Speicherung der Koordinaten: private double edge_[][]; private int proj_[][]; private Graphics g; //Konstanten fuer double buffering: private Dimension size; private Image buffer; private Graphics bufferGraphics; private Thread animator; private boolean please_stop; //**************************************************************** //Funktionen: //**************************************************************** private int x_projection( double[] xyz) { return xTrans+(int)m.round((xyz[0])+ yScale*xyz[1]*m.cos(ProjAngle)); } //**************************************************************** private int y_projection( double[] xyz) { return yTrans-(int)m.round(xyz[2]+ yScale*xyz[1]*m.sin(ProjAngle) ); } //**************************************************************** public void paint(Graphics rg) { this.setBackground( Color.white); g.setColor( Color.black); //writeEdge(); bufferGraphics.setColor(this.getBackground()); bufferGraphics.fillRect(0,0,size.width,size.height); bufferGraphics.setColor(Color.black); // writeAngles(); // writeAngles2(); DrawLines(); g.drawImage(buffer,0,0,this); } //**************************************************************** private void CalcProjection(){ for (int i=0; i<12; i++){ proj_[i][0] = x_projection( edge_[i]); proj_[i][1] = y_projection( edge_[i]); } // writeProj(); } //**************************************************************** public void writeProj() { for (int i=0;i<12;i++) { Object[] args = {new Integer(i), new Integer(proj_[i][0]), new Integer (proj_[i][1]) }; String str = "proj_[{0}][x]={1} proj_[{0}][y]={2}"; bufferGraphics.drawString( MessageFormat.format( str, args), 12, writePos); writePos +=14; } } //**************************************************************** public void writeEdge() { // Object[] args= new Object[3]; for (int i=0;i<12;i++) { Object[] args = {new Integer(i), new Double( edge_[i][0]), new Double( edge_[i][1]), new Double( edge_[i][2]) }; String str = "edge[{0}][x]={1} edge[{0}][y]={2} edge[{0}][2]={3}"; bufferGraphics.drawString( MessageFormat.format( str, args), 12, writePos); writePos +=14; } } //**************************************************************** public void writeAngles() { Object[] args= new Object[6]; args[0] = new Double( 180.* m.acos(cosalph)/m.PI); args[1] = new Double( 180.* m.asin(sinalph)/m.PI); args[2] = new Double( 180.* m.acos(cosvphi)/m.PI); args[3] = new Double( 180.* m.asin(sinvphi)/m.PI); args[4] = new Integer(steps); args[5] = new Integer(phase); String str = "Alpha= {0} bzw. {1}, Phi= {2} bzw.{3} steps:{4}, phase {5} "; bufferGraphics.drawString( MessageFormat.format( str, args), 12, 60); //writePos +=14; } //**************************************************************** public void writeAngles2() { Object[] args= new Object[6]; args[0] = new Double( cosalph); args[1] = new Double( sinalph); args[2] = new Double( cosvphi); args[3] = new Double( sinvphi); args[4] = new Integer(steps); args[5] = new Integer(phase); String str="cosalph={0}sinalph{1}, cosphi={2} sinphi{3}steps:{4},phase{5} "; bufferGraphics.drawString( MessageFormat.format( str, args), 12, 75); //writePos +=14; } //**************************************************************** private void ConnectPoints( int i, int j) { int x=0; int y=1; bufferGraphics.drawLine( proj_[i-1][x] , proj_[i-1][y], proj_[j-1][x], proj_[j-1][y]); } //**************************************************************** private void DrawLines() { CalcProjection(); ConnectPoints( 2, 1); ConnectPoints( 2, 11); ConnectPoints( 2, 12); ConnectPoints( 2, 8); ConnectPoints( 2, 7); ConnectPoints( 1, 11); ConnectPoints( 1, 12); ConnectPoints( 1, 8); ConnectPoints( 1, 7); ConnectPoints( 7, 8); ConnectPoints( 4, 3); ConnectPoints( 4, 7); ConnectPoints( 4, 8); ConnectPoints( 4, 10); ConnectPoints( 4, 9); ConnectPoints( 3, 7); ConnectPoints( 3, 8); ConnectPoints( 3, 10); ConnectPoints( 3, 9); ConnectPoints( 9, 10); ConnectPoints( 6, 5); ConnectPoints( 6, 9); ConnectPoints( 6, 10); ConnectPoints( 6, 12); ConnectPoints( 6, 11); ConnectPoints( 5, 9); ConnectPoints( 5, 10); ConnectPoints( 5, 12); ConnectPoints( 5, 11); ConnectPoints( 11, 12); } //**************************************************************** private void CalcHelpVars() { //wieder von vorne beginnen: if (steps == allsteps) { steps = 0; if( 3 == phase) phase = 0; else phase++; } if (0 == phase) { sinalph = m.sin(m.PI/180. * (-90.+ steps * stepangle )); //- + cosalph = m.cos(m.PI/180. * (-90.+ steps * stepangle )); //+ + cosvphi = 1.-(1./(cosalph+1.)); sinvphi = m.sqrt(1.-cosvphi*cosvphi); } if ( 1== phase) { cosvphi = m.cos( m.PI/180.* ( 90. + steps * stepangle )); sinvphi = m.sin( m.PI/180.* ( 90. + steps * stepangle )); cosalph = 1./(1.-cosvphi)-1.; sinalph = m.sqrt(1.-cosalph*cosalph); } if ( 2 == phase) { sinalph = m.sin(m.PI/180. * (90. - steps * stepangle )); //- + cosalph = m.cos(m.PI/180. * (90. - steps * stepangle )); //+ + cosvphi = 1.-(1./(cosalph+1.)); sinvphi = -m.sqrt(1.-cosvphi*cosvphi); } if ( 3 == phase) { cosvphi = m.cos( m.PI/180.* ( 270. - steps * stepangle )); sinvphi = m.sin( m.PI/180.* ( 270. - steps * stepangle )); cosalph = (1./(1.-cosvphi)-1.); sinalph = -m.sqrt(1.-cosalph*cosalph); } } //**************************************************************** private void CalcPoints() { CalcHelpVars(); //erste ecke: edge_[0][0]= 0.; edge_[0][1]= 0.; edge_[0][2]= a/sqrt3; //zweite Ecke: edge_[1][0]= 0.; edge_[1][1]= 0.; edge_[1][2]= 0.; //dritte Ecke: edge_[2][0]= a*(cosalph-(sinalph/sqrt3)+1.); edge_[2][1]= 0.; edge_[2][2]= a*(sinalph + cosalph/sqrt3); //4. Ecke: edge_[3][0]= a*(cosalph + 1.); edge_[3][1]= 0.; edge_[3][2]= a*sinalph; //5. Ecke: edge_[4][0]= a*((cosalph-(sinalph/sqrt3)+1.)*cosvphi); edge_[4][1]= a*((cosalph-(sinalph/sqrt3)+1.)*sinvphi); edge_[4][2]= a*(sinalph + cosalph/sqrt3); //6. Ecke: edge_[5][0]= a*(cosalph+1.)*cosvphi; edge_[5][1]= a*(cosalph+1.)*sinvphi; edge_[5][2]= a*sinalph; //7. Ecke: edge_[6][0]= a; edge_[6][1]= 0.; edge_[6][2]= 0.; //8. Ecke: edge_[7][0]= a; edge_[7][1]= a/sqrt3; edge_[7][2]= 0.; //9. Ecke: edge_[8][0]= a*((-cosvphi + 1.)*cosalph + 1.); edge_[8][1]= a*sinvphi; edge_[8][2]= a*( (-cosvphi +1.)*sinalph ); //10. Ecke: edge_[9][0]= a*(cosalph *( -cosvphi - sinvphi/sqrt3 +1.)+1.); edge_[9][1]= a*(sinvphi - cosvphi/sqrt3); edge_[9][2]= a*(sinalph *( -cosvphi - sinvphi/sqrt3 + 1.)); //11. Ecke: edge_[10][0]= a*cosvphi; edge_[10][1]= a*sinvphi; edge_[10][2]= 0.; //12. Ecke: edge_[11][0]= a*(cosvphi+sinvphi/sqrt3); edge_[11][1]= a*(sinvphi-cosvphi/sqrt3); edge_[11][2]= 0.; //writeEdge(); } //**************************************************************** private void initBuffer() { size = this.getSize(); buffer= this.createImage(size.width, size.height); bufferGraphics=buffer.getGraphics(); bufferGraphics.setColor( Color.black); } //**************************************************************** public void init() { enableEvents(java.awt.event.MouseEvent.MOUSE_CLICKED); xTrans = 200; yTrans = 250; yScale = 3./4.; ProjAngle = m.PI/6.; steps = 0; phase =1; allsteps = 36; stepangle= 180/allsteps; g = this.getGraphics(); writePos = 14; edge_ = new double[12][3]; proj_ = new int[12][2]; this.add(new Label("Der umst\u00fclpbare W\u00fcrfel, Version 0.2")); this.add(new Label("\u00a9 David Obermann 2000/2002")); this.add(new Label("david.obermann@web.de")); sqrt3 = m.sqrt(3.); a = 100.0; initBuffer(); CalcPoints(); // writeAngles(); } //**************************************************************** public String getAppletInfo(){ return "Der umst\u00fclpbare W\u00fcrfel version 0.2\r\nAutor: David Obermann\r\ndavid.obermann@web.de"; } //**************************************************************** public String[][] getParameterInfo() { return info; } //**************************************************************** public void showStatus( String msg) { } //**************************************************************** public void run() { while(!please_stop) { steps++; CalcPoints(); //writeAngles(); //writeAngles2(); repaint(); try{ Thread.sleep(150);} catch (InterruptedException e) {;} } animator = null; } //**************************************************************** public void start() { if (null == animator ) { please_stop = false; animator = new Thread(this); animator.start(); } } //**************************************************************** public void stop() { please_stop=true; } //**************************************************************** /* public boolean mouseDown(Event e, int x, int y) { if (null!=animator) please_stop = true; else start(); return true; } */ protected void processMouseEvent(MouseEvent e) { switch (e.getID()){ case java.awt.event.MouseEvent.MOUSE_CLICKED: if (null!=animator) please_stop = true; else start(); } } //**************************************************************** public void update(Graphics g) {paint(g);} //**************************************************************** /* public boolean mouseDown(Event e, int x, int y) { steps++; Clear(); CalcPoints(); writeAngles(); writeAngles2(); paint(g); return true; } */ //**************************************************************** public void Clear() { Graphics ng = this.getGraphics(); Rectangle r = this.getBounds(); ng.setColor(this.getBackground()); ng.fillRect(r.x, r.y, r.width, r.height); } //**************************************************************** //**************************************************************** //**************************************************************** }