Mini-AWT

Download from http://www.nyangau.org/miniawt/miniawt.zip.

Why Mini-AWT

Environments exist in which the services of AWT are unavailable.

An example is when running on a UNIX server, with no suitable XWindows server running locally (as is common on business servers in datacentres). Modifying the DISPLAY environment variable does not necessarily constitute a 'solution' as we may be unable to identify a suitable remote server running XWindows.

Quite a few uses of AWT are for basic drawing and graphing needs, with the resulting java.awt.Image serialised into GIF or PNG form, and sent to file or client browser.

What does Mini-AWT provide

Mini-AWT provides replacements for a bare skeleton of java.awt.* classes, which may be accessed using an alternative import statement.

The drawing 'hardware' has 6 shades of red, green and blue, with intensity values 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff, giving 6x6x6=216 distinct colors, which are stored internally in one byte per pixel form.

The following very minimal subset of AWT is provided :-

public class Frame
  {
  public void addNotify();
  public Image createImage(int w, int h);
  }

public class Color
  {
  public static final Color black    ;
  public static final Color blue     ;
  public static final Color green    ;
  public static final Color cyan     ;
  public static final Color red      ;
  public static final Color magenta  ;
  public static final Color yellow   ;
  public static final Color white    ;
  public static final Color darkGray ;
  public static final Color gray     ;
  public static final Color lightGray;
  public static final Color orange   ;
  public static final Color pink     ;
  protected int r, g, b;
  public Color(int r, int g, int b);
  public int getRed  ();
  public int getGreen();
  public int getBlue ();
  }

public class Image
  {
  public Graphics getGraphics();
  }

public class Font
  {
  public static final int PLAIN;
  public static final int BOLD;
  public static final int ITALIC;
  public Font(String face, int style, int pointsize);
  public int getSize();
  public int getStyle();
  }

public class FontMetrics
  {
  public Font getFont();
  public int getAscent();
  public int getMaxAscent();
  public int getDescent();
  public int getMaxDescent();
  public int getLeading();
  public int getHeight();
  public int charWidth(char c);
  public int charWidth(int c);
  public int bytesWidth(byte[] a, int off, int len);
  public int charsWidth(char[] a, int off, int len);
  public int stringWidth(String s);
  public int[] getWidths();
  public int getMaxAdvance();
  }

public class Graphics
  {
  public Color getColor();
  public void setColor(Color c);
  public Font getFont();
  public void setFont(Font f);
  public FontMetrics getFontMetrics();
  public FontMetrics getFontMetrics(Font f);
  public void fillRect(int x, int y, int w, int h);
  public void drawString(String s, int x, int y);
  }

And the following "value-add" is also provided, which essentially recognises the fact that if you're using Mini-AWT, you've probably not got a screen to directly show the result on, so you'll be serialising and sending the result :-

public class PngEncoder
  {
  public PngEncoder(Image i);
  public void encode(OutputStream os);
  }

In the past, the choice of PNG rather than GIF avoided the infamous "Unisys LZW / Compu$erve GIF Tax".

So in short, you can draw simple graphs using rectangles and text, and serialise them for sending to disk or to client browsers.

As time goes on, and the need arises, it is anticipated that additional support will be added.

How to use

Use Mini-AWT, rather than real AWT, ie: don't use :-

import java.awt.*;

replace with :-

import nyangau.miniawt.*;

Then use the subset of AWT, as defined above. eg: In a servlet doGet, you might code something like :-

Frame fr = new Frame();
fr.addNotify();
Image image = fr.createImage(200, 100);
Graphics g = image.getGraphics();
g.setColor(new Color(0x11,0x22,0x33));
g.fillRect(0, 0, 200, 100);
g.setColor(new Color(0xff,0xff,0xff));
g.drawString("Hello", 50, 50);

If you've been using AWT to construct an Image to send to a client browser, you've probably been using the Acme GIF encoder, and have code much like :-

import Acme.JPM.Encoders.GifEncoder;

ServletOutputStream os = resp.getOutputStream();
resp.setContentType("image/gif");
new GifEncoder(image, os).encode();

This can be replaced with the Mini-AWT equivelent :-

ServletOutputStream os = resp.getOutputStream();
resp.setContentType("image/png");
new PngEncoder(image).encode(os);

If you examine the source code, you'll see quite a few member variables and methods which are 'more public' than they should be. They're certainly not a part of AWT. Mini-AWT uses these 'back doors' internally - you should not. Luckily, they're all marked in the source code with comments like

class Image
  {
  ... snip
  public Image(int w, int h)  // not normally public

Future

If I require more drawing primitives, I may well add them.

Copying

Feel free to copy, its public domain. Caveat Emptor.


The documentation is written and maintained by Andy Key
andy.z.key@googlemail.com