package polarDust.calculator;
/*
* Copyright (c) 1996 Sorin Lazareanu, All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies.
*
*/
import java.awt.*;
/**
* Printer01 is a component which shows an array of doubles converted
* by a display component into a certain format plus a binary operator
* representation. The length of the array is 100.
*
*
* The source code.
*
* @version 0.9, 1996.06.04
* @author Sorin Lazareanu
*/
public class Printer01
extends Canvas
implements CalcConstants {
static final int PRN_MAX_LINES = 100;
String lines[] = new String[ PRN_MAX_LINES ];
String text = "", op = "";
/**
* Constructs a printer.
*/
public Printer01() {
super();
int i;
for (i = PRN_MAX_LINES - 1; i >= 0; i--) {
lines[ i ] = "";
}
lines[ 0 ] = " ";
}
/**
* Paints the component. Uses update.
* @see Printer01.update
*/
public void paint(Graphics aGraphics) {
update(aGraphics);
}
/**
* Paints the component uses double buffering.
*
* The source code.
*/
public synchronized void update(Graphics aGraphics) {
int iStringWidth, iFontNewSize, i;
int iMaxWidth = size().width - 2;
int iFontHeight;
String aLine = new String(" ");
Dimension aDimension = size();
FontMetrics currentFontMetrics;
Dimension bufferDimension = new Dimension(0, 0);
Image bufferImage;
Graphics bufferGraphics;
bufferDimension = size();
bufferImage = createImage(bufferDimension.width, bufferDimension.height);
bufferGraphics = bufferImage.getGraphics();
bufferGraphics.setColor(new Color(255, 255, 210));
bufferGraphics.fillRect(0, 0, aDimension.width, aDimension.height);
bufferGraphics.setColor(Color.red);
bufferGraphics.drawRect(0, 0, aDimension.width - 1, aDimension.height - 1);
bufferGraphics.setFont(new Font(COURIER, Font.BOLD, 12));
iStringWidth = bufferGraphics.getFontMetrics().stringWidth(aLine);
if (iStringWidth != 0) {
iFontNewSize = bufferGraphics.getFont().getSize() *
iMaxWidth / iStringWidth + 5;
do { // we must loop here since the font size is not a precise defined measure.
iFontNewSize--;
bufferGraphics.setFont(new Font(COURIER, Font.BOLD, iFontNewSize));
currentFontMetrics = bufferGraphics.getFontMetrics();
iStringWidth = currentFontMetrics.stringWidth(aLine);
iFontHeight = currentFontMetrics.getHeight();
} while (iStringWidth > iMaxWidth);
for (i = PRN_MAX_LINES - 1; i >= 0; i--) {
if (aDimension.height - iFontHeight * (i + 1) > 0) {
bufferGraphics.setColor(Color.blue);
bufferGraphics.drawLine(
0,
bufferDimension.height - iFontHeight * (i + 1) + 2,
bufferDimension.width,
bufferDimension.height - iFontHeight * (i + 1) + 2
);
bufferGraphics.setColor(Color.black);
bufferGraphics.drawString(lines[ i ], 3, aDimension.height - iFontHeight * i - 2 );
}
}
aGraphics.drawImage(bufferImage, 0, 0, this);
}
}
public Dimension preferredSize() {
return new Dimension(155, 264);
}
public Dimension minimumSize() {
return preferredSize();
}
/**
* Advances the "paper tape" one row and prints a number.
*/
public void showResult(String aResult) {
int i;
for (i = PRN_MAX_LINES - 1; i > 0; i--) {
lines[ i ] = lines [ i - 1 ];
}
lines[ 0 ] = aResult + " ";
repaint();
}
/**
* Prints an operator.
*/
void showOp(String anOperator) {
lines[ 0 ] = lines[ 0 ] + anOperator;
repaint();
}
}