// Processed by NMI's Java Code Viewer 4.8.2 © 1997-2000 B. Lemaire // Website: http://njcv.htmlplanet.com E-mail: info@njcv.htmlplanet.com // Copy registered to Evaluation Copy // Source File Name: Board.java import java.awt.*; import java.io.File; class Board extends Canvas { Panel chooseLetter; Image offImage; Graphics offGraphics; Toolkit toolkit; MediaTracker tracker; Image playboard; Image tile; Image joker; Image beforeDragImage; Graphics beforeDrag; Image tileImage; Graphics tileGraphics; int draggedPreLocation[]; char draggedLetter; Image draggedTile; Point margins; Point preLocation; boolean isTileDragged; char bankLettersUntouched[]; boolean jokerIsBeingDragged; int jokerDesiredLocation[]; ChooseDialog chooseJoker; Image tempImage; Graphics tempGraphics; Frame target; public Board(Frame frame) { chooseLetter = new Panel(); toolkit = getToolkit(); draggedPreLocation = new int[3]; margins = new Point(0, 0); preLocation = new Point(0, 0); isTileDragged = false; bankLettersUntouched = new char[7]; jokerIsBeingDragged = false; jokerDesiredLocation = new int[3]; resize(580, 590); loadImages(); target = frame; chooseJoker = new ChooseDialog(target, this); chooseJoker.hide(); } public void loadImages() { tracker = new MediaTracker(this); playboard = toolkit.getImage("images" + File.separator + "board.jpg"); tile = toolkit.getImage("images" + File.separator + "tile.jpg"); joker = toolkit.getImage("images" + File.separator + "joker.jpg"); tracker.addImage(playboard, 0); tracker.addImage(tile, 0); tracker.addImage(joker, 0); try { tracker.waitForAll(); } catch(InterruptedException _ex) { } toolkit = null; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { if(!tracker.checkAll()) { g.clearRect(0, 0, size().width, size().height); return; } if(offGraphics == null) { offImage = createImage(size().width, size().height); offGraphics = offImage.getGraphics(); offGraphics.drawImage(playboard, 0, 0, this); tempImage = createImage(33, 30); tempGraphics = tempImage.getGraphics(); tileImage = createImage(33, 30); tileGraphics = tileImage.getGraphics(); beforeDragImage = createImage(size().width, size().height); beforeDrag = beforeDragImage.getGraphics(); } g.drawImage(offImage, 0, 0, this); } public Image createTile(char c, boolean flag, boolean flag1) { boolean flag2 = false; tileGraphics.drawImage(tile, 0, 0, this); Font font = new Font("Helvetica", 1, 20); c = Character.toUpperCase(c); if(flag1) tileGraphics.setColor(Color.yellow); else tileGraphics.setColor(Color.black); if(!flag) { int i; try { i = ClientData.letterValues[Integer.valueOf(String.valueOf(c - 65)).intValue()]; } catch(ArrayIndexOutOfBoundsException _ex) { i = 3; } tileGraphics.setFont(font); tileGraphics.drawString(String.valueOf(c), 3, 20); font = new Font("Helvetica", 1, 10); tileGraphics.setFont(font); if(i >= 10) tileGraphics.drawString(String.valueOf(i), 19, 25); else tileGraphics.drawString(String.valueOf(i), 22, 25); } if(flag) if(c == '*') { tileGraphics.drawImage(joker, 0, 0, this); } else { int j = ClientData.letterValues[Integer.valueOf(String.valueOf(Character.toUpperCase(c) - 65)).intValue()]; Font font1 = new Font("Helvetica", 1, 20); tileGraphics.setFont(font1); tileGraphics.drawString(String.valueOf(Character.toUpperCase(c)), 3, 20); font1 = new Font("Helvetica", 1, 10); tileGraphics.setFont(font1); if(j >= 10) tileGraphics.drawString(String.valueOf(j), 19, 25); else tileGraphics.drawString(String.valueOf(j), 22, 25); tileGraphics.setColor(Color.red); tileGraphics.drawString("*", 24, 11); tileGraphics.setColor(Color.black); } return tileImage; } public int[] locationOnBoard(int i, int j) { int ai[] = new int[3]; int k = 0; int l = 0; int i1 = 0; if(i < 573 && i > 6 && j < 528 && j > 6) { for(int j1 = 0; j1 < 15; j1++) { if(i <= j1 * 38 + 6 || i >= (j1 + 1) * 38 + 3) continue; l = j1 + 1; break; } for(int l1 = 0; l1 < 15; l1++) { if(j <= l1 * 35 + 6 || j >= (l1 + 1) * 35 + 3) continue; k = l1 + 1; break; } } if(i < 567 && i > 272 && j < 577 && j > 544) { for(int k1 = 0; k1 < 15; k1++) { if(i <= k1 * 43 + 273 || i >= (k1 + 1) * 43 + 264) continue; i1 = k1 + 1; break; } } if(l == 0 || k == 0) { ai[0] = ai[1] = 0; } else { ai[0] = k; ai[1] = l; } ai[2] = i1; return ai; } public Point calculateMargins(int ai[], int i, int j) { Point point = new Point(0, 0); if(ai[0] != 0) { point.x = i - ((ai[1] - 1) * 38 + 8); point.y = j - ((ai[0] - 1) * 35 + 8); } else { point.x = i - ((ai[2] - 1) * 43 + 274); point.y = j - 547; } return point; } public Image dragImaging(int i, int j, boolean flag) { tempGraphics.clipRect(0, 0, 33, 30); if(flag) tempGraphics.drawImage(beforeDragImage, -i, -j, this); else tempGraphics.drawImage(playboard, -i, -j, this); return tempImage; } public void clearBankLetters() { for(byte byte0 = 0; byte0 < 7; byte0++) { bankLettersUntouched[byte0] = '\0'; ClientData.bankLetters[byte0] = '\0'; offGraphics.drawImage(dragImaging(byte0 * 43 + 274, 547, false), byte0 * 43 + 274, 547, this); } repaint(); } public void newBankLetters() { for(byte byte0 = 0; byte0 < 7; byte0++) { bankLettersUntouched[byte0] = ClientData.bankLetters[byte0]; if(ClientData.bankLetters[byte0] != 0) if(ClientData.bankLetters[byte0] == '*') offGraphics.drawImage(createTile(ClientData.bankLetters[byte0], true, true), byte0 * 43 + 274, 547, this); else offGraphics.drawImage(createTile(ClientData.bankLetters[byte0], false, true), byte0 * 43 + 274, 547, this); } repaint(); } public void clearBoard() { offGraphics.drawImage(playboard, 0, 0, this); repaint(); } public void updateTilesOnBoard() { for(byte byte0 = 0; byte0 < 15; byte0++) { for(byte byte1 = 0; byte1 < 15; byte1++) if(ClientData.canBeDragged[byte0][byte1]) { ClientData.canBeDragged[byte0][byte1] = false; offGraphics.drawImage(createTile(ClientData.boardLetters[byte0][byte1], false, false), byte1 * 38 + 8, byte0 * 35 + 8, this); } } repaint(); } public boolean handle_mouseDown(Event event, int i, int j) { if(ClientData.myTurn) { int ai[] = locationOnBoard(i, j); if(ai[0] != 0 || ai[2] != 0) { if(ai[0] != 0) { if(ClientData.canBeDragged[ai[0] - 1][ai[1] - 1]) draggedLetter = ClientData.boardLetters[ai[0] - 1][ai[1] - 1]; else draggedLetter = '\0'; } else { draggedLetter = ClientData.bankLetters[ai[2] - 1]; } if(draggedLetter != 0) { isTileDragged = true; draggedPreLocation = ai; if(Character.isLowerCase(draggedLetter) || draggedLetter == '*') jokerIsBeingDragged = true; if(jokerIsBeingDragged) draggedTile = createTile('*', true, true); else draggedTile = createTile(draggedLetter, false, true); margins = calculateMargins(ai, i, j); preLocation.move(i, j); offGraphics.drawImage(dragImaging(preLocation.x - margins.x, preLocation.y - margins.y, false), preLocation.x - margins.x, preLocation.y - margins.y, this); beforeDrag.drawImage(offImage, 0, 0, this); } } } return true; } public boolean handle_mouseDrag(Event event, int i, int j) { if(isTileDragged) { offGraphics.drawImage(dragImaging(preLocation.x - margins.x, preLocation.y - margins.y, true), preLocation.x - margins.x, preLocation.y - margins.y, this); repaint(preLocation.x - margins.x, preLocation.y - margins.y, 33, 30); offGraphics.drawImage(draggedTile, i - margins.x, j - margins.y, this); repaint(i - margins.x, j - margins.y, 33, 30); preLocation.move(i, j); } return true; } public boolean handle_mouseUp(Event event, int i, int j) { if(isTileDragged) { int ai[] = locationOnBoard(i, j); offGraphics.drawImage(dragImaging(preLocation.x - margins.x, preLocation.y - margins.y, true), preLocation.x - margins.x, preLocation.y - margins.y, this); repaint(preLocation.x - margins.x, preLocation.y - margins.y, 33, 30); if(!jokerIsBeingDragged) { if(ai[0] != 0) { if(ClientData.boardLetters[ai[0] - 1][ai[1] - 1] == 0) { offGraphics.drawImage(draggedTile, (ai[1] - 1) * 38 + 8, (ai[0] - 1) * 35 + 8, this); ClientData.boardLetters[ai[0] - 1][ai[1] - 1] = draggedLetter; ClientData.canBeDragged[ai[0] - 1][ai[1] - 1] = true; updateTilePrevLocation(); } else { bringBack(); } } else if(ai[2] != 0) { if(ClientData.bankLetters[ai[2] - 1] == 0) { offGraphics.drawImage(draggedTile, (ai[2] - 1) * 43 + 274, 547, this); ClientData.bankLetters[ai[2] - 1] = draggedLetter; updateTilePrevLocation(); } else { bringBack(); } } else { bringBack(); } repaint(); beforeDrag.clearRect(0, 0, size().width, size().height); isTileDragged = false; } if(jokerIsBeingDragged) { boolean flag = false; if(draggedPreLocation[0] != 0) draggedTile = createTile(ClientData.boardLetters[draggedPreLocation[0] - 1][draggedPreLocation[1] - 1], true, true); if(ai[0] != 0) { if(ClientData.boardLetters[ai[0] - 1][ai[1] - 1] == 0) { offGraphics.drawImage(tile, (ai[1] - 1) * 38 + 8, (ai[0] - 1) * 35 + 8, this); repaint((ai[1] - 1) * 38 + 8, (ai[0] - 1) * 35 + 8, 33, 30); updateTilePrevLocation(); flag = true; } else { bringBack(); } } else if(ai[2] != 0) { if(ClientData.bankLetters[ai[2] - 1] == 0) { offGraphics.drawImage(createTile('*', true, true), (ai[2] - 1) * 43 + 274, 547, this); ClientData.bankLetters[ai[2] - 1] = '*'; updateTilePrevLocation(); } else { bringBack(); } } else { bringBack(); } repaint(); beforeDrag.clearRect(0, 0, size().width, size().height); isTileDragged = false; jokerIsBeingDragged = false; if(flag) { for(byte byte0 = 0; byte0 < 3; byte0++) jokerDesiredLocation[byte0] = ai[byte0]; boolean flag1 = false; chooseJoker.show(); } } } return true; } public boolean handleEvent(Event event) { if(event.target == this && event.id == 501) return handle_mouseDown(event, event.x, event.y); if(event.target == this && event.id == 506) return handle_mouseDrag(event, event.x, event.y); if(event.target == this && event.id == 502) return handle_mouseUp(event, event.x, event.y); if((event.target instanceof ChooseDialog) && event.id == 1001) { char c = Character.toLowerCase(event.arg.toString().toCharArray()[0]); updateBoardWithJoker(c); return true; } else { return false; } } public void updateBoardWithJoker(char c) { int ai[] = new int[3]; for(byte byte0 = 0; byte0 < 3; byte0++) { ai[byte0] = jokerDesiredLocation[byte0]; jokerDesiredLocation[byte0] = 0; } if(ai[0] != 0) { offGraphics.drawImage(createTile(c, true, true), (ai[1] - 1) * 38 + 8, (ai[0] - 1) * 35 + 8, this); ClientData.boardLetters[ai[0] - 1][ai[1] - 1] = c; ClientData.canBeDragged[ai[0] - 1][ai[1] - 1] = true; } repaint((ai[1] - 1) * 38 + 8, (ai[0] - 1) * 35 + 8, 33, 30); } public void updateTilePrevLocation() { if(draggedPreLocation[0] != 0) { ClientData.boardLetters[draggedPreLocation[0] - 1][draggedPreLocation[1] - 1] = '\0'; ClientData.canBeDragged[draggedPreLocation[0] - 1][draggedPreLocation[1] - 1] = false; return; } else { ClientData.bankLetters[draggedPreLocation[2] - 1] = '\0'; return; } } public void bringBack() { if(draggedPreLocation[0] != 0) { offGraphics.drawImage(draggedTile, (draggedPreLocation[1] - 1) * 38 + 8, (draggedPreLocation[0] - 1) * 35 + 8, this); return; } else { offGraphics.drawImage(draggedTile, (draggedPreLocation[2] - 1) * 43 + 274, 547, this); return; } } public void handleUndoButton() { for(byte byte0 = 0; byte0 < 15; byte0++) { for(byte byte1 = 0; byte1 < 15; byte1++) if(ClientData.canBeDragged[byte0][byte1]) { ClientData.canBeDragged[byte0][byte1] = false; ClientData.boardLetters[byte0][byte1] = '\0'; offGraphics.drawImage(dragImaging(byte1 * 38 + 8, byte0 * 35 + 8, false), byte1 * 38 + 8, byte0 * 35 + 8, this); } } for(byte byte2 = 0; byte2 < 7; byte2++) { ClientData.bankLetters[byte2] = bankLettersUntouched[byte2]; if(ClientData.bankLetters[byte2] != 0) { if(ClientData.bankLetters[byte2] == '*') offGraphics.drawImage(createTile('*', true, true), byte2 * 43 + 274, 547, this); else offGraphics.drawImage(createTile(ClientData.bankLetters[byte2], false, true), byte2 * 43 + 274, 547, this); } else { offGraphics.drawImage(dragImaging(byte2 * 43 + 274, 547, false), byte2 * 43 + 274, 547, this); } } repaint(); } public void handlePassButton() { handleUndoButton(); } public void handleDoneButton() { } }