public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setLayout(new FillLayout());
   final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
   styledText.setText(text);
   FontData data = display.getSystemFont().getFontData()[0];
   Font font = new Font(display, data.getName(), 16, SWT.BOLD);
   styledText.setFont(font);
   styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
   styledText.addListener(
       SWT.Resize,
       new Listener() {
         public void handleEvent(Event event) {
           Rectangle rect = styledText.getClientArea();
           Image newImage = new Image(display, 1, Math.max(1, rect.height));
           GC gc = new GC(newImage);
           gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
           gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
           gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true);
           gc.dispose();
           styledText.setBackgroundImage(newImage);
           if (oldImage != null) oldImage.dispose();
           oldImage = newImage;
         }
       });
   shell.setSize(700, 400);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) display.sleep();
   }
   if (oldImage != null) oldImage.dispose();
   font.dispose();
   display.dispose();
 }
Esempio n. 2
0
 /**
  * Releases any internal resources back to the operating system and clears all fields except the
  * device handle.
  *
  * <p>When a device is destroyed, resources that were acquired on behalf of the programmer need to
  * be returned to the operating system. For example, if the device allocated a font to be used as
  * the system font, this font would be freed in <code>release</code>. Also,to assist the garbage
  * collector and minimize the amount of memory that is not reclaimed when the programmer keeps a
  * reference to a disposed device, all fields except the handle are zero'd. The handle is needed
  * by <code>destroy</code>. This method is called before <code>destroy</code>.
  *
  * <p>If subclasses reimplement this method, they must call the <code>super</code> implementation.
  *
  * @see #dispose
  * @see #destroy
  */
 protected void release() {
   for (int i = 0; i < colors.length; i++) {
     if (colors[i] != null) colors[i].dispose();
   }
   colors = null;
   if (systemFont != null) systemFont.dispose();
   systemFont = null;
   if (tracking) {
     synchronized (trackingLock) {
       if (TRACK & objects != null) {
         for (int i = 0; i < objects.length; i++) {
           if (objects[i] != null) {
             System.err.println(objects[i]);
             errors[i].printStackTrace();
           }
         }
       }
     }
   }
 }
  private static void paintImage2(GC gc, Point size, int f) {
    gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    gc.fillRectangle(0, 0, size.x, size.y);

    // Scale line width, corner roundness, and font size.
    // Caveat: line width expands in all directions, so the origin also has to move.

    gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION));
    gc.fillRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f);

    gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    gc.setLineWidth(f);
    gc.drawRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f);
    FontData fontData = gc.getFont().getFontData()[0];
    fontData.setHeight(fontData.getHeight() * f);
    Font font = new Font(gc.getDevice(), fontData);
    try {
      gc.setFont(font);
      gc.drawText(fontData.toString(), 10 * f, 10 * f, true);
    } finally {
      font.dispose();
    }
  }