我没有做过图形方面的,看看我错在哪里?
Frame sourceFrame = new Frame();
sourceFrame.addNotify();
Image im = sourceFrame.createImage(100,100);
Graphics g = im.getGraphics();
g.drawString("Hello",50,50);
我用上面的方法生成了一个IMAGE,然后用下面的方法调用时,异常:"Image must be 8-bit"GIFEncoder ge = new GIFEncoder(im);附GIFEncoder(image im)
public GIFEncoder(Image img) {
width = img.getWidth(null);
height = img.getHeight(null);
pixels = new byte[width * height];
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println(e);
};
ColorModel cm = pg.getColorModel();
if (cm instanceof IndexColorModel)
{
pixels = (byte[])(pg.getPixels());
// hpm
IndexColorModel icm = (IndexColorModel)cm;
setTransparentPixel(icm.getTransparentPixel());
}
else
throw new IllegalArgumentException("Image must be 8-bit");
IndexColorModel m = (IndexColorModel)cm;
int mapSize = m.getMapSize();
r = new byte[mapSize];
g = new byte[mapSize];
b = new byte[mapSize];
m.getReds(r);
m.getGreens(g);
m.getBlues(b);
interlace = false;
pixelIndex = 0;
numPixels = width*height; }
Frame sourceFrame = new Frame();
sourceFrame.addNotify();
Image im = sourceFrame.createImage(100,100);
Graphics g = im.getGraphics();
g.drawString("Hello",50,50);
我用上面的方法生成了一个IMAGE,然后用下面的方法调用时,异常:"Image must be 8-bit"GIFEncoder ge = new GIFEncoder(im);附GIFEncoder(image im)
public GIFEncoder(Image img) {
width = img.getWidth(null);
height = img.getHeight(null);
pixels = new byte[width * height];
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println(e);
};
ColorModel cm = pg.getColorModel();
if (cm instanceof IndexColorModel)
{
pixels = (byte[])(pg.getPixels());
// hpm
IndexColorModel icm = (IndexColorModel)cm;
setTransparentPixel(icm.getTransparentPixel());
}
else
throw new IllegalArgumentException("Image must be 8-bit");
IndexColorModel m = (IndexColorModel)cm;
int mapSize = m.getMapSize();
r = new byte[mapSize];
g = new byte[mapSize];
b = new byte[mapSize];
m.getReds(r);
m.getGreens(g);
m.getBlues(b);
interlace = false;
pixelIndex = 0;
numPixels = width*height; }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货