*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "graphics 2d/servlet problem" Watch "graphics 2d/servlet problem" New topic
Author

graphics 2d/servlet problem

eric clark
Greenhorn

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.
error:
javax.servlet.ServletException: sun/awt/X11GraphicsEnvironment
servlet source:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
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
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // Anti-alias the painting
// First set background color to white by painting a filled rectangle
g.setPaint(Color.white);
Rectangle2D rectangle = new Rectangle2D.Double(0, 0, width, height);
g.fill(rectangle);
GradientPaint gp = new GradientPaint(
0, 0, Color.lightGray,
400, 100, Color.black); // 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.
try
{
System.out.println("writing file");
OutputStream os =
new FileOutputStream("bla.jpg");
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(os);
encoder.encode(image);
os.close();
}
catch (Exception e)
{
}
}
}

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: graphics 2d/servlet problem