drawPolyLine

From: Taji (gabsaga_tata_at_hotmail.com)
Date: 10/01/04


Date: 1 Oct 2004 11:25:58 -0700

Does anyone have a servlet that draws a poly-line graph that I can
use? I have one but it draws the graph with the origin at the top left
corner of the image object. I just need to know how to translate the
graph so that the origin is at the bottom left corner so it looks like
a normal graph with x and y coordinates. Any help will be appreciated.
Thanks. Gabsaga

// servlet imports
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// image imports
import java.awt.* ;
import java.awt.image.* ;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.image.codec.jpeg.*;

public class PlotPoints extends HttpServlet {

/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}

/**Process the HTTP Post request*/
public void service(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

int xCoords[] = {0,10,200,300,400};
int yCoords[] = {0,10,100,200,0};
int numOfPoints = xCoords.length;

BufferedImage img = new BufferedImage
(200,200,BufferedImage.TYPE_3BYTE_BGR);

Graphics g = img.getGraphics();

Font font = Font.getFont("Dialog");

g.setFont(font);
g.setColor(Color.white);
//g.translate(200 / 20, 200 - 200 / 20); // <---- I tried this but it
didn't work
g.drawPolyline(xCoords, yCoords, numOfPoints);

response.setContentType("image/gif");

ServletOutputStream out = response.getOutputStream();

JPEGImageEncoder jenc = JPEGCodec.createJPEGEncoder(out);
jenc.encode(img);

out.flush();
out.close();
}
} //end of doService