叶
叶枫
Unregistered / Unconfirmed
GUEST, unregistred user!
请大家看看下面的代码到底那里有问题,为什么生成不了img2?
在下初学JAVA,从来没有做过图形开发,还请各位多多帮忙!
在下初学JAVA,从来没有做过图形开发,还请各位多多帮忙!
代码:
BufferedImage image;
Image img;
Image img2;
public voiddo
Get(HttpServletRequest req,HttpServletResponse res)
throws ServletException ,IOException {
PrintWriter out = res.getWriter();
try {
Frame frame = new Frame();
frame.addNotify();
frame.setBackground(new Color(255,212,212));
image = (BufferedImage) frame.createImage(512, 384);
Graphics graphics = image.getGraphics();
MediaTracker mt = new MediaTracker(frame) ;
try {
img = Toolkit.getDefaultToolkit().getImage("C://kyo.jpg");
mt.addImage(img, 0) ;
mt.waitForID(0) ;
}
catch (Exception e) {
e.printStackTrace() ;
}
int imgWidth = img.getWidth(frame) ;
int imgHeight = img.getHeight(frame) ;
ImageFilter imgFilter = new ReplicateScaleFilter( imgWidth/2 , imgHeight/2 ) ;
FilteredImageSource fImgSource = new FilteredImageSource(img.getSource(), imgFilter) ;
img2 = frame.createImage(fImgSource);
if (graphics.drawImage(img2, 0, 0,frame)) {
res.setContentType("image/jpeg");
ServletOutputStream outi = res.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outi);
encoder.encode(image);
}
else
{
out.println("no draw!");
}
} catch(Exception e) {
out.println(e);
}
}