//The design of this program was an inspiration from an example in
Sun's Java toutorial.
package misc;
import java.awt.*;
import java.applet.*;
import algebra.*;
//This class defines the canvas.
class Mycanvas extends Canvas {
- int tx = 0;
- int ty = 0;
- int text_width = 0;
- int text_height = 0;
- Dimension D;
- int numans = 1;
- //The array that holds various trials.
- answer ans[] = new answer[100];
- public Mycanvas(int width, int height) {
- super();
- ans[0] = new answer();
- ans[1] = new answer();
- D = new Dimension(width,height);
- }
- public Dimension minimumSize() { return D; }
- public Dimension preferredSize() { return minimumSize(); }
- public void newanswer() {
- ++numans;
- ans[numans] = new answer();
- }
- public void newline(entity ent) { ans[numans].add_line(ent); }
- //The clear operation is easy because of the garbage collection
- //in java. All we need to do is to set them to null!!
- public void clear() {
- for(int i = 0; i < numans; i++) {
- }
- ans[0] = new answer();
- ans[1] = new answer();
- numans = 1;
- repaint();
- text_width = 0;
- text_height = 0;
- }
- public void paint(Graphics g) {
- //The width and height of the canvas.
- int w = size().width;
- int h = size().height;
- //Boundary of the canvas is drawn
- g.drawRect(0,0,w-1,h-1);
- g.translate(-tx,-ty);
- //The text begins from 30 units down.
- int y = 30;
- integer Y = new integer(y);
- //This loops through each trial in the array.
- for(int a = 1; a <= numans ; a++) {
- int x = 30;
- int width = 30;
- int height = 0;
- integer W = new integer(width);
- integer H = new integer(height);
- integer X = new integer(x);
- String heading = "Trial #: " + String.valueOf( a );
- Color Col = g.getColor();
- g.setColor( Color.blue );
- g.drawString( heading, x, Y.intValue() );
- FontMetrics fm = g.getFontMetrics();
- if( height < fm.getHeight() )
- H.setValue( fm.getHeight() );
- Y.setValue( Y.intValue() + H.intValue() );
- g.setColor( Col );
- //Loops through the lines in each answer.
- for(int i = 0; i < (ans[a].noflines()); i++) {
- W.setValue(30);
- H.setValue(0);
- X.setValue(x);
- //The object is instantiated as a Drawable(interface).
- Drawable D = ans[a].get_line(i);
- D.draw(g, W, H, X, Y);
- if (text_width <= W.intValue() )
- text_width = W.intValue() + 20;
- }// end of lines loop
- if( a != numans ) Y.setValue( Y.intValue() + 20 );
- text_height = Y.intValue();
- }// end of the answer loop
- return;
- }
- public void paint( entity ent) {
- newline(ent);
- repaint();
- repaint();
- return;
- }
}
//This canvas has the Mycanvas defined above and positions
//it with the scrollbars.
public class ScrollableCanvas extends Panel {
- Scrollbar vert;
- Scrollbar horz;
- Mycanvas canvas;
- public ScrollableCanvas(int w, int h) {
- canvas = new Mycanvas(w,h);
- canvas.resize(canvas.minimumSize());
- canvas.setBackground(this.getBackground() );
- //Create horizontal scrollbar.
- horz = new Scrollbar(Scrollbar.HORIZONTAL);
- //Create vertical scrollbar.
- vert = new Scrollbar(Scrollbar.VERTICAL);
- //Add Components to the
- Applet. setLayout(new BorderLayout());
- add("Center",canvas);
- add("East",vert);
- add("South",horz);
- resizeHorz();
- resizeVert();
- validate();
- }
- public void paint(entity ent) { //if (ent.equals(new sentence())){
- canvas.paint(ent);
- resizeHorz();
- resizeVert();
- }
- public void newanswer() {
- canvas.newanswer();
- resizeHorz();
- resizeVert();
- }
- public void clear() {
- canvas.clear();
- resizeHorz();
- resizeVert();
- }
- public boolean handleEvent(Event evt) {
- switch (evt.id) {
- case Event.SCROLL_LINE_UP:
- case Event.SCROLL_LINE_DOWN:
- case Event.SCROLL_PAGE_UP:
- case Event.SCROLL_PAGE_DOWN:
- case Event.SCROLL_ABSOLUTE:
- if (evt.target == vert) {
- canvas.ty = ((Integer)evt.arg).intValue();
- resizeVert();
- canvas.repaint();
- canvas.repaint();
- }
- if (evt.target == horz) {
- canvas.tx = ((Integer)evt.arg).intValue();
- resizeHorz();
- canvas.repaint();
- canvas.repaint();
- }
- default:
- resizeVert();
- resizeHorz();
- }
- return super.handleEvent(evt);
- }
- //Don't call this until the canvas size is valid.
- void resizeHorz() {
- int canvasWidth = canvas.size().width;
- int tw = canvas.text_width;
- if ((canvas.tx + canvasWidth) > tw) {
- int newtx = tw - canvasWidth;
- if (newtx < 0) { newtx = 0; }
- canvas.tx = newtx;
- }
- horz.setValues(
- //draw the part of the image that starts at this x:
- canvas.tx,
- //amount to scroll for a "page":
- (int)(canvasWidth * 0.9),
- //minimum image x to specify:
- 0,
- //maximum image x to specify:
- tw - canvasWidth);
- //"visible" arg to setValues() has
- //no effect after scrollbar is visible.
- horz.setPageIncrement((int)(canvasWidth * 0.9));
- return;
- }
- //Don't call this until the canvas size is valid.
- void resizeVert() {
- int canvasHeight = canvas.size().height;
- int th = canvas.text_height;
- if (canvasHeight <= 0) {
- System.out.println("Canvas has no height" + " can't
resize scrollbar"); return;
- }
- //Shift everything downward if we're displaying empty space
- //on the bottom.
- if ((canvas.ty + canvasHeight) > th) {
- int newty = th - canvasHeight;
- if (newty < 0) {
- }
- canvas.ty = newty;
- }
- vert.setValues(
- //initially draw part of image starting at this y:
- canvas.ty,
- //visible arg--amount to scroll for a "page":
- (int)(canvasHeight * 0.9),
- //minimum image y to specify:
- 0,
- //maximum image y to specify:
- th - canvasHeight);
- //"visible" arg to setValues() has
- //no effect after scrollbar is visible.
- vert.setPageIncrement((int)(canvasHeight * 0.9));
- return;
- }
- public void paint(Graphics g) {
- //This method probably was called due to applet being resized.
- resizeHorz();
- resizeVert();
- return;
- }
- }
- back.