File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes graphics 2d/servlet problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "graphics 2d/servlet problem" Watch "graphics 2d/servlet problem" New topic

graphics 2d/servlet problem

eric clark

Joined: Jan 18, 2001
Posts: 16
hithere, just in case any1 knows. i have a servlet that writes a jpg from a text and a background rectangle, using graphics2d. when the x11 server runs, it werx fine, but when its not running, it makes some weird errors. the weird thing is, that theres no connection to x11. and i dont need the graphical enviroment, i only wanna genaret a jpg.
javax.servlet.ServletException: sun/awt/X11GraphicsEnvironment
servlet source:
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.geom.Rectangle2D;
public class JPEGServlet {

public void generateButton()
// Create image
int width = 400; // width of image
int height = 200; // height of image
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); // Image object to paint with
Graphics2D g = image.createGraphics(); // Get graphics context
RenderingHints.VALUE_ANTIALIAS_ON); // Anti-alias the painting
// First set background color to white by painting a filled rectangle
Rectangle2D rectangle = new Rectangle2D.Double(0, 0, width, height);
GradientPaint gp = new GradientPaint(
0, 0, Color.lightGray,
400, 100,; // Create a gradient fill from lightgray to black
g.setPaint(gp); // Use gradient fill to draw text
g.setFont(new Font("Serif", Font.BOLD, 40)); // Set font bold and size 40
g.drawString("Hello!", 0, 30);
// JPEG-encode the image
//and write to file.
System.out.println("writing file");
OutputStream os =
new FileOutputStream("bla.jpg");
JPEGImageEncoder encoder =
catch (Exception e)

I agree. Here's the link:
subject: graphics 2d/servlet problem
jQuery in Action, 3rd edition