import java.applet.*; import java.awt.*; public class RockCanvas extends Canvas { Dimension offDimension; Image offImage; Graphics offGraphics; int rowSize, colSize, choiceRow, choiceCol; int laneWidth, ballSize, startHeight, bottomHeight; int boardWidth, origBoardWidth, boardHeight; Point pt; public static int topLeftOff; Dimension minSize; public Dimension getSize() { return getPreferredSize(); } public Dimension getPreferredSize() { return getMinimumSize(); } public synchronized Dimension getMinimumSize() { return minSize; } public RockCanvas() { offGraphics = null; rowSize = (Rocks.MAXSCREENY - 4) / (Rocks.MAXROWS+1); /* Extra 1 is for startHeight and BottomHeight */ colSize = (2 * rowSize)/3; topLeftOff = 2; laneWidth = colSize /2; ballSize = laneWidth; startHeight = laneWidth; bottomHeight = colSize; /* startHeight + bottomHeight = rowSize */ boardHeight = rowSize * (Rocks.MAXROWS + 1); boardWidth = laneWidth * (Rocks.MAXCOLS-1); origBoardWidth = boardWidth; minSize = new Dimension(Rocks.MAXSCREENX, Rocks.MAXSCREENY); } public void initCanvas() { rowSize = (Rocks.MAXSCREENY - 4) / (Rocks.numRows+1); /* Extra 1 is for startHeight and BottomHeight */ colSize = (2 * rowSize)/3; if (origBoardWidth < ((colSize/2) * (Rocks.numCols-1))) { colSize = 2 * (origBoardWidth / (Rocks.numCols - 1)); rowSize = (3 * colSize)/2; } topLeftOff = 2; laneWidth = colSize /2; ballSize = laneWidth; startHeight = laneWidth; bottomHeight = colSize; /* startHeight + bottomHeight = rowSize */ boardHeight = rowSize * (Rocks.numRows + 1); boardWidth = laneWidth * (Rocks.numCols-1); // minSize = new Dimension(Rocks.MAXSCREENX, Rocks.MAXSCREENY); repaint(); } public boolean mouseDown(Event evt, int x, int y) { int r, c, m; if (pt == null) pt = new Point(x, y); else { pt.x = x; pt.y = y; } r = (pt.y - topLeftOff) / startHeight; c = (pt.x - topLeftOff) / laneWidth; if ((Rocks.whoseTurn == Rocks.COMPUTER) || (Rocks.boardChanging)) Rocks.statusMsg.setText("Its not your Turn"); else { if ((r == 0) && (c >= 0) && (c < Rocks.numCols-1)) { Rocks.statusMsg.setText(""); Rocks.addBall(c); } else Rocks.statusMsg.setText("Add Rocks in Green Start Lanes."); } return true; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { int ax, ay, bx, by, cx, cy, xi, yi, si, dy; int r, c, c2, bn, ln; Dimension d = getSize(); boolean top, bottom, left, right; if ( (offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height) ) { offDimension = d; offImage = createImage(d.width, d.height); offGraphics = offImage.getGraphics(); } offGraphics.setColor(getBackground()); offGraphics.fillRect(0,0,d.width-1,d.height-1); offGraphics.setColor(Color.black); offGraphics.drawRect(topLeftOff, topLeftOff, boardWidth, boardHeight); if (Rocks.board != null) { offGraphics.setColor(Color.green); /* Green Start Lanes */ offGraphics.fillRect(topLeftOff+1, topLeftOff+1, (Rocks.numCols-1)*laneWidth, startHeight); offGraphics.setColor(Color.black); for (c = 0; c < Rocks.numCols-1; c++) { // offGraphics.drawString("" + c, topLeftOff+c*laneWidth+(laneWidth/3), topLeftOff-10); offGraphics.drawLine(topLeftOff+c*laneWidth, topLeftOff, topLeftOff+c*laneWidth, topLeftOff+startHeight); offGraphics.drawLine(topLeftOff+(c+1)*laneWidth-1, topLeftOff, topLeftOff+(c+1)*laneWidth-1, topLeftOff+startHeight); } for (r = 0; r < Rocks.numRows; r++) { for (c = 0; c < Rocks.numCols; c++) { if (Rocks.board[r][c][Rocks.GATETYPE] != Rocks.NOGATE) { offGraphics.setColor(Color.black); ax = topLeftOff + ((c-1) * laneWidth); bx = topLeftOff + ((c+1) * laneWidth) - 1; ay = topLeftOff + startHeight + (r * rowSize); by = topLeftOff + startHeight + ((r+1) * rowSize) - 1; cx = ax + ((bx - ax) / 2); cy = ay + ((by - ay) / 3); dy = cy + ((by - cy) / 2); switch (Rocks.board[r][c][Rocks.GATETYPE]) { case Rocks.REGULAR: /* Left */ // offGraphics.drawLine(ax, ay, ax, by); offGraphics.drawLine(ax, ay, ax, dy); /* Middle */ // offGraphics.drawLine(cx, cy, cx, by); /* Right */ // offGraphics.drawLine(bx, ay, bx, by); offGraphics.drawLine(bx, ay, bx, dy); /* Gate */ offGraphics.setColor(Color.black); offGraphics.fillRoundRect(cx-1,dy-1, 2, 2, 2, 2); offGraphics.setColor(Color.red); if (Rocks.board[r][c][Rocks.GATEPOSITION] == Rocks.LEFT) { offGraphics.drawLine(ax, cy, cx, cy); offGraphics.drawLine(ax + ((cx - ax)/2), cy, cx + ((bx-cx)/2), by); // offGraphics.drawLine(cx, by, bx, by); } else { offGraphics.drawLine(cx, cy, bx, cy); offGraphics.drawLine(cx + ((bx-cx)/2), cy, ax + ((cx - ax)/2), by); // offGraphics.drawLine(ax, by, cx, by); } break; case Rocks.LEFTEDGE: /* Left */ // offGraphics.drawLine(cx, ay, cx, by); // offGraphics.drawLine(bx, ay, bx, by); offGraphics.drawLine(cx, ay, cx, dy); offGraphics.drawLine(bx, ay, bx, dy); break; case Rocks.RIGHTEDGE: /* Right */ // offGraphics.drawLine(ax, ay, ax, by); // offGraphics.drawLine(cx, ay, cx, by); offGraphics.drawLine(ax, ay, ax, dy); offGraphics.drawLine(cx, ay, cx, dy); break; } } } } if (Rocks.ballsOnboard != -1) { bn = Rocks.ballsOnboard; while (bn != -1) { if (Rocks.balls[bn][Rocks.BALLCOLOR] == Rocks.RED) offGraphics.setColor(Color.red); else offGraphics.setColor(Color.blue); si = (Rocks.balls[bn][Rocks.SUBINDEX] % Rocks.MAXSUB); xi = (laneWidth * si) / Rocks.MAXSUB; yi = (laneWidth * si) / Rocks.MAXSUB; switch (Rocks.balls[bn][Rocks.LOCATION]) { case Rocks.INSTART: ax = topLeftOff + (laneWidth * Rocks.balls[bn][Rocks.INDEXC]); ay = topLeftOff; switch (Rocks.balls[bn][Rocks.WHEREINGATE]) { case 0: // ax = ax; ay = ay - yi; break; case 1: ax = ax + (laneWidth / 2) - (xi/2); ay = ay + (laneWidth / 6) - (yi/2); /* add a little to Y */ break; case 2: ax = ax - (laneWidth / 2) + (xi/2); ay = ay + (laneWidth / 6) - (yi/2); break; } // g.fillOval(ax, ay, ballSize, ballSize); offGraphics.fillRoundRect(ax,ay, ballSize, ballSize, ballSize, ballSize); break; case Rocks.INGATE: ln = Rocks.balls[bn][Rocks.INDEXC] - 1; if (Rocks.balls[bn][Rocks.WHEREINGATE] > 3) ln = ln + 1; if ((Rocks.balls[bn][Rocks.WHEREINGATE] != 3) && (Rocks.balls[bn][Rocks.WHEREINGATE] != 7)) { ax = topLeftOff + (laneWidth * ln); ay = topLeftOff + startHeight + rowSize * Rocks.balls[bn][Rocks.INDEXR]; ay = ay + laneWidth * (Rocks.balls[bn][Rocks.WHEREINGATE] % 4) - yi; } else { ax = topLeftOff + (laneWidth * ln); if (Rocks.balls[bn][Rocks.WHEREINGATE] == 3) ax = ax - (laneWidth / 2) + (xi/2); else ax = ax + (laneWidth / 2) - (xi/2); ay = topLeftOff + startHeight + rowSize * Rocks.balls[bn][Rocks.INDEXR]; ay = ay + 2 * laneWidth; // ay = ay + (laneWidth / 2) - (yi/2); ay = ay - (yi/2); } // g.fillOval(ax, ay, ballSize, ballSize); offGraphics.fillRoundRect(ax,ay, ballSize, ballSize, ballSize, ballSize); break; case Rocks.INBOTTOM: ax = topLeftOff + (laneWidth * Rocks.balls[bn][Rocks.INDEXC]); ay = topLeftOff + startHeight + rowSize * Rocks.numRows; ay = ay + (Rocks.balls[bn][Rocks.WHEREINGATE] * laneWidth) - yi; // g.fillOval(ax, ay, ballSize, ballSize); offGraphics.fillRoundRect(ax,ay, ballSize, ballSize, ballSize, ballSize); break; } bn = Rocks.balls[bn][Rocks.NEXTBALL]; } } g.drawImage(offImage, 0, 0, this); } } }