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

📄 hangmanpanel.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.GameLogger;
import com.aztsoft.games.gfx.GButton;

import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

/**
 * The GUI of the game. 
 */
public class HangmanPanel extends Panel implements MouseListener {
    protected Font largeFont = new Font("Arial", Font.PLAIN, 36);
    protected Font smallFont = new Font("Arial", Font.PLAIN, 22);
    protected Font verysmallFont = new Font("Arial", Font.PLAIN, 14);
    HangmanGame hangmanGame;
    private Image[] gpImageArray;
    private Image gpHunged;
    private Dimension dimension;
    private int currentSelection;
    Graphics bufferGraphics;
    Image offscreen;
    GButton startGameButton;
    boolean playing = true;
    Thread animThread;
    long millisRunning;

    public HangmanPanel(Dimension dimen, HangmanGame hg, Image[] hanArray, Image hunIm) {
        setBackground(Color.black);
        dimension = dimen;
        hangmanGame = hg;
        gpImageArray = hanArray;
        gpHunged = hunIm;
        addMouseListener(this);
        startGameButton = new GButton((int) dimen.getWidth() / 2 - 35, (int) dimen.getHeight() / 2 + 120, 80, 28, "start again");

    }

    public Dimension getPreferredSize() {
        return dimension;
    }

    public void paintNetworkImage(Graphics g) {
        hangmanGame.getImageBox().paint(g, getSize(), this);
    }




    public void startGame() {
        offscreen = createImage(dimension.width, dimension.height);
        bufferGraphics = offscreen.getGraphics();
        playing = true;
        animThread = new Thread() {
            public void run() {
                millisRunning = 0;
                while (playing) {
                    long millisStarting = System.currentTimeMillis();
                    pause(10);
                    repaint();
                    long millisEnding = System.currentTimeMillis();
                    millisRunning = millisRunning + (millisEnding - millisStarting);
                }
            }
        };
        animThread.start();
    }

    public void stop() {
        playing = false;
        animThread = null;
    }


    public void paint(Graphics g) {
    if (bufferGraphics != null) {
            paintBack(bufferGraphics);
            paintKeyword(bufferGraphics);
            paintGallow(bufferGraphics);
          try {
                paintNetworkImage(bufferGraphics);
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            if (hangmanGame.isGameOver()) {
                paintGameOver(bufferGraphics);
            } else {
                hangmanGame.getLetterBox().paint(bufferGraphics);
            }
        }
        if (g != null) {
            g.drawImage(offscreen, 0, 0, this);
        }

    }

    private void paintGameOver(Graphics g) {

        int x_center = getWidth() / 2;
        int y_center = getHeight() / 2 + getHeight() / 10;
        FontMetrics fmS = g.getFontMetrics(smallFont);
        FontMetrics fmL = g.getFontMetrics(largeFont);
        if (hangmanGame.getHiddenKeyword().found()) { //won
            g.setFont(largeFont);
            g.setColor(Color.yellow);
            String st = "Congratulations";
            int width = fmL.stringWidth(st);

            g.drawString(st, x_center - width / 2, y_center);
            g.setFont(smallFont);
            st = "Score: " + hangmanGame.getTriesNumberLeft() + "/" + hangmanGame.kMAX_TRIES;
            width = fmS.stringWidth(st);
            g.drawString(st, x_center - width / 2, y_center + 50);


        } else {
            g.setFont(largeFont);
            g.setColor(new Color(0xCC0000)); //red opaque
            String st = "Game Over";
            int width = fmL.stringWidth(st);
            g.drawString(st, x_center - width / 2, y_center);
            st = hangmanGame.getHiddenKeyword().getHiddenValue();
            g.setFont(smallFont);
            width = fmS.stringWidth(st);
            g.setColor(Color.white);
            g.drawString(st, x_center - width / 2, y_center + 40);
        }
        startGameButton.paint(g);
    }

    private void paintGallow(Graphics g) {

        FontMetrics fm = g.getFontMetrics(smallFont);
        if (gpImageArray[hangmanGame.getTriesNumberLeft()] != null) {
            if (hangmanGame.getHiddenKeyword() != null) {
                if (hangmanGame.getHiddenKeyword().found()) {
                    g.drawImage(gpImageArray[hangmanGame.kMAX_TRIES], getWidth() / 2 - 48, 12, 96, 189, this);
                } else {
                    g.drawImage(gpImageArray[hangmanGame.getTriesNumberLeft()], getWidth() / 2 - 48, 12, 96, 189, this);
                }
            }
        }
        g.setColor(Color.white);
        g.setFont(smallFont);
        g.drawString(hangmanGame.kMAX_TRIES - hangmanGame.getTriesNumberLeft() + " / " + hangmanGame.kMAX_TRIES, getWidth() / 2 + 25, 35);
        g.setColor(new Color(0x00CC00)); //green opaque
        String fullString = hangmanGame.getGamesWon() + " / " + hangmanGame.getGamesLost();
        int width = fm.stringWidth(fullString);
        int width2 = fm.stringWidth(hangmanGame.getGamesWon() + " / ");
        int width3 = fm.stringWidth(hangmanGame.getGamesWon() + " ");
        int leftOffset = getWidth() - width - 25;
        g.drawString(hangmanGame.getGamesWon() + " ", leftOffset, 35);
        g.setColor(Color.white);
        g.drawString("/ ", leftOffset + width3, 35);
        g.setColor(Color.red);
        g.drawString(hangmanGame.getGamesLost() + "", leftOffset + width2, 35);


    }


    private void paintKeyword(Graphics g) {

        FontMetrics fm = g.getFontMetrics(largeFont);

        if (hangmanGame.getHiddenKeyword() != null) {
            String st = hangmanGame.getHiddenKeyword().getPrintValue().toString();
            g.setColor(Color.white);
            Font tempFont = g.getFont();
            g.setFont(largeFont);
            int width = fm.stringWidth(st);
            g.drawString(st, getWidth() / 2 - width / 2, getHeight() / 2);
            g.setFont(tempFont);

        }

    }

    private void paintBack(Graphics grBuffer) {
        if (grBuffer != null) {
            FontMetrics fmS = grBuffer.getFontMetrics(verysmallFont);

            grBuffer.setColor(Color.black);
            grBuffer.fillRect(0, 0, getWidth(), getHeight());
            grBuffer.setColor(Color.gray);
            grBuffer.drawRect(10, 10, getWidth() - 20, getHeight() - 20);
            grBuffer.setColor(Color.gray);
            grBuffer.setFont(verysmallFont);
            String st = "Copyright'2006 www.thanassis.com";
            int width = fmS.stringWidth(st);
            grBuffer.drawString(st, getWidth() / 2 - width / 2, getHeight() - 30);
        }
    }

    public void processHit(int letterSel) {
        char ch = (char) letterSel;
        if (hangmanGame.getLetterBox().contains(ch)) {
            GameLogger.log("Try again, you've already used letter " + ch);
        } else if (hangmanGame.getHiddenKeyword().guess(ch)) {
            hangmanGame.getLetterBox().addToSuccesses(ch);
            GameLogger.log("Success, you have found letter " + ch);

        } else {
            hangmanGame.getLetterBox().addToFailures(ch);
            hangmanGame.decreaseTriesLeft();

        }
        if (hangmanGame.isGameOver()) {
            GameLogger.log("GAME OVER");
            hangmanGame.gameOver();
        }
        if (hangmanGame.getHiddenKeyword().found()) {
            hangmanGame.setGameOver(true);
            hangmanGame.congratulations();
            GameLogger.log("Congratulations");
        }
    }


    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }


    public void mousePressed(MouseEvent e) {
        if (!hangmanGame.isGameOver()) {
            int result = -1;
            if ((result = hangmanGame.getLetterBox().isHit(e.getX(), e.getY())) > 0) {
                currentSelection = result;
                hangmanGame.getLetterBox().select(result);
            }

        } else {
            if (startGameButton.isHit(e.getX(), e.getY())) {
                startGameButton.setStatus(GButton.kSTATUS_FOCUSED);
            }
        }
    }

    public void mouseReleased(MouseEvent e) {
        if (!hangmanGame.isGameOver()) {
            int result = -1;
            if ((result = hangmanGame.getLetterBox().isHit(e.getX(), e.getY())) > 0) {
              if (result == currentSelection) {
                    processHit(result);
                }
                hangmanGame.getLetterBox().unselect();
            }
             hangmanGame.getLetterBox().print();
            GameLogger.log("hV:" + hangmanGame.getHiddenKeyword().getHiddenValue());
        } else {
           if (startGameButton.isHit(e.getX(), e.getY())) {
                startGameButton.setStatus(GButton.kSTATUS_NORMAL);
                hangmanGame.startGame();
            }

        }
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void pause(int mil) {
        try {
            Thread.currentThread().sleep(mil);
        } catch (Exception ex) {
        }
    }

}

⌨️ 快捷键说明

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