package hangman; import java.awt.*; import javax.swing.*; public class AnswerCanvas extends JPanel{ HangmanGame game = null; Font font = new Font("Courier", Font.BOLD, 24); FontMetrics fm = null; Font font2 = new Font("Courier", Font.PLAIN, 14); FontMetrics fm2 = null; public AnswerCanvas(){ fm = getFontMetrics(font); fm2 = getFontMetrics(font2); } public void paint(Graphics graphics){ if(game.partialWord == null){ return; } String partialWord = game.partialWord.toString(); int wordWidth = fm.stringWidth(partialWord); int x = (getWidth() - wordWidth)/2; int fontHeight = fm.getHeight(); int wordY = fontHeight + 10; graphics.setColor(new Color(127, 255, 127)); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setFont(font); graphics.setColor(Color.black); if(game.loadProgress != -1){ graphics.drawString("Loading, please wait. " + game.loadProgress + " words.", 10, wordY); return; } graphics.drawString(partialWord, x, wordY); int y = wordY + 10; int characterWidth = wordWidth/partialWord.length(); for (int i = 0; i < partialWord.length(); i++){ graphics.drawLine(x + 1, y, x + charWidth - 1, y); graphics.drawLine(x + 1, y + 1, x + charWidth - 1, y + 1); x+= characterWidth; } String definition = game.currentWord.definition; if(definition != null){ wordWidth = fm2.stringWidth(definition); x = (getWidth() - wordWidth)/2; fontHeight = fm2.getHeight(); graphics.setFont(font2); graphics.drawString(definition, x, wordY + fontHeight + 10); } } }