• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Image

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have the following code:
import java.awt.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Acme.JPM.Encoders.GifEncoder;
public class Confidentializer extends HttpServlet {
Frame frame = null;
Graphics g = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Construct a reusable unshown frame
frame = new Frame();
frame.addNotify();
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream();
try {
// Get the image location from the path info
String source = req.getPathTranslated();
if (source == null) {
throw new ServletException("Extra path information " +
"must point to an image");
}

// Load the image (from bytes to an Image object)
MediaTracker mt = new MediaTracker(frame); // frame acts as an ImageObserver
Image image = Toolkit.getDefaultToolkit().getImage(source);
mt.addImage(image, 0);
try {
mt.waitForAll();
}
catch (InterruptedException e) {
getServletContext().log(e, "Interrupted while loading image");
throw new ServletException(e.getMessage());
}
// Construct a matching-size off screen graphics context
int w = image.getWidth(frame);
int h = image.getHeight(frame);
Image offscreen = frame.createImage(w, h);
g = offscreen.getGraphics();
// Draw the image to the off screen graphics context
g.drawImage(image, 0, 0, frame);

// Write CONFIDENTIAL over its top
g.setFont(new Font("Monospaced", Font.BOLD | Font.ITALIC, 30));
g.drawString("CONFIDENTIAL", 10, 30);
// Encode the off screen graphics into a GIF and send it to the client
res.setContentType("image/gif");
GifEncoder encoder = new GifEncoder(offscreen, out);
encoder.encode();
}
finally {
// Clean up resources
if (g != null) g.dispose();
}
}
public void destroy() {
// Clean up resources
if (frame != null) frame.removeNotify();
}
}
Can anyone tell me why we can not simply pass image to encoder's constructor? Why do we have to create a new Image offscreen from frame.createImage(w, h)?
Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic