Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

graphics 2d/servlet problem  RSS feed

eric clark
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!