首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

SWT&JFace学习札记1

2012-09-07 
SWT&JFace学习笔记1SWT Graphics and Image Handling?在SWT里,所有实现接口org.eclipse.swt.graphics.Draw

SWT&JFace学习笔记1

SWT Graphics and Image Handling

?

在SWT里,所有实现接口org.eclipse.swt.graphics.Drawable的类都能做Drawing的操作。

ControlImageDevice(Display, Printer)

?

Drawing Graphics的基本步骤如下:

    从目标Drawable Object上得到Graphic Context 实例在Graphic Context上操作drawingDispose the Graphic Context

示例code fragments for drawing on controls:

?? Fragment1:

 progressBar.addPaintListener(new PaintListener() {            public void paintControl(PaintEvent e) {                // The string to draw.                String string = (progressBar.getSelection() * 1.0 /                 (progressBar.getMaximum()-progressBar.getMinimum()) * 100) + "%";                Point point = progressBar.getSize();                Font font = new Font(shell.getDisplay(),"Courier",10,SWT.BOLD);                e.gc.setFont(font);                e.gc.setForeground(                   shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));                FontMetrics fontMetrics = e.gc.getFontMetrics();                int stringWidth =                   fontMetrics.getAverageCharWidth() * string.length();                int stringHeight = fontMetrics.getHeight();                                e.gc.drawString(string, (point.x-stringWidth)/2 ,                   (point.y-stringHeight)/2, true);                // Remember to dispose it, because you created it by calling                 // constructor.                font.dispose();            }        });

?Code Fragment2

 Display display = new Display();    Shell shell = new Shell(display);    shell.setLayout(new GridLayout(2, true));    Image image = new Image(display, "icons/eclipse.gif");    // Clones the image.    Image image2 = new Image(display, image.getImageData());    // Draws an oval    GC gc = new GC(image2);    gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));    gc.drawOval(10, 10, 90, 40);    gc.dispose();    CLabel label = new CLabel(shell, SWT.NULL);    label.setImage(image);    label.setBounds(10, 10, 130, 130);    CLabel label2 = new CLabel(shell, SWT.NULL);    label2.setImage(image2);    label2.setBounds(150, 10, 130, 130); 

?

Using Canvas:

// You can optionally specify one or more of its painting configuration styles,// SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.NO_MERGE_PAINTS etc.Canvas canvas = new Canvas(shell, SWT.NULL);    canvas.setBounds(10, 10, 200, 100);    canvas.addPaintListener(new PaintListener() {        public void paintControl(PaintEvent e) {            e.gc.drawRoundRectangle(10, 10, 180, 80, 10, 10);        }    });

?final Canvas canvas = new Canvas(shell, SWT.NULL); final Image image = new Image(display, "icons/eclipse.gif"); canvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Region region = new Region(); // A triangle region. region.add(new int[]{60, 10, 10, 100, 110, 100}); e.gc.setClipping(region); e.gc.drawImage(image, 0, 0); } });?

final Canvas doubleBufferedCanvas = new Canvas(shell, SWT.NO_BACKGROUND); doubleBufferedCanvas.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { // Creates new image only when absolutely necessary. Image image = (Image) doubleBufferedCanvas .getData("double-buffer-image"); if (image == null || image.getBounds().width != canvas.getSize().x || image.getBounds().height != canvas.getSize().y) { image = new Image( display, canvas.getSize().x, canvas.getSize().y); doubleBufferedCanvas.setData("double-buffer-image", image); } // Initializes the graphics context of the image. GC imageGC = new GC(image); imageGC.setBackground(e.gc.getBackground()); imageGC.setForeground(e.gc.getForeground()); imageGC.setFont(e.gc.getFont()); // Fills the background. Rectangle imageSize = image.getBounds(); imageGC.fillRectangle(0, 0, imageSize.width + 1, imageSize.height + 1); // Performs actual drawing here ... imageGC.drawRoundRectangle(10, 10, 200, 100, 5, 5); // Draws the buffer image onto the canvas. e.gc.drawImage(image, 0, 0); imageGC.dispose(); } });?

?

?

?

热点排行