Ejemplo n.º 1
0
  /** Creates the "Example" widgets. */
  void createExampleWidgets() {

    /* Compute the widget style */
    int style = getDefaultStyle();
    if (horizontalButton.getSelection()) style |= SWT.H_SCROLL;
    if (verticalButton.getSelection()) style |= SWT.V_SCROLL;
    if (borderButton.getSelection()) style |= SWT.BORDER;
    if (noBackgroundButton.getSelection()) style |= SWT.NO_BACKGROUND;
    if (noFocusButton.getSelection()) style |= SWT.NO_FOCUS;
    if (noMergePaintsButton.getSelection()) style |= SWT.NO_MERGE_PAINTS;
    if (noRedrawResizeButton.getSelection()) style |= SWT.NO_REDRAW_RESIZE;

    /* Create the example widgets */
    paintCount = 0;
    cx = 0;
    cy = 0;
    canvas = new Canvas(canvasGroup, style);
    canvas.addPaintListener(
        new PaintListener() {
          public void paintControl(PaintEvent e) {
            paintCount++;
            GC gc = e.gc;
            if (fillDamageButton.getSelection()) {
              Color color = e.display.getSystemColor(colors[paintCount % colors.length]);
              gc.setBackground(color);
              gc.fillRectangle(e.x, e.y, e.width, e.height);
            }
            Point size = canvas.getSize();
            gc.drawArc(cx + 1, cy + 1, size.x - 2, size.y - 2, 0, 360);
            gc.drawRectangle(cx + (size.x - 10) / 2, cy + (size.y - 10) / 2, 10, 10);
          }
        });
    canvas.addControlListener(
        new ControlAdapter() {
          public void controlResized(ControlEvent event) {
            Point size = canvas.getSize();
            maxX = size.x * 3 / 2;
            maxY = size.y * 3 / 2;
            resizeScrollBars();
          }
        });
    ScrollBar bar = canvas.getHorizontalBar();
    if (bar != null) {
      bar.addSelectionListener(
          new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
              scrollHorizontal((ScrollBar) event.widget);
            }
          });
    }
    bar = canvas.getVerticalBar();
    if (bar != null) {
      bar.addSelectionListener(
          new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
              scrollVertical((ScrollBar) event.widget);
            }
          });
    }
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    final ImageFileNameProvider filenameProvider =
        new ImageFileNameProvider() {
          @Override
          public String getImagePath(int zoom) {
            switch (zoom) {
              case 150:
                return IMAGE_PATH_150;
              case 200:
                return IMAGE_PATH_200;
              default:
                return IMAGE_PATH_100;
            }
          }
        };
    final ImageDataProvider imageDataProvider =
        new ImageDataProvider() {
          @Override
          public ImageData getImageData(int zoom) {
            switch (zoom) {
              case 150:
                return new ImageData(IMAGE_PATH_150);
              case 200:
                return new ImageData(IMAGE_PATH_200);
              default:
                return new ImageData(IMAGE_PATH_100);
            }
          }
        };

    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));

    new Label(shell, SWT.NONE).setText(IMAGE_200 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_200));

    new Label(shell, SWT.NONE).setText(IMAGE_150 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_150));

    new Label(shell, SWT.NONE).setText(IMAGE_100 + ":");
    new Label(shell, SWT.NONE).setImage(new Image(display, IMAGE_PATH_100));

    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

    new Label(shell, SWT.NONE).setText("ImageFileNameProvider:");
    new Label(shell, SWT.NONE).setImage(new Image(display, filenameProvider));

    new Label(shell, SWT.NONE).setText("ImageDataProvider:");
    new Label(shell, SWT.NONE).setImage(new Image(display, imageDataProvider));

    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

    new Label(shell, SWT.NONE).setText("Canvas\n(PaintListener)");
    final Point size = new Point(550, 35);
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.addPaintListener(
        new PaintListener() {
          @Override
          public void paintControl(PaintEvent e) {
            Point size = canvas.getSize();
            paintImage(e.gc, size);
          }
        });
    canvas.setLayoutData(new GridData(size.x, size.y));

    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

    new Label(shell, SWT.NONE).setText("Painted image\n (default resolution)");
    Image image = new Image(display, size.x, size.y);
    GC gc = new GC(image);
    try {
      paintImage(gc, size);
    } finally {
      gc.dispose();
    }
    new Label(shell, SWT.NONE).setImage(image);

    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

    new Label(shell, SWT.NONE).setText("Painted image\n(multi-res, unzoomed paint)");
    new Label(shell, SWT.NONE)
        .setImage(
            new Image(
                display,
                new ImageDataProvider() {
                  @Override
                  public ImageData getImageData(int zoom) {
                    Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100);
                    GC gc = new GC(temp);
                    try {
                      paintImage(gc, size);
                      return temp.getImageData();
                    } finally {
                      gc.dispose();
                      temp.dispose();
                    }
                  }
                }));

    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL)
        .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

    new Label(shell, SWT.NONE).setText("Painted image\n(multi-res, zoomed paint)");
    new Label(shell, SWT.NONE)
        .setImage(
            new Image(
                display,
                new ImageDataProvider() {
                  @Override
                  public ImageData getImageData(int zoom) {
                    Image temp = new Image(display, size.x * zoom / 100, size.y * zoom / 100);
                    GC gc = new GC(temp);
                    try {
                      paintImage2(
                          gc, new Point(size.x * zoom / 100, size.y * zoom / 100), zoom / 100);
                      return temp.getImageData();
                    } finally {
                      gc.dispose();
                      temp.dispose();
                    }
                  }
                }));

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