首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

作图Item

2012-11-11 
绘制Item1Display display new Display()2Shell shell new Shell(display)3shell.setBounds(10,10,2

绘制Item

1Display display = new Display();2Shell shell = new Shell(display);3shell.setBounds(10,10,200,250);4final Table table = new Table(shell, SWT.NONE);5table.setBounds(10,10,150,200);6table.setLinesVisible(true);7for (int i = 0; i8???new TableItem(table, SWT.NONE).setText("item " + i);9}10table.addListener(SWT.MeasureItem, new Listener() {11???public void handleEvent(Event event) {12??????int clientWidth = table.getClientArea().width;13??????event.height = event.gc.getFontMetrics().getHeight() * 2;14??????event.width = clientWidth * 2;15???}16});17shell.open();18while (!shell.isDisposed()) {19???if (!display.readAndDispatch()) display.sleep();20}21display.dispose();

Listing 1. Specifying custom widths and heights for items in a table with no columns

Lines 1-3:
Creates a display and shell, and sets the shell's bounds.

Lines 4-6:
Creates the table with no columns, sets its bounds, and sets its lines to be visible.

Lines 7-9:
Creates the table's items.

Lines 10-11
Adds an
Figure 1. Screenshot of Listing 1 running, which specifies custom widths and heights for the items in a table

作图Item
Figure 2. Screenshot of Listing 1 running without the1Display display = new Display();2Shell shell = new Shell(display);3shell.setBounds(10,10,400,200);4Table table = new Table(shell, SWT.NONE);5table.setBounds(10,10,350,150);6table.setHeaderVisible(true);7table.setLinesVisible(true);8final TableColumn column0 = new TableColumn(table, SWT.NONE);9column0.setWidth(100);10final TableColumn column1 = new TableColumn(table, SWT.NONE);11column1.setWidth(100);12column0.addListener(SWT.Selection, new Listener() {13???public void handleEvent(Event event) {14??????column0.pack();15???}16});17column1.addListener(SWT.Selection, new Listener() {18???public void handleEvent(Event event) {19??????column1.pack();20???}21});22for (int i = 0; i23???TableItem item = new TableItem(table, SWT.NONE);24???item.setText(0, "item " + i + " col 0");25???item.setText(1, "item " + i + " col 1");26}27table.addListener(SWT.MeasureItem, new Listener() {28???public void handleEvent(Event event) {29??????event.width *= 2;30???}31});32shell.open();33while (!shell.isDisposed()) {34???if (!display.readAndDispatch()) display.sleep();35}36display.dispose();

Listing 2. Specifying custom cell content widths that are used by
Figure 3. Screenshot of Listing 2 running after the table's columns have been packed using the cell content sizes specified in theSWT.MeasureItem
Figure 4. Screenshot of Listing 2 running after the table's columns have been packed using the default cell sizes

SWT.EraseItem

SWT.EraseItem1Display display = new Display();2Shell shell = new Shell(display);3final Color red = display.getSystemColor(SWT.COLOR_RED);4final Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);5final Table table = new Table(shell, SWT.FULL_SELECTION);6table.setHeaderVisible(true);7new TableColumn(table, SWT.NONE).setWidth(100);8new TableColumn(table, SWT.NONE).setWidth(100);9new TableColumn(table, SWT.NONE).setWidth(100);10for (int i = 0; i11???TableItem item = new TableItem(table, SWT.NONE);12???item.setText(0, "item " + i + " col 0");13???item.setText(1, "item " + i + " col 1");14???item.setText(2, "item " + i + " col 2");15}16table.pack();17table.addListener(SWT.EraseItem, new Listener() {18???public void handleEvent(Event event) {19??????event.detail &= ~SWT.HOT;20??????if ((event.detail & SWT.SELECTED) == 0) return; /* item not selected */21??????int clientWidth = table.getClientArea().width;22??????GC gc = event.gc;23??????Color oldForeground = gc.getForeground();24??????Color oldBackground = gc.getBackground();25??????gc.setForeground(red);26??????gc.setBackground(yellow);27??????gc.fillGradientRectangle(0, event.y, clientWidth, event.height, false);28??????gc.setForeground(oldForeground);29??????gc.setBackground(oldBackground);30??????event.detail &= ~SWT.SELECTED;31???}32});33shell.pack();34shell.open();35while (!shell.isDisposed()) {36???if (!display.readAndDispatch()) display.sleep();37}38display.dispose();

Listing 3: Custom drawing item selection

Lines 1-2:
Creates a display and shell.

Lines 3-4:
Obtains the system's red and yellow colors, which will be used for drawing the custom selection.

Lines 5-6:
Creates the table and sets its header to be visible. The table's style is specified to be
Figure 5. Screenshot of Listing 3 running, which uses an1final String[] MONTHS = {2???"Jan", "Feb", "Mar", "Apr", "May", "Jun",3???"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"4}5final int[] HIGHS = {-7, -4, 1, 11, 18, 24, 26, 25, 20, 13, 5, -4};6final int[] LOWS = {-15, -13, -7, 1, 7, 13, 15, 14, 10, 4, -2, -11};7final int SCALE_MIN = -30; final int SCALE_MAX = 30;8final int SCALE_RANGE = Math.abs(SCALE_MIN - SCALE_MAX);9Display display = new Display();10Shell shell = new Shell(display);11shell.setBounds(10,10,400,350);12shell.setText("Ottawa Average Daily Temperature Ranges");13final Color blue = display.getSystemColor(SWT.COLOR_BLUE);14final Color white = display.getSystemColor(SWT.COLOR_WHITE);15final Color red = display.getSystemColor(SWT.COLOR_RED);16final Image parliamentImage = new Image(display, "./parliament.jpg");17final Table table = new Table(shell, SWT.NONE);18table.setBounds(10,10,350,300);19table.setBackgroundImage(parliamentImage);20for (int i = 0; i21???TableItem item = new TableItem(table, SWT.NONE);22???item.setText(MONTHS[i] + " (" + LOWS[i] + "C..." + HIGHS[i] + "C)");23}24final int clientWidth = table.getClientArea().width;25table.addListener(SWT.MeasureItem, new Listener() {26???public void handleEvent(Event event) {27??????int itemIndex = table.indexOf((TableItem)event.item);28??????int rightX = (HIGHS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;29??????event.width = rightX;30???}31});32table.addListener(SWT.EraseItem, new Listener() {33???public void handleEvent(Event event) {34??????int itemIndex = table.indexOf((TableItem)event.item);35??????int leftX = (LOWS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;36??????int rightX = (HIGHS[itemIndex] - SCALE_MIN) * clientWidth / SCALE_RANGE;37??????GC gc = event.gc;38??????Rectangle clipping = gc.getClipping();39??????clipping.x = leftX;40??????clipping.width = rightX - leftX;41??????gc.setClipping(clipping);42??????Color oldForeground = gc.getForeground();43??????Color oldBackground = gc.getBackground();44??????gc.setForeground(blue);45??????gc.setBackground(white);46??????gc.fillGradientRectangle(
?????????event.x, event.y, event.width / 2, event.height, false);
47??????gc.setForeground(white);48??????gc.setBackground(red);49??????gc.fillGradientRectangle(
?????????event.x + event.width / 2, event.y, event.width / 2, event.height, false);
50??????gc.setForeground(oldForeground);51??????gc.setBackground(oldBackground);52??????event.detail &= ~SWT.BACKGROUND;53??????event.detail &= ~SWT.HOT;54???}55});56shell.open();57while (!shell.isDisposed()) {58???if (!display.readAndDispatch()) display.sleep();59}60parliamentImage.dispose();61display.dispose();

Listing 4. Custom drawing item backgrounds

Lines 1-8:
Defines constants that will be used to set the table's item data and to draw item backgrounds.

Lines 9-12:
Creates a display and shell, and sets the shell's bounds and title.

Lines 13-15:
Obtains the system's blue, white and red colors, which will be used for drawing item backgrounds.

Line 16:
Loads an image that will be set into the table as the background.

Lines 17-18:
Creates the table with no columns and sets its bounds.

Line 19:
Sets the background image into the table.

Lines 20-23:
Creates the table's items.

Line 24:
Stores the table's client width for later use when drawing the item backgrounds.

Lines 25-26:
Adds an
Figure 6. Screenshot of Listing 4 running, which uses an1Display display = new Display();2Shell shell = new Shell(display);3shell.setBounds(10, 10, 350, 200);4Image xImage = new Image (display, 16, 16);5GC gc = new GC(xImage);6gc.setForeground(display.getSystemColor(SWT.COLOR_RED));7gc.drawLine(1, 1, 14, 14);8gc.drawLine(1, 14, 14, 1);9gc.drawOval(2, 2, 11, 11);10gc.dispose();11final int IMAGE_MARGIN = 2;12final Tree tree = new Tree(shell, SWT.CHECK);13tree.setBounds(10, 10, 300, 150);14TreeItem item = new TreeItem(tree, SWT.NONE);15item.setText("root item");16for (int i = 0; i17???TreeItem newItem = new TreeItem(item, SWT.NONE);18???newItem.setText("descendent " + i);19???if (i % 2 == 0) newItem.setData(xImage);20???item.setExpanded(true);21???item = newItem;22}23tree.addListener(SWT.MeasureItem, new Listener() {24???public void handleEvent(Event event) {25??????TreeItem item = (TreeItem)event.item;26??????Image trailingImage = (Image)item.getData();27??????if (trailingImage != null) {28?????????event.width += trailingImage.getBounds().width + IMAGE_MARGIN;29??????}30???}31});32tree.addListener(SWT.PaintItem, new Listener() {33???public void handleEvent(Event event) {34??????TreeItem item = (TreeItem)event.item;35??????Image trailingImage = (Image)item.getData();36??????if (trailingImage != null) {37?????????int x = event.x + event.width + IMAGE_MARGIN;38?????????int itemHeight = tree.getItemHeight();39?????????int imageHeight = trailingImage.getBounds().height;40?????????int y = event.y + (itemHeight - imageHeight) / 2;41?????????event.gc.drawImage(trailingImage, x, y);42??????}43???}44});45shell.open();46while (!shell.isDisposed()) {47???if (!display.readAndDispatch()) display.sleep();48}49xImage.dispose();50display.dispose();

Listing 5. Using
Figure 7. Screenshot of Listing 5 running, which uses an1final int COLUMN_COUNT = 4;2final int ITEM_COUNT = 8;3final int TEXT_MARGIN = 3;4Display display = new Display();5Shell shell = new Shell(display);6final Table table = new Table(shell, SWT.FULL_SELECTION);7table.setHeaderVisible(true);8table.setLinesVisible(true);9for (int i = 0; i10???new TableColumn(table, SWT.NONE);11}12for (int i = 0; i13???TableItem item = new TableItem(table, SWT.NONE);14???for (int j = 0; j15??????String string = "item " + i + " col " + j;16??????if ((i + j) % 3 == 1) {17?????????string +="\nnew line1";18??????}19??????if ((i + j) % 3 == 2) {20?????????string +="\nnew line1\nnew line2";21??????}22??????item.setText(j, string);23???}24}25table.addListener(SWT.MeasureItem, new Listener() {26???public void handleEvent(Event event) {27??????TableItem item = (TableItem)event.item;28??????String text = item.getText(event.index);29??????Point size = event.gc.textExtent(text);30??????event.width = size.x + 2 * TEXT_MARGIN;31??????event.height = Math.max(event.height, size.y + TEXT_MARGIN);32???}33});34table.addListener(SWT.EraseItem, new Listener() {35???public void handleEvent(Event event) {36??????event.detail &= ~SWT.FOREGROUND;37???}38});39table.addListener(SWT.PaintItem, new Listener() {40???public void handleEvent(Event event) {41??????TableItem item = (TableItem)event.item;42??????String text = item.getText(event.index);43??????/* center column 1 vertically */44??????int yOffset = 0;45??????if (event.index == 1) {46?????????Point size = event.gc.textExtent(text);47?????????yOffset = Math.max(0, (event.height - size.y) / 2);48??????}49??????event.gc.drawText(text, event.x + TEXT_MARGIN, event.y + yOffset, true);50???}51});52for (int i = 0; i53???table.getColumn(i).pack();54}55table.pack();56shell.pack();57shell.open();58while (!shell.isDisposed()) {59???if (!display.readAndDispatch()) display.sleep();60}61display.dispose();

Listing 6. Using
Figure 8. Screenshot of Listing 6 running, which uses an?SWT.PaintItem?listener to custom draw the table's items

?