Screen.java
Screen.java · 8.9 KB
From the archive palrichclient.zip
Download the archive « Back to the archive contents
/*
Java Mud Client -- Screen module
by Michael Barton <red@smileyface.com>
Originally written for Divine Blood, telnet://shell.tetradine.com:4000
Permission is granted to modify and distribute freely.
Credit/links are appreciated, but not necessary.
Bugs and other issues can be noted to red@smileyface.com.
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Screen extends Panel implements AdjustmentListener
{
boolean reversed = false;
Scrollbar bar = null;
FontMetrics fm = null;
int fontHeight = 0;
Vector lines;
Image buf = null;
Graphics bufferGraphics = null;
String memory = "";
int leftSpace = 5;
Font font = null;
Color screenColor = Color.black;
Color defaultTextColor = Color.white;
boolean wrap = true;
boolean colors = true;
BlinkThread bt = new BlinkThread(this);
boolean blink = true;
int scroll = 0;
public Screen()
{
bar = new Scrollbar(1);
bar.addAdjustmentListener(this);
add(bar);
font = new Font("Monospaced", Font.PLAIN, 13);
fm = getFontMetrics(font);
fontHeight = fm.getMaxAscent()+2;
lines = new Vector();
newLine("");
}
public final void setBackground(Color c)
{
screenColor = c;
repaint();
}
public final void setTextColor(Color c)
{
defaultTextColor = c;
repaint();
}
public final void reverse()
{
reversed = !reversed;
repaint();
}
public final void color(boolean tf)
{
colors = tf;
repaint();
}
public final void pageUp()
{
if ((bar.getValue()-bar.getBlockIncrement()) > bar.getMinimum())
bar.setValue(bar.getValue()-bar.getBlockIncrement());
else
bar.setValue(bar.getMinimum());
repaint();
}
public final void pageDown()
{
if ((bar.getValue()+bar.getBlockIncrement()) < bar.getMaximum())
bar.setValue(bar.getValue()+bar.getBlockIncrement());
else
bar.setValue(bar.getMaximum());
repaint();
}
public final void clearScreen()
{
lines.removeAllElements();
repaint();
}
public final void concat(String str)
{
if (lines.size() > 0)
lines.removeElementAt(lines.size()-1);
if (str.indexOf("\033[J") > -1)
{
clearScreen();
memory = "";
str = str.substring(str.lastIndexOf("\033[J")+3);
}
while (str.indexOf("\n") > -1)
{
newLine(memory.concat(str.substring(0, str.indexOf("\n"))));
memory = "";
if (str.length() > (str.indexOf("\n")+1))
str = str.substring(str.indexOf("\n")+1);
else str = "";
}
memory = memory.concat(str);
newLine(memory);
repaint();
}
public final void update(Graphics g)
{paint(g);}
public final void setFont(Font f)
{
font = f;
fm = getFontMetrics(font);
if (buf != null)
bufferGraphics.setFont(font);
repaint();
}
public final void paint(Graphics g)
{
Color c = defaultTextColor;
Color bc = screenColor;
Color uc = screenColor;
int r, nextToken = 0, endToken = 0, width = 0, height = 0;
int printFrom = bar.getValue() + scroll;
String str = "";
if (buf == null)
{
buf = createImage(size().width, size().height);
bufferGraphics = buf.getGraphics();
bufferGraphics.setFont(font);
}
bufferGraphics.setColor(screenColor);
bufferGraphics.fillRect(0,0,size().width-bar.getSize().width, size().height);
if (!colors) for (r = printFrom-scroll; r < printFrom; r++)
{
width = leftSpace;
height = size().height - fontHeight*(printFrom-r);
bufferGraphics.drawString(lineAt(r).getText(), width, height);
}
else for (r = printFrom-scroll; r < printFrom; r++)
{
ScreenLine at = lineAt(r);
str = at.getText();
nextToken = str.indexOf("\033");
width = leftSpace;
height = size().height - fontHeight*(printFrom-r);
c = lineAt(r-1).endColor;
bc = lineAt(r-1).endBGColor;
while (nextToken > -1)
{
if (!bc.equals(screenColor))
{
bufferGraphics.setColor(bc);
bufferGraphics.fillRect(width, height-fm.getMaxAscent()+2, fm.stringWidth(str.substring(0, nextToken)), fm.getMaxAscent());
}
if (!uc.equals(screenColor))
{
bufferGraphics.setColor(bc);
bufferGraphics.drawLine(width, height, fm.stringWidth(str.substring(0, nextToken)), fm.getMaxAscent());
}
bufferGraphics.setColor(c);
bufferGraphics.drawString(str.substring(0, nextToken), width, height);
width += fm.stringWidth(str.substring(0, nextToken));
str = str.substring(nextToken+1);
for (endToken = 0; endToken < str.length(); endToken++)
if (str.charAt(endToken) <= 'z' && str.charAt(endToken) >= 'a')
break;
switch(str.charAt(endToken-2)*10+str.charAt(endToken-1))
{
case '3'*10+'0': c = Color.gray; break;
case '3'*10+'1': c = Color.red; break;
case '3'*10+'2': c = Color.green; break;
case '3'*10+'3': c = Color.yellow; break;
case '3'*10+'4': c = Color.blue; break;
case '3'*10+'5': c = Color.magenta;break;
case '3'*10+'6': c = Color.cyan; break;
case '3'*10+'7': c = Color.white; break;
case '4'*10+'0': bc = Color.gray; break;
case '4'*10+'1': bc = Color.red; break;
case '4'*10+'2': bc = Color.green; break;
case '4'*10+'3': bc = Color.yellow; break;
case '4'*10+'4': bc = Color.blue; break;
case '4'*10+'5': bc = Color.magenta;break;
case '4'*10+'6': bc = Color.cyan; break;
case '4'*10+'7': bc = Color.white; break;
default: c = defaultTextColor; bc = screenColor; uc = screenColor; break;
}
if (str.startsWith("[0m"));
switch(str.charAt(1))
{
default:
case '0':
c = (reversed)?c.brighter():c.darker();
break;
case '1':
c = (reversed)?c.darker():c.brighter();
break;
case '4':
uc = c;
break;
case '5':
c = (bt.blink)?c:screenColor;
break;
}
str = str.substring(endToken+1);
nextToken = str.indexOf("\033");
}
if (!bc.equals(screenColor))
{
bufferGraphics.setColor(bc);
bufferGraphics.fillRect(width, height-fm.getMaxAscent()+2, fm.stringWidth(str), fm.getMaxAscent());
}
bufferGraphics.setColor(c);
bufferGraphics.drawString(str, width, height);
at.endColor = c;
at.endBGColor = bc;
}
g.drawImage(buf, 0, 0, this);
}
private ScreenLine lineAt(int i)
{
if (i < 0 || i >= lines.size())
return (new ScreenLine(""));
return ((ScreenLine)(lines.elementAt(i)));
}
private final void newLine(String str)
{
if (str.startsWith("\r"))
str = str.substring(1);
if (wrap && (stringWidthNC(str) > (size().width-(bar.size().width+2*leftSpace))))
wrapLine(str);
else
addLine(str);
if (lines.size() > 1500)
lines.removeElementAt(0);
}
private final void addLine(String str)
{
lines.addElement(new ScreenLine(str));
if (bar.getValue() < (lines.size()-scroll)-3)
bar.setValues(bar.getValue(), scroll, 0, lines.size());
else
bar.setValues(lines.size()-scroll, scroll, 0, lines.size());
}
private final void wrapLine(String str)
{
byte bytes[] = str.getBytes();
int i;
int screenWidth = size().width - (bar.size().width + 2*leftSpace);
for (i = bytes.length-1; i > 0; i--)
{
if (bytes[i] == ' ')
if (stringWidthNC(new String(bytes, 0, i)) <= screenWidth)
{
newLine(str.substring(0, i));
newLine(str.substring(i+1));
return;
}
}
addLine(str);
}
private final int stringWidthNC(String strA)
{
byte bytes[] = strA.getBytes();
int i = 0, j = 0;
boolean inAnsi = false;
for (i = 0; i < bytes.length; i++)
{
if (bytes[i] == '\033')
inAnsi = true;
else if (!inAnsi)
j += fm.charWidth(bytes[i]);
else if (bytes[i] == 'm')
inAnsi = false;
}
return j;
}
public final synchronized void adjustmentValueChanged(AdjustmentEvent evt)
{
repaint();
}
public void doLayout()
{
if (buf != null)
{
bufferGraphics.dispose();
bufferGraphics = null;
buf = null;
}
scroll = size().height/fontHeight-1;
bar.setBounds(size().width-bar.getPreferredSize().width, 0, bar.getPreferredSize().width, size().height);
bar.setValues(lines.size()-scroll, scroll, 0, lines.size());
repaint();
}
}