`

Java SWT 图像 PaletteData 索引图和直接图

 
阅读更多

索引图(Indexed palette):

就是在palette中定义了一个RGB的数组,然后,每一个像素点都是一个RGB数组的坐标来到palette中去取值。

public class PaletteIndexExample {
	static Display display;

	public void drawImage(final Image image) {
		Shell shell = new Shell (display);
		shell.setLayout (new FillLayout ());
		shell.setSize(400, 300);
		shell.setLocation(600,300);
		Canvas canvas = new Canvas (shell, SWT.NONE);
		canvas.addPaintListener (new PaintListener () {
			public void paintControl (PaintEvent e) {
				e.gc.drawImage (image, 20, 20);
			}
		});

		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ())
				display.sleep ();
		}
	}

	public Image createIndexImage() {
		RGB redRGB = new RGB(255,0,0);
		RGB greenRGB = new RGB(0,255,0);
		PaletteData paletteData = new PaletteData(new RGB[]{redRGB,greenRGB});

		ImageData imageData = new ImageData(48,48,1,paletteData);
		for(int x=11;x<35;x++){
			for(int y=11;y<35;y++){
				imageData.setPixel(x,y,1);
			}
		}
		return new Image(display,imageData);
	}

	public static void main(String[] args) {
		display = new Display();
		PaletteIndexExample e = new PaletteIndexExample();
		e.drawImage(e.createIndexImage());
	}
}

 

 

直接图(Direct palette):

ImageData中的data存的直接的RGB,但这个RGB怎么对应的是有Palette的(redMask, greenMask, blueMask)来决定的,如下面的(redMask, greenMask, blueMask)为(0xFF0000   , 0xFF00   , 0xFF),这样在利用ImagaData的setPixel(int)的时候,如imageData.setPixel(x,y,0xFF0055),(x,y)这个像素点的red设置成的FF、green设置成了00、blue设置成了55.

public class PaletteDirectExample {

	static Display display;

	public void drawImage(final Image image) {
		Shell shell = new Shell (display);
		shell.setLayout (new FillLayout ());
		shell.setSize(400, 300);
		shell.setLocation(600,300);
		Canvas canvas = new Canvas (shell, SWT.NONE);
		canvas.addPaintListener (new PaintListener () {
			public void paintControl (PaintEvent e) {
				e.gc.drawImage (image, 20, 20);
			}
		});

		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ())
				display.sleep ();
		}
	}

	public Image createIndexImage() {
		// ***                    PaletteData(redMask, greenMask, blueMask)
		PaletteData palette = new PaletteData(0xFF0000   , 0xFF00   , 0xFF);

		ImageData imageData = new ImageData(48,48,24,palette);
		for (int x=0;x<48;x++) {
			for(int y=0;y<48;y++) {
				if(y > 11 && y < 35 && x > 11 && x < 35) {
					imageData.setPixel(x,y,0xFFFFFF);   // Set the center to green
				} else {
					imageData.setPixel(x,y,0xFF);   // and everything else to red
				}
			}
		};
		return new Image(display,imageData);
	}

	public static void main(String[] args) {
		display = new Display();
		PaletteDirectExample e = new PaletteDirectExample();
		e.drawImage(e.createIndexImage());
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics