import java.awt.*; class BiorhythmCanvas extends Canvas { static final int width = 400; static final int height = 210; static final int gridWidth = 15; static final double amplitude = -70.0; static final int PHYSICAL_ORIGIN_X = 45; static final int SENSITIVITY_ORIGIN_X = 30; static final int INTELLECTUAL_ORIGIN_X = 15; static final int ORIGIN_Y = 110; private Image physicalImage, sensitivityImage, intellectualImage; private Color physicalColor, sensitivityColor, intellectualColor; private Color borderLineColor, backgroundColor, biorhythmGraphColor, stringColor; private Biorhythm yourBiorhythm; private Dimension physicalImageDimension, sensitivityImageDimension, intellectualImageDimension; private int[] physicalValue = {9, 10, 11, 11, 0, 0, 0, 1, 1, 1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8}; private int[] sensitivityValue = {11, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10}; private int[] intellectualValue = {1, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 7, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 0}; private Image offScreenImage; private Graphics offScreenGraphics; public BiorhythmCanvas(Biorhythm yourBiorhythm) { this.yourBiorhythm = yourBiorhythm; physicalImage = null; sensitivityImage = null; intellectualImage = null; physicalColor = Color.blue; sensitivityColor = Color.red; intellectualColor = Color.green; borderLineColor = Color.black; biorhythmGraphColor = Color.lightGray; backgroundColor = Color.white; stringColor = Color.black; resize(width, height); setBackground(backgroundColor); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (offScreenGraphics == null) { offScreenImage = createImage(size().width, size().height); offScreenGraphics = offScreenImage.getGraphics(); } offScreenGraphics.setColor(getBackground()); offScreenGraphics.fillRect(0, 0, size().width, size().height); drawCurves(offScreenGraphics); drawMarks(offScreenGraphics); drawStrings(offScreenGraphics); drawInstruction(offScreenGraphics); g.drawImage(offScreenImage, 0, 0, this); } private void drawCurves(Graphics g) { g.setColor(biorhythmGraphColor); g.fillRect(0, 35, width, 150); int oldX = -3 * gridWidth; int oldYPhysical = (int)(amplitude * Math.sin((double)(-3 - 18) * Math.PI / 12.0)); int oldYSensitivity = (int)(amplitude * Math.sin((double)(-3 - 22) * Math.PI / 12.0)); int oldYIntellectual = (int)(amplitude * Math.sin((double)(-3 - 2) * Math.PI / 12.0)); for (int i = -2; i < 27; i++) { int x = i * gridWidth; int yPhysical = (int)(amplitude * Math.sin((double)(i - 18) * Math.PI / 12.0)); int ySensitivity = (int)(amplitude * Math.sin((double)(i - 22) * Math.PI / 12.0)); int yIntellectual = (int)(amplitude * Math.sin((double)(i - 2) * Math.PI / 12.0)); g.setColor(physicalColor); g.drawLine(oldX + PHYSICAL_ORIGIN_X, oldYPhysical + ORIGIN_Y, x + PHYSICAL_ORIGIN_X, yPhysical + ORIGIN_Y); g.setColor(sensitivityColor); g.drawLine(oldX + SENSITIVITY_ORIGIN_X, oldYSensitivity + ORIGIN_Y, x + SENSITIVITY_ORIGIN_X, ySensitivity + ORIGIN_Y); g.setColor(intellectualColor); g.drawLine(oldX + INTELLECTUAL_ORIGIN_X, oldYIntellectual + ORIGIN_Y, x + INTELLECTUAL_ORIGIN_X, yIntellectual + ORIGIN_Y); oldX = x; oldYPhysical = yPhysical; oldYSensitivity = ySensitivity; oldYIntellectual = yIntellectual; } g.setColor(borderLineColor); g.drawLine(0, ORIGIN_Y, width, ORIGIN_Y); g.setColor(biorhythmGraphColor); g.fillRect(width, 35, gridWidth * 3, 150); } private void drawMarks(Graphics g) { int physicalPoint = physicalValue[yourBiorhythm.getPhysicalBiorhythm()] * 2; if (physicalImage != null) { g.drawImage(physicalImage, gridWidth * physicalPoint + PHYSICAL_ORIGIN_X - physicalImageDimension.width / 2, (int)(amplitude * Math.sin((double)(physicalPoint - 18) * Math.PI / 12.0)) + ORIGIN_Y - physicalImageDimension.height / 2, this); } else { g.setColor(physicalColor); g.fillRoundRect(gridWidth * physicalPoint + PHYSICAL_ORIGIN_X - 7, (int)(amplitude * Math.sin((double)(physicalPoint - 18) * Math.PI / 12.0)) + ORIGIN_Y - 7, 14, 14, 10, 10); } int sensitivityPoint = sensitivityValue[yourBiorhythm.getSensitivityBiorhythm()] * 2; if (sensitivityImage != null) { g.drawImage(sensitivityImage, gridWidth * sensitivityPoint + SENSITIVITY_ORIGIN_X - sensitivityImageDimension.width / 2, (int)(amplitude * Math.sin((double)(sensitivityPoint - 22) * Math.PI / 12.0)) + ORIGIN_Y - sensitivityImageDimension.height / 2, this); } else { g.setColor(sensitivityColor); g.fillRoundRect(gridWidth * sensitivityPoint + SENSITIVITY_ORIGIN_X - 7, (int)(amplitude * Math.sin((double)(sensitivityPoint - 22) * Math.PI / 12.0)) + ORIGIN_Y - 7, 14, 14, 10, 10); } int intellectualPoint = intellectualValue[yourBiorhythm.getIntellectualBiorhythm()] * 2; if (intellectualImage != null) { g.drawImage(intellectualImage, gridWidth * intellectualPoint + INTELLECTUAL_ORIGIN_X - intellectualImageDimension.width / 2, (int)(amplitude * Math.sin((double)(intellectualPoint - 2) * Math.PI / 12.0)) + ORIGIN_Y - intellectualImageDimension.height / 2, this); } else { g.setColor(intellectualColor); g.fillRoundRect(gridWidth * intellectualPoint + INTELLECTUAL_ORIGIN_X - 7, (int)(amplitude * Math.sin((double)(intellectualPoint - 2) * Math.PI / 12.0)) + ORIGIN_Y - 7, 14, 14, 10, 10); } } private void drawStrings(Graphics g) { g.setColor(stringColor); g.setFont(new Font("Courier", Font.PLAIN, 12)); g.drawString("Birthday : " + yourBiorhythm.getBirthdayYear() + "/" + yourBiorhythm.getBirthdayMonth() + "/" + yourBiorhythm.getBirthdayDate(), 0, 12); g.drawString("Calculate day : ", 135, 208); g.setFont(new Font("Dialog", Font.PLAIN, 24)); g.drawString(yourBiorhythm.getCalcYear() + "/" + yourBiorhythm.getCalcMonth() + "/" + yourBiorhythm.getCalcDate(), 245, 208); } private void drawInstruction(Graphics g) { g.setFont(new Font("Courier", Font.PLAIN, 12)); g.drawImage(physicalImage, 0, 15, this); g.setColor(physicalColor); g.drawString("PHYSICAL", 18, 29); g.drawImage(sensitivityImage, 110, 15, this); g.setColor(sensitivityColor); g.drawString("SENSITIVITY", 128, 29); g.drawImage(intellectualImage, 220, 15, this); g.setColor(intellectualColor); g.drawString("INTELLECTUAL", 238, 29); } public void setPhysicalColor(Color physicalColor) { this.physicalColor = physicalColor; } public void setSensitivityColor(Color sensitivityColor) { this.sensitivityColor = sensitivityColor; } public void setIntellectualColor(Color intellectualColor) { this.intellectualColor = intellectualColor; } public void setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; setBackground(backgroundColor); } public void setBorderLineColor(Color borderLineColor) { this.borderLineColor = borderLineColor; } public void setBiorhythmGraphColor(Color biorhythmGraphColor) { this.biorhythmGraphColor = biorhythmGraphColor; } public void setStringColor(Color stringColor) { this.stringColor = stringColor; } public void setPhysicalImage(Image physicalImage) { this.physicalImage = physicalImage; if (physicalImage != null) { physicalImageDimension = getImageDimension(physicalImage); } } public void setSensitivityImage(Image sensitivityImage) { this.sensitivityImage = sensitivityImage; if (sensitivityImage != null) { sensitivityImageDimension = getImageDimension(sensitivityImage); } } public void setIntellectualImage(Image intellectualImage) { this.intellectualImage = intellectualImage; if (intellectualImage != null) { intellectualImageDimension = getImageDimension(intellectualImage); } } Dimension getImageDimension(Image im) { int width; int height; while ((width = im.getWidth(this)) < 0) { try { Thread.sleep(100); } catch (InterruptedException e) { } } while ((height = im.getHeight(this)) < 0) { try { Thread.sleep(100); } catch (InterruptedException e) { } } return new Dimension(width, height); } }