// ShapePuzzle2 by Exner Enterprises, C.1997-99 // November 10, 1999 // Author: Nicholas Exner // http://www.uiuc.edu/ph/www/exner // Free to redistribute/publish/modify pending that // the above comments are retained. import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.*; import java.awt.image.*; public class ShapePuzzle2 extends Applet { Image offscreenImage, menu; Graphics h; public int numImages = 10; ShapeShell2 a,b,cc,d,e,f; Vector objVect; public void init(){ setBackground(new Color(255,255,255)); ShapeShell2.loadImages(this); objVect = new Vector(); a=new ShapeShell2(336,8,3); b=new ShapeShell2(336,85,3); cc=new ShapeShell2(336,162,3); d=new ShapeShell2(231,20,8); e=new ShapeShell2(231,93,8); f=new ShapeShell2(231,160,8); objVect.addElement(a); objVect.addElement(b); objVect.addElement(cc); objVect.addElement(d); objVect.addElement(e); objVect.addElement(f); offscreenImage = createImage(this.size().width,this.size().height); h = offscreenImage.getGraphics(); repaint(); } public void paint(Graphics g) { h.setColor(Color.white); h.fillRect(0,0,this.size().width,this.size().height); Enumeration e1 = objVect.elements(); while(e1.hasMoreElements()) { ShapeShell2 c = (ShapeShell2)e1.nextElement(); c.show(h); } h.setColor(Color.black); h.drawLine(120,25,227,210); h.drawLine(120,25,12,210); h.drawLine(12,210,227,210); g.drawImage(offscreenImage,0,0,this); } public void scrambleEggs() { Enumeration e1 = objVect.elements(); while(e1.hasMoreElements()) { ShapeShell2 c = (ShapeShell2)e1.nextElement(); c.folderX=(int)(Math.random()*200+200); c.folderY=(int)(Math.random()*290); } return; } public boolean keyDown(Event evt, int key) { if (key == 'r') { System.out.println("Rotate ShapeShell2"); Enumeration e1 = objVect.elements(); while(e1.hasMoreElements()) { ShapeShell2 c = (ShapeShell2)e1.nextElement(); if (c.lastSel) { switch (c.shellPeice) { case 1: c.shellPeice = 3; break; case 2: c.shellPeice = 4; break; case 3: c.shellPeice = 5; break; case 4: c.shellPeice = 6; break; case 5: c.shellPeice = 2; break; case 6: c.shellPeice = 1; break; case 7: c.shellPeice = 8; break; case 8: c.shellPeice = 10; break; case 9: c.shellPeice = 7; break; case 10: c.shellPeice = 9; break; } } } } if (key == 's') { System.out.println("Scramble Eggs"); scrambleEggs(); } repaint(); return true; } public boolean mouseDown(Event evt, int x, int y) { System.out.println("x: "+(x)+"y: "+(y)); // Debug position Enumeration e1 = objVect.elements(); while(e1.hasMoreElements()) { ShapeShell2 c = (ShapeShell2)e1.nextElement(); if (c.selStat) c.reset(); if (!c.oneSelected()) c.setMousePoint(x,y); if (x > 0 && x < 10) if (y > 0 && y < 10) System.out.println("x:"+c.folderX+" y:"+c.folderY); } repaint(); return true; } public boolean mouseUp(Event evt, int x, int y) { Enumeration e1 = objVect.elements(); while(e1.hasMoreElements()) { ShapeShell2 c = (ShapeShell2)e1.nextElement(); c.contactUp(); } repaint(); return true; } public boolean mouseDrag(Event evt, int x, int y) { Enumeration e1 = objVect.elements(); while(e1.hasMoreElements()) { ShapeShell2 c = (ShapeShell2)e1.nextElement(); c.objectDrag(x,y); } repaint(); return true; } public void update(Graphics g) { paint(g); } } // End EggPuzzle /////////////////// ShapeShell2 Class /////////////////////////////// class ShapeShell2 extends Applet { int folderX=0, folderY=0, folderX_Old, folderY_Old, pointOnImageX, pointOnImageY; public int numImages = 10; static Image imageStrip; static private boolean selected = false; boolean mouseContact = false, lastSel=false; int shellPeice; static boolean selStat=false; private double angle=0.0; public ShapeShell2(int x, int y, int num) { folderX=folderX_Old=x; folderY=folderY_Old=y; shellPeice=num; } public ShapeShell2(int x, int y, int num, double degree) { folderX=folderX_Old=x; folderY=folderY_Old=y; shellPeice=num; angle = degree; } public static void loadImages(Applet parent){ imageStrip = parent.getImage(parent.getCodeBase(),"all.gif"); } public void reset() { lastSel = false; } public void drawStripImage(Graphics g, Image imagePtr, int totalImageNum, int imageNum, int drawX, int drawY) { int imageWidth = imagePtr.getWidth(this) / totalImageNum; int imageHeight = imagePtr.getHeight(this); //getHeight(); Graphics subArea = g.create(drawX, drawY, imageWidth, imageHeight); int stripX = (imageNum-1)*imageWidth; int stripY = 0; subArea.drawImage(imagePtr, -stripX, -stripY, this); subArea.dispose(); } public void oldCoord () { folderX_Old = folderX; folderY_Old = folderY; } public boolean oneSelected() { return selected; } public void setMousePoint(int x ,int y) { //145w 117h int width=0; int height=0; switch(shellPeice) { case 1: width = 110; height = 63; break; case 2: width = 67; height = 111; break; case 3: width = 126; height = 74; break; case 4: width = 76; height = 126; break; case 5: width = 111; height = 65; break; case 6: width = 69; height = 112; break; case 7: width = 64; height = 73; break; case 8: width = 74; height = 63; break; case 9: width = 74; height = 63; break; case 10: width = 64; height = 73; break; default: width = 145; height = 117; } if (x>=folderX && x <=(folderX+width)) { if (y>=folderY && y<=(folderY+height)) { pointOnImageX = (folderX - x); pointOnImageY = (folderY - y); contact(); } } } public void objectDrag(int x, int y) { if (mouseContact) { oldCoord(); folderX=(x+pointOnImageX); folderY=(y+pointOnImageY); } } public void show (Graphics g) { drawStripImage(g, imageStrip, numImages, shellPeice ,folderX,folderY); } public void contact() { if (mouseContact == false) { mouseContact = true; selected = true; lastSel = true; selStat = true; } else { mouseContact = false; selected = false; } } public void contactUp() { if (mouseContact) { mouseContact = false; selected = false;} } } // End Class Shell