// Program: Result display program // In its final shape result should be depicted by two snakes // Programmer: H K Gupta Date: 15 July 1997 //************************************************************************ import java.awt.*; import java.applet.Applet; public class HridayeshSnake extends Canvas { private boolean resultIn = false; private int totOne, totTwo; private Color clrOne, clrTwo; public void updateResult(Color clrOne, Color clrTwo, int totOne, int totTwo) { this.clrOne = clrOne; this.clrTwo = clrTwo; if(totOne == totTwo) { this.totOne = this.totTwo = 1; // avoid divide by 0 } else { this.totOne = totOne; this.totTwo = totTwo; } resultIn = true; repaint(); } public void clearSnake() { resultIn = false; repaint(); } public void update(Graphics g) { Dimension d = size(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); if(resultIn) { int percentOne = (100 * totOne) / (totOne + totTwo); drawSnake(g, clrOne, percentOne, 0); drawSnake(g, clrTwo, 100 - percentOne, (int)(d.width / 2)); } } private void drawSnake(Graphics g, Color clr, int percent, int xOff) { Dimension d = size(); int faceHeight = d.width / 2 - 4; int yOff = d.height + faceHeight - ((d.height - faceHeight) * percent) / 100; int y = ((d.height - faceHeight) * percent) / 100; g.setColor(clr); g.fillRect(xOff + d.width/4 - 4, yOff, 8, y); drawFace(percent, g, xOff + 2, yOff - faceHeight, faceHeight); } private void drawFace(int percent, Graphics g, int xOff, int yOff, int w) { Dimension d = size(); g.drawOval(xOff, yOff, w, w); g.fillOval(xOff + w / 5, yOff + w / 5, w / 5, w / 5); g.fillOval(xOff + (3 * w) / 5, yOff + w / 5, w / 5, w / 5); xOff += w / 5; yOff += (3 * w) / 5; if(percent > 50) // Happy face g.drawArc(xOff, yOff, (3 * w) / 5, w / 5, 180, 180); if(percent < 50) // Sad face g.drawArc(xOff, yOff, (3 * w) / 5, w / 5, 0, 180); if(percent == 50) g.fillRect(xOff, yOff, (3 * w) / 5, 2); } public void paint(Graphics g) { update(g); } }