Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

graphics 2d/servlet problem

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
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)
{
}
}
}

 
reply
    Bookmark Topic Watch Topic
  • New Topic