Beispiel #1
0
 /** Loads the image resources. */
 public void initResources() {
   final Class clazz = PaintExample.class;
   if (PaintExample.resourceBundle != null)
     try {
       for (int i = 0; i < PaintExample.tools.length; ++i) {
         Tool tool = PaintExample.tools[i];
         String id = tool.group + '.' + tool.name;
         InputStream sourceStream =
             clazz.getResourceAsStream(PaintExample.getResourceString(id + ".image"));
         ImageData source = new ImageData(sourceStream);
         ImageData mask = source.getTransparencyMask();
         tool.image = new Image(null, source, mask);
         try {
           sourceStream.close();
         } catch (IOException e) {
           e.printStackTrace();
         }
       }
       return;
     } catch (Throwable t) {
     }
   String error =
       PaintExample.resourceBundle != null
           ? PaintExample.getResourceString("error.CouldNotLoadResources")
           : "Unable to load resources";
   freeResources();
   throw new RuntimeException(error);
 }
Beispiel #2
0
 /**
  * Gets a string from the resource bundle and binds it with the given arguments. If the key is not
  * found, return the key.
  */
 public static String getResourceString(String key, Object[] args) {
   try {
     return MessageFormat.format(PaintExample.getResourceString(key), args);
   } catch (MissingResourceException e) {
     return key;
   } catch (NullPointerException e) {
     return "!" + key + "!";
   }
 }
Beispiel #3
0
 public static void main(java.lang.String[] args) {
   org.eclipse.swt.widgets.Display display = new org.eclipse.swt.widgets.Display();
   org.eclipse.swt.widgets.Shell shell = new org.eclipse.swt.widgets.Shell(display);
   shell.setText(getResourceString("window.title"));
   shell.setLayout(new org.eclipse.swt.layout.GridLayout());
   org.eclipse.swt.examples.paint.PaintExample instance =
       new org.eclipse.swt.examples.paint.PaintExample(shell);
   instance.createToolBar(shell);
   org.eclipse.swt.widgets.Composite composite =
       new org.eclipse.swt.widgets.Composite(shell, SWT.NONE);
   composite.setLayout(new org.eclipse.swt.layout.FillLayout());
   composite.setLayoutData(new org.eclipse.swt.layout.GridData(SWT.FILL, SWT.FILL, true, true));
   instance.createGUI(composite);
   instance.setDefaults();
   setShellSize(display, shell);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   instance.dispose();
 }
Beispiel #4
0
 /** Adds a tool item to the toolbar. Note: Only called by standalone. */
 private ToolItem addToolItem(final ToolBar toolbar, final Tool tool) {
   final String id = tool.group + '.' + tool.name;
   ToolItem item = new ToolItem(toolbar, tool.type);
   item.setText(PaintExample.getResourceString(id + ".label"));
   item.setToolTipText(PaintExample.getResourceString(id + ".tooltip"));
   item.setImage(tool.image);
   item.addSelectionListener(
       new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
           tool.action.run();
         }
       });
   final int childID = toolbar.indexOf(item);
   toolbar
       .getAccessible()
       .addAccessibleListener(
           new AccessibleAdapter() {
             public void getName(org.eclipse.swt.accessibility.AccessibleEvent e) {
               if (e.childID == childID)
                 e.result = PaintExample.getResourceString(id + ".description");
             }
           });
   return item;
 }
Beispiel #5
0
 /** Invokes as a standalone program. */
 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setText(PaintExample.getResourceString("window.title"));
   shell.setLayout(new GridLayout());
   PaintExample instance = new PaintExample(shell);
   instance.createToolBar(shell);
   Composite composite = new Composite(shell, SWT.NONE);
   composite.setLayout(new FillLayout());
   composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
   instance.createGUI(composite);
   instance.setDefaults();
   PaintExample.setShellSize(display, shell);
   shell.open();
   while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep();
   instance.dispose();
 }
 /**
  * Returns name associated with this tool.
  *
  * @return the localized name of this tool
  */
 @Override
 public String getDisplayName() {
   return PaintExample.getResourceString("tool.Rectangle.label");
 }
Beispiel #7
0
  /** Creates the GUI. */
  public void createGUI(Composite parent) {
    GridLayout gridLayout;
    GridData gridData;

    /** * Create principal GUI layout elements ** */
    Composite displayArea = new Composite(parent, SWT.NONE);
    gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    displayArea.setLayout(gridLayout);

    // Creating these elements here avoids the need to instantiate the GUI elements
    // in strict layout order. The natural layout ordering is an artifact of using
    // SWT layouts, but unfortunately it is not the same order as that required to
    // instantiate all of the non-GUI application elements to satisfy referential
    // dependencies. It is possible to reorder the initialization to some extent, but
    // this can be very tedious.

    // paint canvas
    final Canvas paintCanvas =
        new Canvas(
            displayArea,
            SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
    paintCanvas.setLayoutData(gridData);
    paintCanvas.setBackground(paintColorWhite);

    // color selector frame
    final Composite colorFrame = new Composite(displayArea, SWT.NONE);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
    colorFrame.setLayoutData(gridData);

    // tool settings frame
    final Composite toolSettingsFrame = new Composite(displayArea, SWT.NONE);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
    toolSettingsFrame.setLayoutData(gridData);

    // status text
    final Text statusText = new Text(displayArea, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
    gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
    statusText.setLayoutData(gridData);

    /** * Create the remaining application elements inside the principal GUI layout elements ** */
    // paintSurface
    paintSurface = new PaintSurface(paintCanvas, statusText, paintColorWhite);

    // finish initializing the tool data
    PaintExample.tools[PaintExample.Pencil_tool].data = new PencilTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.Airbrush_tool].data =
        new AirbrushTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.Line_tool].data = new LineTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.PolyLine_tool].data =
        new PolyLineTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.Rectangle_tool].data =
        new RectangleTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.RoundedRectangle_tool].data =
        new RoundedRectangleTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.Ellipse_tool].data =
        new EllipseTool(toolSettings, paintSurface);
    PaintExample.tools[PaintExample.Text_tool].data = new TextTool(toolSettings, paintSurface);

    // colorFrame
    gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    colorFrame.setLayout(gridLayout);

    // activeForegroundColorCanvas, activeBackgroundColorCanvas
    activeForegroundColorCanvas = new Canvas(colorFrame, SWT.BORDER);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.heightHint = 24;
    gridData.widthHint = 24;
    activeForegroundColorCanvas.setLayoutData(gridData);

    activeBackgroundColorCanvas = new Canvas(colorFrame, SWT.BORDER);
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.heightHint = 24;
    gridData.widthHint = 24;
    activeBackgroundColorCanvas.setLayoutData(gridData);

    // paletteCanvas
    final Canvas paletteCanvas = new Canvas(colorFrame, SWT.BORDER | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL_HORIZONTAL);
    gridData.heightHint = 24;
    paletteCanvas.setLayoutData(gridData);
    paletteCanvas.addListener(
        SWT.MouseDown,
        new Listener() {
          public void handleEvent(Event e) {
            Rectangle bounds = paletteCanvas.getClientArea();
            Color color = getColorAt(bounds, e.x, e.y);

            if (e.button == 1) setForegroundColor(color);
            else setBackgroundColor(color);
          }

          private Color getColorAt(Rectangle bounds, int x, int y) {
            if (bounds.height <= 1 && bounds.width <= 1) return paintColorWhite;
            final int row = (y - bounds.y) * PaintExample.numPaletteRows / bounds.height;
            final int col = (x - bounds.x) * PaintExample.numPaletteCols / bounds.width;
            return paintColors[
                Math.min(
                    Math.max(row * PaintExample.numPaletteCols + col, 0), paintColors.length - 1)];
          }
        });
    Listener refreshListener =
        new Listener() {
          public void handleEvent(Event e) {
            if (e.gc == null) return;
            Rectangle bounds = paletteCanvas.getClientArea();
            for (int row = 0; row < PaintExample.numPaletteRows; ++row)
              for (int col = 0; col < PaintExample.numPaletteCols; ++col) {
                final int x = bounds.width * col / PaintExample.numPaletteCols;
                final int y = bounds.height * row / PaintExample.numPaletteRows;
                final int width =
                    Math.max(bounds.width * (col + 1) / PaintExample.numPaletteCols - x, 1);
                final int height =
                    Math.max(bounds.height * (row + 1) / PaintExample.numPaletteRows - y, 1);
                e.gc.setBackground(paintColors[row * PaintExample.numPaletteCols + col]);
                e.gc.fillRectangle(bounds.x + x, bounds.y + y, width, height);
              }
          }
        };
    paletteCanvas.addListener(SWT.Resize, refreshListener);
    paletteCanvas.addListener(SWT.Paint, refreshListener);
    // paletteCanvas.redraw();

    // toolSettingsFrame
    gridLayout = new GridLayout();
    gridLayout.numColumns = 4;
    gridLayout.marginHeight = 0;
    gridLayout.marginWidth = 0;
    toolSettingsFrame.setLayout(gridLayout);

    Label label = new Label(toolSettingsFrame, SWT.NONE);
    label.setText(PaintExample.getResourceString("settings.AirbrushRadius.text"));

    final Scale airbrushRadiusScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL);
    airbrushRadiusScale.setMinimum(5);
    airbrushRadiusScale.setMaximum(50);
    airbrushRadiusScale.setSelection(toolSettings.airbrushRadius);
    airbrushRadiusScale.setLayoutData(
        new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
    airbrushRadiusScale.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            toolSettings.airbrushRadius = airbrushRadiusScale.getSelection();
            updateToolSettings();
          }
        });

    label = new Label(toolSettingsFrame, SWT.NONE);
    label.setText(PaintExample.getResourceString("settings.AirbrushIntensity.text"));

    final Scale airbrushIntensityScale = new Scale(toolSettingsFrame, SWT.HORIZONTAL);
    airbrushIntensityScale.setMinimum(1);
    airbrushIntensityScale.setMaximum(100);
    airbrushIntensityScale.setSelection(toolSettings.airbrushIntensity);
    airbrushIntensityScale.setLayoutData(
        new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL));
    airbrushIntensityScale.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            toolSettings.airbrushIntensity = airbrushIntensityScale.getSelection();
            updateToolSettings();
          }
        });
  }