001 package com.croftsoft.core.gui;
002
003 import com.croftsoft.core.lang.*;
004 import java.awt.*;
005 import java.util.*;
006
007 /*********************************************************************
008 * @version
009 * 1997-04-28
010 * @author
011 * <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
012 *********************************************************************/
013
014 public class TextCanvas extends Canvas {
015 //////////////////////////////////////////////////////////////////////
016 //////////////////////////////////////////////////////////////////////
017
018 private Vector line_Vector = new Vector ( );
019 private Vector color_Vector = new Vector ( );
020 private Dimension size;
021 private Graphics graphics;
022 private Image offscreenImage;
023 private Graphics offscreenGraphics;
024
025 private static final int TEXT_INSET_LEFT = 5;
026
027 //////////////////////////////////////////////////////////////////////
028 //////////////////////////////////////////////////////////////////////
029
030 public TextCanvas (
031 Color foreground, Color background, Font font ) {
032 //////////////////////////////////////////////////////////////////////
033 setForeground ( foreground );
034 setBackground ( background );
035 setFont ( font );
036 }
037 /*
038 public synchronized void dispose ( ) {
039 //////////////////////////////////////////////////////////////////////
040 if ( graphics != null ) graphics.dispose ( );
041 if ( offscreenGraphics != null ) offscreenGraphics.dispose ( );
042 }
043 */
044 public void write ( String line ) {
045 //////////////////////////////////////////////////////////////////////
046 write ( line, getForeground ( ) );
047 }
048
049 public void write ( String line, Color color ) {
050 //////////////////////////////////////////////////////////////////////
051 String [ ] lines = StringLib.toStringArray ( line );
052 synchronized ( this ) {
053 for ( int i = 0; i < lines.length; i++ ) {
054 line_Vector.addElement ( lines [ i ] );
055 color_Vector.addElement ( color );
056 }
057 }
058 update_offscreen ( );
059 repaint ( );
060 }
061
062 private synchronized void update_offscreen ( ) {
063 //////////////////////////////////////////////////////////////////////
064 if ( size == null ) return;
065 int font_height = getFontMetrics ( getFont ( ) ).getHeight ( );
066 int max_lines = size.height / font_height;
067 if ( max_lines < 1 ) max_lines = 1;
068 int line_count;
069 while ( ( line_count = line_Vector.size ( ) ) > max_lines ) {
070 line_Vector.removeElementAt ( 0 );
071 color_Vector.removeElementAt ( 0 );
072 }
073 if ( offscreenGraphics == null ) {
074 offscreenImage = createImage ( size.width, size.height );
075 if ( offscreenImage == null ) return;
076 offscreenGraphics = offscreenImage.getGraphics ( );
077 }
078 offscreenGraphics.setColor ( getBackground ( ) );
079 offscreenGraphics.fillRect ( 0, 0, size.width, size.height );
080 offscreenGraphics.setFont ( getFont ( ) );
081 for ( int i = 0; i < line_count; i++ ) {
082 offscreenGraphics.setColor (
083 ( Color ) color_Vector.elementAt ( i ) );
084 offscreenGraphics.drawString (
085 ( String ) line_Vector.elementAt ( i ),
086 TEXT_INSET_LEFT, font_height * ( i + 1 ) );
087 }
088 }
089
090 public void paint ( Graphics g ) {
091 //////////////////////////////////////////////////////////////////////
092 try {
093 if ( graphics == null ) graphics = getGraphics ( );
094 if ( graphics == null ) return;
095 if ( offscreenImage != null ) {
096 graphics.drawImage ( offscreenImage, 0, 0, this );
097 }
098 } catch ( Exception e ) { e.printStackTrace ( ); }
099 }
100
101 public synchronized void reshape (
102 int x, int y, int width, int height ) {
103 //////////////////////////////////////////////////////////////////////
104 try {
105 super.reshape ( x, y, width, height );
106 Dimension size = size ( );
107 if ( !size.equals ( this.size ) ) {
108 this.size = size;
109 if ( offscreenGraphics != null ) {
110 offscreenGraphics.dispose ( );
111 offscreenGraphics = null;
112 offscreenImage = null;
113 System.gc ( );
114 }
115 update_offscreen ( );
116 }
117 } catch ( Exception e ) { e.printStackTrace ( ); }
118 }
119
120 public void repaint ( ) {
121 //////////////////////////////////////////////////////////////////////
122 paint ( null );
123 }
124
125 public void update ( Graphics g ) {
126 //////////////////////////////////////////////////////////////////////
127 paint ( null );
128 }
129
130 //////////////////////////////////////////////////////////////////////
131 //////////////////////////////////////////////////////////////////////
132 }