Control, Devices, Images
1. 创建一个 或者 维持一个 GC
2. drawing
3 . 销毁 GC
GC gc = new GC(display);
gc.drawRectangle(...);
gc.drawText(...);
gc.drawImage(...);
gc.dispose();
swt 使用两维 坐标系, 以(0,0)为原点, 从左到右, 从上到下递增
PaintEvent 包含了一个有效的GC instance, 而且不能销毁它
画布画图形的时候, 相对于画布的左上角图标
fillRectangle: 是一个固态控件, 没有边框
drawRectangle: 有边框
Drawing Text:
你可以显示一个text在一个单行, 也可以自动换行
改变font, size, color, style, orientation
drawString()
drawText() : 处理 newLine and tabs
void drawText(String text, int x, int y, int flags) :
Draws the specified string with its origin at the point specified by (x, y), processing newlines and expanding tabs.
Uses the rules specified by flags (see Table 10-2 for more information).
Table 10-2: drawText() Flags
Constant Description
SWT.DRAW_DELIMITER Processes newlines by drawing subsequent characters on the next line. (是否显示newLine)
SWT.DRAW_TAB Processes tabs by displaying a gap between surrounding characters. (是否显示Tab)
SWT.DRAW_MNEMONIC Draws an underline beneath the mnemonic character—the character preceded by an ampersand (&).
Use this when drawing menus. (是否显示下划线)
SWT.DRAW_TRANSPARENT Uses a transparent background when drawing the string (是否使文本透明)
如果你没有指定font, 就会使用Dispaly.getSystemFont()
你可以调用GC.setFont() ,但是要记得销毁掉 Font
Font font = new Font(shell.getDisplay(), "Helvetica", 18, SWT.NORMAL);
font.dispose();
// fontData 代表的只是Font 的数据, 不要销毁它
public FontData(String name, int height, int style)
// 用 gc.getStringExtend() 得到String 的宽度
int strWidht = gc.getStringExtend("abc");
int strHigh = gc.getFontMetrics().getHeight();
// 绘制背景色
gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
gc.drawText("I'm in blue!");
// drawing vertical Text
//setup 1. draw the text to an offscreen image
FontMetrics fm = gc.getFontMetrics();
Point pt = gc.textExtent(string);
Image stringImage = new Image(display, pt.x, pt.y);
/ / setp 2
Image(支持GIF, PNG, JPEG, TIFF,ICO,BMP,RLE)
1. 从一个file 中创建一个Image
Image image= new Image(displau, "c:\\temp\\swt.png");
Image image = new Image(display, MyClass.getResourceAsStream("/temp/swt.png"));
2. 创建一个空的Image
Image image1 = new Image(display, 300, 200);
Image image2 = new Image(display, myRect);
3. 从 ImageData 中创建 Image
Device
1. 你可以了解当前屏幕的一些属性