SWT&JFace学习笔记1

SWT Graphics and Image Handling

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

  • Control
  • Image
  • Device(Display, Printer)

Drawing Graphics的基本步骤如下:

  1. 从目标Drawable Object上得到Graphic Context 实例
  2. 在Graphic Context上操作drawing
  3. Dispose 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);
        }
    });

 In SWT, graphics objects live in a plane defined by Cartesian coordinates, where the origin is at the topleft corner (0, 0), the x axis increases from left to right, and the y axis increases from top to bottom.

 如果你熟悉Java2D,你可以很容易理解如何使用SWT里的drawing methods

你可以使用SWT的做图API来

  • Drawing Lines, Arcs, and Shapes

  • Filling Shapes

  • Drawing and Copying Images

  • Drawing Text

  • Clipping & XOR & Double Buffering

           Clipping is a technique used to limits the extent of a drawing.

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);
        }
    });
 

           Double buffering works in this way: Instead of drawing graphics directly to the graphics context of a canvas, you draw graphics objects on an image and then draw the image to the canvas.

 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();
        }
    });
 

猜你喜欢

转载自lokinell2006.iteye.com/blog/1029909
今日推荐