Tag: code

LineStyles.java Provides examples of the available styles for joining line segments

import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** A demonstration of different controls when joining two line  *  segments. The style of the line end point is controlled  *  through the capStyle parameter.  *  ************************************ public class LineStyles extends JPanel {   private GeneralPath path;   private static int x = 30, deltaX = 150, y …

Continue reading

Illustrates using a local font (Goudy Handtooled BT) to perform drawing in Java 2D

import java.awt.*; /** An example of using local fonts to perform drawing in  *  Java 2D.  *  ********************** public class FontExample extends GradientPaintExample {   public FontExample() {     GraphicsEnvironment env =       GraphicsEnvironment.getLocalGraphicsEnvironment();     env.getAvailableFontFamilyNames();     setFont(new Font(“Goudy Handtooled BT”, Font.PLAIN, 100));   }   protected void drawBigString(Graphics2D g2d) {     g2d.setPaint(Color.black);     g2d.drawString(“Java …

Continue reading

Draws a circle with a gradient fill

GradientPaintExample.java Draws a circle with a gradient fill. Inherits from ShapeExample.java. ************************************** import java.awt.*; /** An example of applying a gradient fill to a circle. The  *  color definition starts with red at (0,0), gradually  *  changing to yellow at (175,175).  *  ********************************** public class GradientPaintExample extends ShapeExample {   private GradientPaint gradient =     …

Continue reading

An applet that permits freehand drawing

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** An applet that lets you perform freehand drawing.  *    **************** public class SimpleWhiteboard extends Applet {   protected int lastX=0, lastY=0;   public void init() {     setBackground(Color.white);     setForeground(Color.blue);     addMouseListener(new PositionRecorder());     addMouseMotionListener(new LineDrawer());   }   protected void record(int x, int y) {     …

Continue reading

A TextField that uses key events to correct the spelling of the names of computer languages entered into it

import java.awt.*; import java.awt.event.*; /** A spelling-correcting TextField for entering  *  a language name.  *    ******************* public class LanguageField extends TextField {   private String[] substrings =     { “”, “J”, “Ja”, “Jav”, “Java” };   public LanguageField() {     addKeyListener(new SpellingCorrector());     addActionListener(new WordCompleter());     addFocusListener(new SubliminalAdvertiser());   }   // Put caret …

Continue reading

DebugTag.java Custom tag that optionally makes use of a tag body

DebugTag.java Custom tag that optionally makes use of a tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; /** A tag that includes the body content only if  *  the “debug” request parameter is set.  *  <P>  *  Taken from Core Web Programming Java 2 Edition  *  from Prentice Hall and Sun …

Continue reading

HeadingTag.java Custom tag that makes use of a tag body

HeadingTag.java Custom tag that makes use of a tag body package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; /** Generates an HTML heading with the specified background  *  color, foreground color, alignment, font, and font size.  *  You can also turn on a border around it, which normally  *  just barely encloses the heading, but …

Continue reading

PrimeTag.java Custom tag that outputs a random prime number of a user-specifiable approximate length

PrimeTag.java Custom tag that outputs a random prime number of a user-specifiable approximate length package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; /** Generates an N-digit random prime (default N = 50).  *  Extends SimplePrimeTag, adding a length attribute  *  to set the size of the prime. The doStartTag  *  method of the parent class …

Continue reading

SimplePrimeTag.java Custom tag that outputs a random prime number of a fixed approximate length

SimplePrimeTag.java Custom tag that outputs a random prime number of a fixed approximate length package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import java.math.*; import cwp.*; /** Generates a prime of approximately 50 digits.  *  (50 is actually the length of the random number  *  generated — the first prime above that number will  *  …

Continue reading

cwp-taglib.tld Tag Library Descriptor file used by the custom tags in this chapter.

cwp-taglib.tld Tag Library Descriptor file used by the custom tags in this chapter. <?xml version=”1.0″ encoding=”ISO-8859-1″ ?> <!DOCTYPE taglib  PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN”  “http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd”> <!– a tag library descriptor –> <taglib>   <!– after this the default space is         “http://java.sun.com/j2ee/dtds/jsptaglibrary_1_2.dtd”    –>   <tlibversion>1.0</tlibversion>   <jspversion>1.1</jspversion>   <shortname>cwp</shortname>   <!– …

Continue reading