import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Shape;
public class HangMan {
private int width = 0;
private int height = 0;
private int margin = 2;
private Color background = Color.darkGray;
private Color foreground = Color.WHITE;
private int errorState = 0;
public HangMan(int widht, int height) {
this.width = widht;
this.height = height;
}
public void draw(Graphics g, int x, int y) {
Shape clipOld = g.getClip();
Color colorOld = g.getColor();
g.setClip(x, y, width, height);
g.setColor(background);
g.fillRect(x, y, width, height);
g.setColor(foreground);
g.drawRect(x, y, width - 1, height - 1);
drawGallow(g, x, y);
drawMan(g, x, y);
g.setColor(colorOld);
g.setClip(clipOld);
}
private void drawGallow(Graphics g, int x, int y) {
int sizeWood = getSizeWood();
int widthCrossGrider = getWithCrossGider();
int heightGallow = getHeigthGallow();
int xp = getGallowOriginX(x);
int yp = getGallowOriginY(y);
int xx[] = { xp, xp + widthCrossGrider, xp + widthCrossGrider, xp + sizeWood, xp + sizeWood, xp + 4 * sizeWood,
xp + 4 * sizeWood, xp - 1 * sizeWood, xp - 1 * sizeWood, xp, xp };
int yy[] = { yp, yp, yp + sizeWood, yp + sizeWood, yp + sizeWood + heightGallow, yp + sizeWood + heightGallow,
yp + 2 * sizeWood + heightGallow, yp + 2 * sizeWood + heightGallow, yp + sizeWood + heightGallow,
yp + sizeWood + heightGallow, yp };
g.fillPolygon(new Polygon(xx, yy, xx.length));
}
private void drawMan(Graphics g, int x, int y) {
int ropeLenght = getRopeLenght();
int xp = getGallowOriginX(x) + getWithCrossGider();
int yp = getGallowOriginY(y) + getSizeWood();
if (errorState > 0)
g.drawLine(xp - 1, yp, xp, yp + ropeLenght);
if (errorState > 1)
g.drawOval(xp - ropeLenght / 2, yp + ropeLenght, ropeLenght, ropeLenght); // head
if (errorState > 2)
g.drawLine(xp, yp + 2 * ropeLenght, xp, yp + 3 * ropeLenght); // body
if (errorState > 3)// left leg
g.drawLine(xp, yp + 3 * ropeLenght, xp - ropeLenght / 2, yp + 4 * ropeLenght);
if (errorState > 4)// right leg
g.drawLine(xp, yp + 3 * ropeLenght, xp + ropeLenght / 2, yp + 4 * ropeLenght);
if (errorState > 5)// left arm
g.drawLine(xp, yp + (int) (2.5 * ropeLenght), xp - (int) (ropeLenght * .8), yp + (int) (2.7 * ropeLenght));
if (errorState > 6)// left arm
g.drawLine(xp, yp + (int) (2.5 * ropeLenght), xp + (int) (ropeLenght * .8), yp + (int) (2.7 * ropeLenght));
// ... do rest of errors here same way
}
public int getErrorState() {
return errorState;
}
private int getGallowOriginX(int x) {
return margin + x + 2 * getSizeWood();
}
private int getGallowOriginY(int y) {
return margin + y + getHeigthGallow()/4;
}
private int getHeigthGallow() {
return (int) (height * 2 / 3.0);
}
private int getRopeLenght() {
return getHeigthGallow() / 5;
}
private int getSizeWood() {
return getWithBase() / 6;
}
private int getWithBase() {
return (int) (width * .25);
}
private int getWithCrossGider() {
return getWithBase() * 2;
}
public void setBackground(Color background) {
this.background = background;
}
public void setForeground(Color foreground) {
this.foreground = foreground;
}
public void setErrorState(int errorState) {
this.errorState = errorState;
}
}