import java.awt.*; import java.util.*; import java.applet.Applet; import java.net.*; import java.awt.image.*; public class pseudospheres extends BufferedApplet { final int OK = 0; final int ABORTED = 1; int flag = OK; int[][][] pixelarray; int[] pixel; Image image; Image temp_image; int[] pixels; int w,h; Matrix3D m = new Matrix3D(); int a; int M=128; int N=128; double sphereMesh[][][] = new double[128][128][4]; double sphereMesh2[][][] = new double[128][128][4]; Color grays[]= new Color[255]; Random rand = new Random(); double rotation; double rotationstart; int barks=45; boolean scaleflag=false; double pi = 3.1415926535; public void init() { super.init(); for (int i=0;i<255;i++) grays[i]= new Color(i,i,i); for (int i = 0 ; i < M ; i++) for (int j = 0 ; j < N ; j++) { double u = i / (M - 1.0); double v = j / (N - 1.0); double theta = 2*pi*u; double phi = pi*v-pi/2; sphereMesh[i][j][0]=Math.cos(theta)*Math.cos(phi); sphereMesh[i][j][1]=Math.sin(theta)*Math.cos(phi); sphereMesh[i][j][2]=Math.sin(phi); sphereMesh[i][j][3]=0; } image = getImage(getDocumentBase(),"spheremap2.png"); MediaTracker mt = new MediaTracker(this); mt.addImage(image,0); try{ mt.waitForID(0); }catch(InterruptedException e){ } w = image.getWidth(this); h = image.getHeight(this); pixels = new int[w * h]; PixelGrabber pg = new PixelGrabber(image,0,0,w,h,pixels,0,w); try { pg.grabPixels(); }catch(InterruptedException e){ } if((pg.getStatus() & ImageObserver.ABORT) != 0){ flag = ABORTED; }else{ flag = OK; } repaint(); pixelarray=new int[w][h][4]; for (int i=0;i> 24) & 0xFF; pixelarray[i][j][1]=(pixels[i*w+j] >> 16); pixelarray[i][j][2]=(pixels[i*w+j] >> 8) & 0xFF; pixelarray[i][j][3]=(pixels[i*w+j] ) & 0xFF; } for (int i = 0 ; i < M ; i++) for (int j = 0 ; j < N ; j++) for (int k= 0;k<3;k++) { sphereMesh[i][j][k]*=(double) (1.0+(double)(pixelarray[i][j][1]/((double)2550.0))); } m.identity(); m.translate(250,250,0); m.rotateX(pi/2); m.scale(200,200,200); } public void render(Graphics g) { m.rotateZ(rotation/2000); if (scaleflag==true) m.scale(1.01,1.01,1.01); g.setColor(Color.gray); g.fillRect(0, 0, bounds().width, bounds().height); for (int i=0;i0){ g.drawLine((int) sphereMesh2[i][j][0],(int) sphereMesh2[i][j][1],(int) sphereMesh2[i+1][j][0],(int) sphereMesh2[i+1][j][1]); g.drawLine((int) sphereMesh2[i+1][j][0],(int) sphereMesh2[i+1][j][1],(int) sphereMesh2[i+1][j+1][0],(int) sphereMesh2[i+1][j+1][1]); g.drawLine((int) sphereMesh2[i][j+1][0],(int) sphereMesh2[i][j+1][1],(int) sphereMesh2[i+1][j+1][0],(int) sphereMesh2[i+1][j+1][1]); g.drawLine((int) sphereMesh2[i][j][0],(int) sphereMesh2[i][j][1],(int) sphereMesh2[i][j+1][0],(int) sphereMesh2[i][j+1][1]); } } } public boolean mouseDown(Event e, int x, int y) { rotationstart=x; return true; } public boolean mouseUp(Event e, int x, int y) { rotation-=x-rotationstart; return true; } public boolean keyUp(Event e, int key) { switch (key) { case ' ': scaleflag=true; } return true; } }