/* * RollingCubes.java * The RollingCubes game * Usage: 3.14159/2) { rotating=0; synchronized(waitthread) { waitthread.notify(); } } repaint(); pause(30); } public boolean mouseDown(Event evt, int xm, int ym) { int x = (xm-margin)/sz; int y = (ym-margin)/sz; if (mousemode==1 && x>=0 && x<3 && y>=0 && y<3) { mv.x = x; mv.y = y; mousemode=0; synchronized(waitthread) { waitthread.notify(); } } return true; } public void update(Graphics g) { if (back == null) { back = createImage(xpix,ypix); backgr = back.getGraphics(); backgr.setColor(getBackground()); backgr.fillRect(0,0,xpix,ypix); } if (rotating==1) { paint(backgr); alpha=0; rotating=2; } if (rotating==2) { rotate(backgr); } else if (rotating==0) { paint(backgr); } g.drawImage(back,0,0,this); } // Draw the box at the specified position void drawbox(Graphics g, int x0, int y0, int w, int h, int dir) { if (dir==TOP || dir==BOT || dir==EMPTY) { g.setColor(tcolors[dir]); g.fillRect(x0,y0,w,h); } else if (dir==N || dir==S) { g.setColor(dir==N?Color.black:Color.white); g.fillRect(x0,y0,w,h/2); g.setColor(dir==S?Color.black:Color.white); g.fillRect(x0,y0+(h-h/2),w,h/2); } else if (dir==E || dir==W) { g.setColor(dir==W?Color.black:Color.white); g.fillRect(x0,y0,w/2,h); g.setColor(dir==E?Color.black:Color.white); g.fillRect(x0+(w-w/2),y0,w/2,h); } g.setColor(border); g.drawRect(x0,y0,w,h); } public void paint(Graphics g) { for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { drawbox(g,margin+sz*i,margin+sz*j,sz,sz,data[i][j]); } } if (win==1) { g.setColor(Color.white); g.fillRect(margin+1,margin+1,xpix-2*margin,20); g.setColor(Color.red); g.drawString("Congratulations! You win!",margin+2,margin+15); } } // Pause for n ms public void pause(int n) { try { Thread.currentThread().sleep(n); } catch (InterruptedException e) {} } }