⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 letterbox.java

📁 WebHangman a game can be play on the website contain the total code of JAVA
💻 JAVA
字号:
package com.aztsoft.games.hangman;

import com.aztsoft.games.util.Rect;
import com.aztsoft.games.util.GameLogger;

import java.awt.*;

/**
 * The gui element that paints the letters and allows the user to select letters
 */
public class LetterBox {

    protected Font largeFont = new Font("Arial", Font.PLAIN, 18);
    protected Font smallFont = new Font("Arial", Font.PLAIN, 10);
    protected int largeFontHeight = 14;
    protected int smallFontHeight = 10;
    private final static int kMAX_LETTER_BOX_NUM_X = 9;
    private final static int kMAX_LETTER_BOX_NUM_Y = 3;

    private char[] lbox = new char[26];
    private int[] successes = new int[26];
    private int scounter, fcounter;
    private int[] failures = new int[26];
    private int[] availableCharSet;

    private int counter = 0;
    Rect rect;

    public LetterBox(Rect rect, int[] availCharSet) {
        this.rect = rect;
        this.availableCharSet = availCharSet;
    }

    public boolean belongsToFailed(int c) {
        boolean b = false;
        for (int i = 0; i < fcounter; i++) {
            int failure = failures[i];
            if (failure == c) {
                return true;
            }
        }
        return b;
    }

    public boolean belongsToSuccess(int c) {
        boolean b = false;
        for (int i = 0; i < scounter; i++) {
            int success = successes[i];
            if (success == c) {
                return true;
            }
        }
        return b;
    }

    public void addToSuccesses(char c) {
        successes[scounter] = c;
        scounter++;
    }

    public void addToFailures(char c) {
        failures[fcounter] = c;
        fcounter++;
    }

    public boolean contains(char c) {
        for (int i = 0; i < counter; i++) {
            if (lbox[i] == c) {
                return true;
            }
        }
        lbox[counter] = c;
        counter++;
        return false;
    }

    public void changeAvailableCharSet(int[] availableCharSet) {
        this.availableCharSet = availableCharSet;
    }


    public String toString() {
        String st = "";
        for (int i = 0; i < counter; i++) {
            st = st + lbox[i];
        }
        return st;
    }

    public int isHit(int x, int y) {
        int target = -1;
        int stepX = rect.width / kMAX_LETTER_BOX_NUM_X;
        int stepY = rect.height / kMAX_LETTER_BOX_NUM_Y;

        if (((x > rect.x) && (x < rect.x + rect.width)) && ((y > rect.y) && (y < rect.y + rect.height))) {
            int row = (y - rect.y) / stepY;
            int col = (x - rect.x) / stepX;
            int mult = row * kMAX_LETTER_BOX_NUM_X + col;
            if (mult < availableCharSet.length) {
                target = availableCharSet[mult];
            } else {
                target = -1;
            }
        }
        return target;

    }

    private int currentSelection = -1;

    public void select(int ch) {
        currentSelection = ch;
    }

    public void unselect() {
        currentSelection = -1;
    }

    public void paint(Graphics g) {
        String st = this.toString();
        g.setFont(largeFont);

        g.setColor(Color.black);
        g.setColor(Color.white);
        g.fillRect(rect.x, rect.y, rect.width, rect.height);
        g.setColor(Color.black);
        g.drawRect(rect.x, rect.y, rect.width, rect.height);
        int stepX = rect.width / kMAX_LETTER_BOX_NUM_X;
        int stepY = rect.height / kMAX_LETTER_BOX_NUM_Y;


        for (int x = 0; x < kMAX_LETTER_BOX_NUM_X; x++) {
            g.drawLine(rect.x + x * stepX, rect.y, rect.x + x * stepX, rect.y2);
        }
        for (int y = 0; y < kMAX_LETTER_BOX_NUM_Y; y++) {
            g.drawLine(rect.x, rect.y + y * stepY, rect.x2, rect.y + y * stepY);
            for (int x = 0; x < kMAX_LETTER_BOX_NUM_X; x++) {
                int posIdx = x + y * kMAX_LETTER_BOX_NUM_X;
                if (posIdx < availableCharSet.length) {
                    int tempInt = availableCharSet[posIdx];
                    if (tempInt == currentSelection) {
                        g.setColor(Color.black);
                        g.fillRect(rect.x + x * stepX, rect.y + y * stepY, stepX, stepY);
                        g.setColor(Color.white);
                    }
                    if (belongsToFailed(tempInt)) {
                        g.setColor(new Color(0xCC0000)); //red opaque
                        g.fillRect(rect.x + x * stepX + 1, rect.y + y * stepY + 1, stepX - 1, stepY);
                        g.setColor(Color.black);
                    } else if (belongsToSuccess(tempInt)) {
                        g.setColor(new Color(0x00CC00)); //green opaque
                        g.fillRect(rect.x + x * stepX + 1, rect.y + y * stepY + 1, stepX - 1, stepY);
                        g.setColor(Color.black);
                    }
                    g.drawString((char) tempInt + "", rect.x + 10 + x * stepX, rect.y - 3 + stepY - 5 + (y * stepY));
                    g.setColor(Color.black);
                }
            } //for
        }
        //g.drawString(st, 80, 65, Graphics.TOP | Graphics.LEFT);
    }

    public void print() {
        GameLogger.log("\nLetterBox:");
        for (int i = 0; i < counter; i++) {
            GameLogger.log(lbox[i] + "");
        }
        GameLogger.log("");
        GameLogger.log("\nSuccesses:");
        for (int i = 0; i < scounter; i++) {
            GameLogger.log((char) successes[i] + ",");
        }
        GameLogger.log("");
        GameLogger.log("\nFailures:");
        for (int i = 0; i < fcounter; i++) {
            GameLogger.log((char) failures[i] + ",");
        }
        GameLogger.log("");

    }

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -