Java User-Interface Design
November/December 1996 Dr. Dobb's Sourcebook
by Jeffrey Kay
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import java.io.*;
// Code is provided "AS IS" for example purposes only, without warranty
public interface MCPaintable {
abstract void paint(Graphics g, Object o,int width, int inset);
}
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import java.io.*;
// Code provided "AS IS" for example purposes only, without warranty
public interface MCSortable {
abstract boolean lessthan(Object o);
abstract boolean greaterthan(Object o);
}
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.util.*;
import java.io.*;
// Code provided "AS IS" for example purposes only, without warranty
class MCImage extends Object implements MCPaintable {
Image image;
public MCImage(Image i) {
image = i;
}
public void paint(Graphics g, Object o,int width,int inset) {
if (g == null) return;
if (o instanceof ImageObserver) {
g.drawImage(image,inset,0,(ImageObserver)o);
}
}
public String toString() {
return "Cryptolope";
}
}
class TitleAbstract extends Object implements MCPaintable, MCSortable {
String Title = "";
String Abstract = "";
public TitleAbstract(String t, String a) {
Title = t;
Abstract = a;
}
public String toString() {
return Title + " " + Abstract;
}
public boolean lessthan(Object o) {
return toString().compareTo(o.toString()) < 0;
}
public boolean greaterthan(Object o) {
return toString().compareTo(o.toString()) > 0;
}
// void wrapString(Graphics g,String s,int w)
public void paint(Graphics g, Object o,int width, int inset) {
if (g == null) return;
width -= inset;
if (width <= 0) return; // hidden row
String temp = "";
FontMetrics fm = null;
Font f = g.getFont();
int col = 0; // x coordinate
int y = 0; // y coordinate
int lineheight = 0; // lineheight
Font newFont = new Font(f.getName(),Font.BOLD,f.getSize());
for(int i=0;i<2;i++) {
StringTokenizer st = null;
if (i == 0) {
g.setFont(newFont);
st = new StringTokenizer(Title);
fm = g.getFontMetrics();
y = fm.getMaxAscent();
}
else {
g.setFont(f);
fm = g.getFontMetrics();
st = new StringTokenizer(Abstract);
}
lineheight = fm.getMaxAscent() + fm.getMaxDescent();
while (st.hasMoreTokens()) {
String tok = st.nextToken();
if (temp == "") {
temp = tok;
}
else {
if (fm.stringWidth(temp + " " + tok) < width - col) {
temp += " " + tok;
}
else {
if ((col + fm.stringWidth(temp) > width) && (col != 0)) {
col = 0;
y += lineheight;
if (fm.stringWidth(temp + " " + tok) < width - col) {
g.drawString(temp,col,y);
col = 0;
y += lineheight;
temp = tok;
}
}
else {
g.drawString(temp,col+inset,y);
col = 0;
y += lineheight;
temp = tok;
}
}
}
}
if (temp != "") {
g.drawString(temp,col+inset,y);
col += fm.stringWidth(temp)+fm.stringWidth("XX");
temp = "";
if (col > width) {
col = 0;
y += lineheight;
}
}
}
}
}
End Listings
Copyright © 1996, Dr. Dobb's Journal