/* * DrawText.java * Usage: #font * #file * * * Ken Shirriff ken.shirriff@eng.sun.com * * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * Please refer to the file "license.txt" * for further important copyright and licensing information. */ import java.awt.*; import java.awt.image.*; import java.applet.Applet; import java.net.*; import java.io.*; import pcffont.*; public class DrawText extends Applet { PCFFont font; Image backImg; Color bg = Color.white; public void init() { String fontname = getParameter("font"); String filename = getParameter("file"); if (fontname == null || filename == null) { System.out.println("Must specify text and file"); showStatus("Must specify text and file"); return; } try { // Load the font URL url = new URL(getDocumentBase(), fontname); font = new PCFFont(url.openStream()); font.setBgColor(null); // Create the image int dispWidth = size().width; int dispHeight = size().height; backImg = createImage(dispWidth, dispHeight); Graphics g = backImg.getGraphics(); g.setColor(bg); g.fillRect(0, 0, dispWidth, dispHeight); // Open the text file URL fileURL = new URL(getDocumentBase(), filename); DataInputStream dis = new DataInputStream(fileURL.openStream()); // Read lines from the file and draw them in the font int ypos = font.getAscent(); String line; while ((line = dis.readLine()) != null) { if (font.is2Byte()) { line = PCFFont.toJISX(line); } font.drawString(g, line, 0, ypos); ypos += font.getHeight(); } } catch (Exception e) { System.out.println("Exception "+e); return; } repaint(); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(backImg, 0, 0, null); } public String getAppletInfo() { return "DrawText by Ken Shirriff shirriff@eng.sun.com"; } }