import java.awt.*; import java.awt.color.*; import javax.swing.*; public class StickDraw extends JPanel { private int nbrToDraw; private final int NBRBODYPARTS = 10; public StickDraw() { init(); } public void init() { nbrToDraw = 0; repaint(); } public void add() { if (nbrToDraw < NBRBODYPARTS) { nbrToDraw++; repaint(); } } public boolean full() { return (nbrToDraw >= NBRBODYPARTS); } /* (non-Javadoc) * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) */ protected void paintComponent(Graphics g) { super.paintComponent(g); final int startx = 15; final int starty = 15; final int headsize = 40; g.drawLine(startx, getHeight(), startx, starty); g.drawLine(startx, starty, getWidth()/2, starty); g.drawLine(getWidth()/2, starty, getWidth()/2, startx+headsize/2); int figsDrawn = nbrToDraw; if (figsDrawn <= 0) return; int headbegin = getHeight()/5; g.fillOval((getWidth()-headsize)/2, headbegin, headsize, headsize); // draw head. 1 miss if (figsDrawn-- <=0) return; // draw body. 2 misses final int bodybegin = headbegin+headsize; final int bodyend = bodybegin+headsize*3/2; if (figsDrawn-- <=0) return; g.drawLine(getWidth()/2, bodybegin, getWidth()/2, bodyend); if (figsDrawn-- <= 0) return; // legs are next misses 3 and 4 g.drawLine(getWidth()/2, bodyend, getWidth()/4, getHeight()); if (figsDrawn-- <= 0) return; g.drawLine(getWidth()/2, bodyend, getWidth()/2+getWidth()/4, getHeight()); if (figsDrawn-- <= 0) return; // arms are misses 5 and 6 int armsplace = (bodyend-bodybegin)/2 + bodybegin; g.drawLine(getWidth()/2, armsplace, getWidth()/4, bodybegin); if (figsDrawn-- <= 0) return; g.drawLine(getWidth()/2, armsplace, getWidth()/4 + getWidth()/2, bodybegin); // feet are misses 7 and 8 if (figsDrawn-- <= 0) return; g.fillOval(getWidth()/4-headsize/4, getHeight()-headsize/4, headsize/2, headsize/4); if (figsDrawn-- <= 0) return; g.fillOval(getWidth()/2+getWidth()/4-headsize/4, getHeight()-headsize/4, headsize/2, headsize/4); // hands are misses 9 and 10 if (figsDrawn-- <= 0) return; g.fillOval(getWidth()/4-15, bodybegin-headsize/4, headsize/2, headsize/4); if (figsDrawn-- <=0) return; g.fillOval(getWidth()/4 + getWidth()/2-5, bodybegin-headsize/4, headsize/2, headsize/4); } }