// ipercubo2 by Sergio Savoldelli, 30 Nov. 2009: http:\\www.savoldelli.net import java.awt.*; import java.awt.event.*; import java.util.StringTokenizer; public class Ipercubo4 extends java.applet.Applet implements MouseListener, MouseMotionListener{ public int w,h, h2,w2, mx0,my0,iCol[],dcol,ster= 5,az,px; public int[][] Fx,Fy; public int[][] fx2,fy2; public int mov1,mov2,mov3,mov4,rid,ridb=6, ridc=8; public double fiX = .2, fiY = .3, dfi = .01, scale,cc,bb,bbcos,dd,ee,z,yz,pi= 3.141592,pib,pi2=pi*2; Image buffImage; Graphics buffGraphics; Color[] col; public Label ruotadati,ruotal,labela; private boolean movingCircleOut = false; Checkbox insideCheckbox, outsideCheckbox; String sliderTextR = "Cubi in & out (2X 0 - 180): "; public Scrollbar ruota; Font font; Panel controlPanel,optionPanel0;; public void init() { setSize(650, 550); setBackground(Color.black); setLayout(new FlowLayout()); CheckboxGroup CircleInOutGroup = new CheckboxGroup (); insideCheckbox = new Checkbox("si stereo ", CircleInOutGroup, !movingCircleOut); outsideCheckbox = new Checkbox("no stereo ", CircleInOutGroup, movingCircleOut); insideCheckbox.setForeground(Color.green); outsideCheckbox.setForeground(Color.cyan); font = new Font("Helvetica", Font.BOLD, 12); insideCheckbox.setFont(font); outsideCheckbox.setFont(font); add(insideCheckbox); add(outsideCheckbox); Label labela=new Label(" ",Label.CENTER); add(labela); controlPanel = new Panel(); controlPanel.setLayout(new GridLayout(3,1)); add("East ", controlPanel); addMouseListener(this); addMouseMotionListener(this); ruotadati = new Label(sliderTextR + Integer.toString(px)); Label ruotal=new Label(" Ruota l' ipercubo ",Label.LEFT); ruotal.setForeground(Color.white); ruotadati.setForeground(Color.white); ruotal.setFont(new Font("",Font.BOLD,12)); ruota=new Scrollbar(Scrollbar.HORIZONTAL,0,0,0,159); ruota.setUnitIncrement(1); ruota.setBlockIncrement(0); controlPanel.add(ruotal); controlPanel.add(ruotadati); controlPanel.add(ruota); w = 40+getSize().width; h=getSize().height; h2=(h/2+30); w2=(w/2-20); iCol = new int[6]; Fx = new int[6][]; Fy = new int[6][]; fx2 = new int[6][]; fy2 = new int[6][]; for (int i = 0; i < 4; i++){ Fx[i] = new int[5]; Fy[i] = new int[5]; fx2[i] = new int[5]; fy2[i] = new int[5];} scale = w2 / 2.5; col = new Color[256]; for (int i = 0; i < 256; i++) col[i] = new Color(i, i, i); buffImage = createImage(w, h); buffGraphics = buffImage.getGraphics(); addMouseListener(this); addMouseMotionListener(this); rotate(); } public void destroy() { removeMouseListener(this); removeMouseMotionListener(this); } public void mouseClicked(MouseEvent e){ } public void mousePressed(MouseEvent e) { mx0 = e.getX(); my0 = e.getY(); if (e.isMetaDown()) { rid=(rid+1)%6; if (rid ==0 )rid = 6; ridb=rid; ridc=ridb; if (ridb== 6)ridc=8; repaint();} } public boolean handleEvent(Event evt) { if (evt.target instanceof Scrollbar) { int value = ((Scrollbar)evt.target).getValue(); if (evt.target == ruota) { px = value; // cc =(px)/(pi2*2); cc =(px)/(pi2*4); pib= (cc%(pi+0.01))*(180/pi); ruotadati.setText(sliderTextR + Double.toString(pib)); bb= (Math.sin(cc)); bbcos= (Math.cos(cc)); repaint(); return true; } return true; } if ( evt.target == insideCheckbox || evt.target == outsideCheckbox) { repaint(); return true; } return true; } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) { int x1 = e.getX(); int y1 = e.getY(); mov1 = e.getX(); mov4 = e.getY(); z=z+0.03*((mov1-mov2));mov2=mov1; yz=yz +0.03* ((mov4-mov3));mov3=mov4; fiY += dfi*(x1 - mx0); mx0 = x1; fiX += dfi*(y1 - my0); my0 = y1; rotate(); repaint(); e.consume(); } public void mouseMoved(MouseEvent e) {} public void rotate(){ double ct = Math.cos(fiX), cf = Math.cos(fiY), st = Math.sin(fiX), sf = Math.sin(fiY), ix = (scale)*cf, kx = (scale)*sf, iy = (scale)*st*sf, jy = (scale)*ct, ky =( -scale)*st*cf, iz = -255*ct*sf, jz = 255*st, kz = 255*ct*cf; int Vx[] = {(int)(ix+kx), (int)(ix+kx), (int)(ix-kx), (int)(ix-kx), (int)(-ix+kx),(int)(-ix+kx),(int)(-ix-kx),(int)(-ix-kx)}, Vy[] = {(int)(iy+jy+ky), (int)(iy-jy+ky), (int)(iy-jy-ky), (int)(iy+jy-ky), (int)(-iy+jy+ky),(int)(-iy-jy+ky),(int)(-iy-jy-ky),(int)(-iy+jy-ky)}, x0123[] = {Vx[0],Vx[1],Vx[2],Vx[3]}, y0123[] = {Vy[0],Vy[1],Vy[2],Vy[3]}, x4567[] = {Vx[4],Vx[5],Vx[6],Vx[7]}, y4567[] = {Vy[4],Vy[5],Vy[6],Vy[7]}, x0374[] = {Vx[0],Vx[3],Vx[7],Vx[4]}, y0374[] = {Vy[0],Vy[3],Vy[7],Vy[4]}, x1265[] = {Vx[1],Vx[2],Vx[6],Vx[5]}, y1265[] = {Vy[1],Vy[2],Vy[6],Vy[5]}, x0451[] = {Vx[0],Vx[4],Vx[5],Vx[1]}, y0451[] = {Vy[0],Vy[4],Vy[5],Vy[1]}, x3762[] = {Vx[3],Vx[7],Vx[6],Vx[2]}, y3762[] = {Vy[3],Vy[7],Vy[6],Vy[2]}; Fx[0] = x0123 ; Fy[0] = y0123; Fx[3] = x4567; Fy[3] = y4567; Fx[1] = x0374; Fy[1] = y0374; Fx[4] = x1265; Fy[4] = y1265; Fx[2] = x0451; Fy[2] = y0451; Fx[5] = x3762; Fy[5] = y3762; iCol[0] = (int)255; iCol[1] = (int)0; iCol[2] = (int)0; } public void paint(Graphics g) { int px[] = new int[4], py[] = new int[4], px2[] = new int[4], py2[] = new int[4],px3[] = new int[65], py3[] = new int[65],px4[] = new int[65], py4[] = new int[65]; int pxg[] = new int[4], pyg[] = new int[4], px2g[] = new int[4], py2g[] = new int[4],px3g[] = new int[65], py3g[] = new int[65],px4g[] = new int[65], py4g[] = new int[65]; int b=0,ix=0; buffGraphics.setColor(Color.black); buffGraphics.fillRect(0,65, w, h); if (outsideCheckbox.getState()){ ster = -3000; } if (insideCheckbox.getState()){ ster = 5; } for (int i = 0; i < ridb; i++){ for (int j = 0; j < 4; j++){ px[j] = w2 + (int)(bbcos-dd)+(int)(Fx[i][j]/(1+Math.abs(bb))); py[j] = (int)(bb)+h2 - (int)(Fy[i][j]/(1+Math.abs(bb))); px2[j] = w2 + (int)(bb*120-dd)+(int)(Fx[i][j]/(2-Math.abs(bb))); py2[j] = h2- (int)(Fy[i][j]/(2-Math.abs(bb))); px4[b] = w2 +(int)(bbcos-dd)+ (int)(Fx[i][j]/(1+Math.abs(bb))); py4[b] = (int)(bb)+ h2 - (int) (Fy[i][j]/(1+Math.abs(bb))); px3[b] = w2 + (int)(bb*120-dd)+(int)(Fx[i][j]/(2-Math.abs(bb))); py3[b] = h2- (int)(Fy[i][j]/(2-Math.abs(bb))); ster=ster; pxg[j] = w2+(int)(ster-dd) +(int)(bbcos-2)+(int) (Fx[i][j]/(1.01+Math.abs(bb))); pyg[j] = (int)(bb)+h2-(int)(-ster+ee) - (int)(Fy[i][j]/(1.01+Math.abs(bb))); px2g[j] = w2-(int)(ster-dd) +(int)(bb*120-2) +(int)(Fx[i][j]/(2-Math.abs(bb))); py2g[j] = h2-(int)(-ster+ee) - (int)(Fy[i][j]/(2-Math.abs(bb))); px4g[b] =w2+(int)(ster-dd) + (int)(bbcos-2)+(int)(Fx[i][j]/(1.01+Math.abs(bb))); py4g[b] = (int)(bb)+ h2-(int)(-ster+ee) - (int)(Fy[i][j]/(1.01+Math.abs(bb))); px3g[b] = w2-(int)(ster-dd) +(int)(bb*120-2)+(int)(Fx[i][j]/(2-Math.abs(bb))); py3g[b] = h2-(int)(-ster+ee) -(int) (Fy[i][j]/(2-Math.abs(bb))); b = b+1; } buffGraphics.setColor(new Color(175,200,100,175)); buffGraphics.fillPolygon(px,py, 4); buffGraphics.setColor(new Color(255,0,0,255)); buffGraphics.drawPolygon(px,py,4); buffGraphics.setColor(new Color(200,0,250,127)); buffGraphics.fillPolygon(px2,py2, 4); buffGraphics.setColor(new Color(255,0,0,255)); buffGraphics.drawPolygon(px2,py2,4); //verde buffGraphics.setColor(new Color(0,255,0,255)); buffGraphics.drawPolygon(pxg,pyg, 4); buffGraphics.setColor(new Color(0,255,0,255)); buffGraphics.drawPolygon(px2g,py2g, 4); } g.drawImage(buffImage, 0, 0, this); showStatus( " Clicca con il tasto destro del mouse per aprire l' ipercubo. ################### N. Cubi = " + ridc); g.setFont(new Font("TimesRoman",Font.PLAIN,15)); g.setColor(new Color(255,255,0)); g.drawString(" Per aprire l'ipercubo. Clicca con il tasto destro del mouse N.cubi = " + ridc , 0,15); g.drawString(" Clicca e trascina il Mouse.---------Click and drag. " , 0,65); g.setColor(new Color(0,255,0)); g.drawLine(px3g[0],py3g[0],px4g[0],py4g[0]); g.drawLine(px3g[1],py3g[1],px4g[1],py4g[1]); g.drawLine(px3g[2],py3g[2],px4g[2],py4g[2]); g.drawLine(px3g[3],py3g[3],px4g[3],py4g[3]); g.drawLine(px3g[9],py3g[9],px4g[9],py4g[9]); g.drawLine(px3g[10],py3g[10],px4g[10],py4g[10]); g.drawLine(px3g[18],py3g[18],px4g[18],py4g[18]); g.drawLine(px3g[6],py3g[6],px4g[6],py4g[6]); g.setColor(new Color(255,0,0)); g.drawLine(px3[0],py3[0],px4[0],py4[0]); g.drawLine(px3[1],py3[1],px4[1],py4[1]); g.drawLine(px3[2],py3[2],px4[2],py4[2]); g.drawLine(px3[3],py3[3],px4[3],py4[3]); g.drawLine(px3[9],py3[9],px4[9],py4[9]); g.drawLine(px3[10],py3[10],px4[10],py4[10]); g.drawLine(px3[18],py3[18],px4[18],py4[18]); g.drawLine(px3[6],py3[6],px4[6],py4[6]); } public void update(Graphics g) { paint(g); } }