package hangman; import java.awt.*; import javax.swing.*; import java.awt.geom.*; import java.awt.image.*; public class HangmanCanvas extends JPanel{ HangmanGame game = null; int topLevel = 10; int groundLevel = 0; int leftPole = 0; int braceSize = 0; int bodyCenter = 0; int legTop = 0; int legSize = 0; int bodyTop = 0; int armSize = 0; int headSize = 0; int neckSize = 0; int eyeSize = 0; Color[] colors = new Color[50]; int iColor = 0; BufferedImage offScreen = null; public void paint(Graphics _g){ if(offScreen == null || offScreen.getWidth() != getWidth() || offScreen.getHeight() != getHeight()){ offScreen = (BufferedImage)createImage(getWidth(), getHeight()); } Graphics2D graphics = (Graphics2D)offScreen.getGraphics(); topLevel = 15; groundLevel = getHeight() - 10; leftPole = 20; braceSize = 60; bodyCenter = getWidth()/2; legTop = (int)(getHeight()*0.6f); legSize = 120; bodyTop = (int)(getHeight()*0.3f); armSize = 100; headSize = 80; neckSize = 40; eyeSize = 15; //clear if(game.bWinner){ graphics.setColor(Color.white); } else if(game.mistakeCount > 9){ graphics.setColor(new Color(255, 195, 195)); } else{ graphics.setColor(new Color(195, 195, 195)); } graphics.fillRect(0, 0, getWidth(), getHeight()); iColor = 0; //ground if(game.bWinner){ drawWinner(graphics); } else{ drawHangman(graphics); } _g.drawImage(offScreen, 0, 0, this); } public void makeColors(){ for (int i = 0; i < colors.length; i++){ colors[i] = new Color((float)Math.random(), (float)Math.random(), (float)Math.random()); } } void randomColor(Graphics2D graphics){ graphics.setColor(colors[iColor++]); } void drawLine(Graphics2D graphics, int x0, int y0, int x1, int y1){ double angle = Math.atan2(y1 - y0, x1 - x0); int radius = 10; int nPoint = 24; int len = (int)Math.sqrt((x1 - x0)*(x1 - x0) + (y1 - y0)*(y1 - y0)); int[] xArray = new int[nPoint + 2]; int[] yArray = new int[nPoint + 2]; for (int i = 0; i < nPoint/2 + 1; i++){ double a = (i/(double)nPoint - 0.25)*3.14159*2.f; xArray[i] = (int)(Math.cos(a)*radius + len); yArray[i] = (int)(Math.sin(a)*radius); } for (int i = nPoint/2 + 1; i < nPoint + 2; i++){ double a = ((i - 1)/(double)nPoint - 0.25)*3.14159*2.f; xArray[i] = (int)(Math.cos(a)*radius); yArray[i] = (int)(Math.sin(a)*radius); } Polygon polygon = new Polygon(xArray, yArray, xArray.length); AffineTransform oldT = graphics.getTransform(); graphics.translate(x0, y0); graphics.rotate(angle); randomColor(graphics); graphics.fillPolygon(polygon); graphics.setTransform(oldT); } void drawHangman(Graphics2D graphics){ int mistakeCount = game.mistakeCount; //left pole if(mistakeCount > 0){ drawLine( graphics, leftPole, topLevel, leftPole, groundLevel); } //top if(mistakeCount > 1){ drawLine( graphics, leftPole, topLevel, bodyCenter + 40, topLevel); } //left brace if(mistakeCount > 2){ drawLine( graphics, leftPole, topLevel + braceSize, leftPole + braceSize, topLevel); } //left leg if(mistakeCount > 3){ drawLine( graphics, bodyCenter - legSize, legTop + legSize, bodyCenter, legTop); } //right leg if(mistakeCount > 4){ drawLine( graphics, bodyCenter + legSize, legTop + legSize, bodyCenter, legTop); } //body if(mistakeCount > 5){ drawLine( graphics, bodyCenter, bodyTop - neckSize, bodyCenter, legTop); } //left arm if(mistakeCount > 6){ drawLine( graphics, bodyCenter, bodyTop, bodyCenter - armSize, bodyTop + 10); drawLine( graphics, bodyCenter - armSize, bodyTop + 10, bodyCenter - armSize - 10, bodyTop); } //right arm if(mistakeCount > 7){ drawLine( graphics, bodyCenter, bodyTop, bodyCenter + armSize, bodyTop + 10); drawLine( graphics, bodyCenter + armSize, bodyTop + 10, bodyCenter + armSize + 10, bodyTop); } //head if(mistakeCount > 8){ randomColor(g); graphics.fillOval(bodyCenter - headSize/2, bodyTop - headSize - neckSize, headSize, headSize); } //rope if(mistakeCount > 9){ drawLine( graphics, bodyCenter, topLevel, bodyCenter, bodyTop - headSize - neckSize); } } void drawWinner(Graphics2D graphics){ graphics.translate(0, groundLevel - (legTop + legSize)); //legs, body drawLine( graphics, bodyCenter - legSize, legTop + legSize, bodyCenter, legTop); drawLine( graphics, bodyCenter + legSize, legTop + legSize, bodyCenter, legTop); drawLine( graphics, bodyCenter, bodyTop - neckSize, bodyCenter, legTop); //left arm drawLine( graphics, bodyCenter, bodyTop, bodyCenter - armSize, bodyTop + 10); drawLine( graphics, bodyCenter - armSize, bodyTop + 10, bodyCenter - armSize - 10, bodyTop - armSize); //right arm drawLine( graphics, bodyCenter, bodyTop, bodyCenter + armSize, bodyTop + 10); drawLine( graphics, bodyCenter + armSize, bodyTop + 10, bodyCenter + armSize + 10, bodyTop - armSize); //head graphics.fillOval(bodyCenter - headSize/2, bodyTop - headSize - neckSize, headSize, headSize); //face randomColor(graphics); graphics.fillOval(bodyCenter - eyeSize - 4, bodyTop - headSize/2 - neckSize - eyeSize, eyeSize, eyeSize); randomColor(graphics); graphics.fillOval(bodyCenter + 4, bodyTop - headSize/2 - neckSize - eyeSize, eyeSize, eyeSize); randomColor(graphics); graphics.drawArc( bodyCenter - eyeSize, bodyTop - headSize/2 - neckSize + eyeSize/2, eyeSize*3, eyeSize, -30, -150); } }