Image generation on Java 1.1
For my first generated image, I will create code I can deploy on the Java 1.1 platform. Although 1.2 JVMs are fairly common, not everyone can count on deploying code on the latest platform. Some commercial Web-hosting environments, for example, are somewhat slow to update the Java Runtime Environments (JREs) on their servers. This means that any servlets you deploy must run on Java 1.1. Designing the code for Java 1.1do
es not greatly complicate matters;
however, Ido
discuss a troublesome issue regarding in-memory image creation later in the article.
I will create the image of a simple pie chart. A pie chart is a useful, easy-to-read chart. This pie chart will be complete with colorization and labeling of the pie's pieces. The chart will indicate soft drinks favored by software developers. Now I'll take a look at how you can go about this.
Fordo
ing image I/O on Java 1.1, Sun offers a class library known as Jimi. Sun purchased this class library from a small software vendor. After Sun began distributing the class library, it repackaged the classes into com.sun, but otherwise left them unchanged. The following are the steps you should take to produce a PNG image using the Jimi toolkit.
Construct a Frame window to provide an AWT image.
Use the AWT image to get a Graphics instance.
Draw into the Graphics object.
Create a JimiWriter using the HTTP output stream.
Have the JimiWriter write out the AWT image previously created.
On the Java 1.1 platform, constructing an in-memory image appears to require that an AWT component exist on the desktop. Normally, you should not use Java servlet code to create AWT components. It justdo
esn't make sense for the Web server to create user interface components to respond to a client request. In this case however, you need an image to provide a Graphics instance that you can draw into. So, you will have to live with the small window that appears on the Web server's desktop. If you know of a way to create an image on Java 1.1 without an AWT component, please let me know. One possible design would use a template image on the Web server's disk. You could load the image data from the disk, and construct the AWT image using java.awt.Toolkit.createImage(). After you construct the AWT image, it can provide the Graphics instance, which you can draw into as usual. If this works, you could avoid the spurious window. I will leave this experiment as an exercise to the interested reader.
First of all, the connection between the servlet and the class to produce the pie chart is the interface discussed earlier, ImageProducer. When you construct JIMIProducer, the ImageProducer for the pie chart, it will create an AWT frame, an AWT image, and subsequently an AWT graphics context. The following code sample is from the JIMIProducer:
Frame f = new Frame();
f.setVisible(true);
image = f.createImage(ImageWidth, ImageHeight);
graphics = image.getGraphics();
f.setVisible(false);
The key method in the JIMIProducer class is drawSlice(). Given a label and the number of degrees the slice should consume, the class draws a slice of the pie chart and colorizes it. Next, I'll discuss the way this method works and revisit some high school geometry while I'm at it.
You begin
drawing the pie chart internals at the three o'clock position;
that is, you draw a line from the chart's center to the three o'clock point on the bounding oval. After you draw the initial line, you draw each additional line in a clockwise direction. The following code takes the number of degrees passed to the method and draws a line reflecting a pie slice with that angle. The calculation first converts the angle to radians;
I start with degrees because it seems more natural. At any rate, the angle is converted to Cartesian coordinates. This x and y value is then
used as an offset to the chart's center point. A line is subsequently drawn from the center to the calculated point. This process is repeated for each call to the drawSlice method;
the accumulated calls to this method should total 360 degrees, or the pie chart will not look right.
//***************************************************************
// Convert to radians
// 1 degree = pi/180 radians
//***************************************************************
do
uble theta = degrees * (3.14/180);
currentTheta += theta;
//***************************************************************
// Convert to x/y
// x = r cos @
// y = r sin @
//***************************************************************
do
uble x = Radius * Math.cos(currentTheta);
do
uble y = Radius * Math.sin(currentTheta);
Point mark2 = new Point(center);
mark2.translate((int)x,(int)y);
graphics.drawLine(center.x, center.y, mark2.x, mark2.y);
The next step is to provide a unique color for the new pie slice. The following code illustrates how you cando
this. The handy fillArc() method makes it easy to color the pie slice.
graphics.setColor(colors[colorIndex++]);
graphics.fillArc(Inset,
Inset,
PieWidth,
PieHeight,
-1 * lastAngle,
-1 * degrees);
The final task for drawSlice() is to label the new pie slice. The code calculates an appropriate place for the label based on the font metrics and then
draws the text. The complete pie chart is found in Figure 1.
不想为你做翻译了。