Okay . . .
I’ve not done a lot of serious game development yet, but I’ve played with the Java 2D API and am somewhat familiar with it. It’s been predictable stuff . . . trying to build a little tetris game using AWT, Swing . . . whatever.
But I’m making a web-game this time, and while trying to display mapping info for my in-game world, I’ve come across a situation where I simply MUST generate images on the fly.
“No problem,” I think, “I’ll just whip up a BufferedImage, draw a few quick lines to it, use ImageIO to write it straight to my output stream and voila!”
Great theory.
It doesn’t work.
Here’s the code:
List roadList=(List)request.getSession(true).getAttribute("roadList");
BufferedImage image=new BufferedImage(400,400,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2=(Graphics2D)image.getGraphics(); //I know . . . use createGraphics . . . this is temporary
g2.setBackground(new Color(0,0,0,0));
g2.setPaint(Color.RED);
for(int loop=0; loop<roadList.size()-1; loop++)
{
RoadSegmentMapping start=(RoadSegmentMapping)roadList.get(loop);
RoadSegmentMapping end=(RoadSegmentMapping)roadList.get(loop+1);
g2.drawLine(start.getXloc().intValue(),start.getYloc().intValue(),
end.getXloc().intValue(),end.getYloc().intValue());
}
response.setContentType("image/png");
try
{
ImageOutputStream out=ImageIO.createImageOutputStream(response.getOutputStream());
ImageIO.write(image,"png",out);
response.getOutputStream().flush();
response.getOutputStream().close();
}
The stack trace I get is . . .
java.lang.NoClassDefFoundError
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:141)
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1041)
java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1031)
com.alliance.action.DynamicImageTestAction.execute(DynamicImageTestAction.java:32)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.alliance.servlet.SessionFilter.doFilter(SessionFilter.java:39)
I’m guessing because I’m not in a windowing environment, there’s no GraphicsEvironment set up. I’m running this on a linux server that is not running XWindows or anything like . . . strictly text-based. Running on Tomcat currently. If details are important, I’ll get them, but I just need to know where to start looking.
Thanks in advance.
-PhilBob