Java Tutorial/SWT/TextLayout — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:21, 31 мая 2010

Draw wrapped text using TextLayout

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Draw wrapped text using TextLayout
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutWrapText {
  final static String longString = "The preferred size of a widget is the minimum size needed to show its content. In the case of a Composite, the preferred size is the smallest rectangle that contains all of its children. If children have been positioned by the application, the Composite computes its own preferred size based on the size and position of the children. If a Composite is using a layout class to position its children, it asks the Layout to compute the size of its clientArea, and then it adds in the trim to determine its preferred size.";
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final TextLayout layout = new TextLayout(display);
    layout.setText(longString);
    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        switch (event.type) {
        case SWT.Paint:
          layout.draw(event.gc, 10, 10);
          break;
        case SWT.Resize:
          layout.setWidth(shell.getSize().x - 20);
          break;
        }
      }
    };
    shell.addListener(SWT.Paint, listener);
    shell.addListener(SWT.Resize, listener);
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Embed image and controls in TextLayout

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * TextLayout example snippet: using the GlyphMetrics to embedded images in 
 * a TextLayout. 
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.GlyphMetrics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutImageControl {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Embedding objects in text");
    final Image[] images = { new Image(display, 32, 32), new Image(display, 20, 40),
        new Image(display, 40, 20) };
    int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_MAGENTA, SWT.COLOR_GREEN };
    for (int i = 0; i < images.length; i++) {
      GC gc = new GC(images[i]);
      gc.setBackground(display.getSystemColor(colors[i]));
      gc.fillRectangle(images[i].getBounds());
      gc.dispose();
    }
    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");
    button.pack();
    String text = "Here is some text with a blue image \uFFFC, a magenta image \uFFFC, a green image \uFFFC, and a button: \uFFFC.";
    final int[] imageOffsets = { 36, 55, 72 };
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    for (int i = 0; i < images.length; i++) {
      Rectangle bounds = images[i].getBounds();
      TextStyle imageStyle = new TextStyle(null, null, null);
      imageStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
      layout.setStyle(imageStyle, imageOffsets[i], imageOffsets[i]);
    }
    Rectangle bounds = button.getBounds();
    TextStyle buttonStyle = new TextStyle(null, null, null);
    buttonStyle.metrics = new GlyphMetrics(bounds.height, 0, bounds.width);
    final int buttonOffset = text.length() - 2;
    layout.setStyle(buttonStyle, buttonOffset, buttonOffset);
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        GC gc = event.gc;
        Point margin = new Point(10, 10);
        layout.setWidth(shell.getClientArea().width - 2 * margin.x);
        layout.draw(event.gc, margin.x, margin.y);
        for (int i = 0; i < images.length; i++) {
          int offset = imageOffsets[i];
          int lineIndex = layout.getLineIndex(offset);
          FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
          Point point = layout.getLocation(offset, false);
          GlyphMetrics glyphMetrics = layout.getStyle(offset).metrics;
          gc.drawImage(images[i], point.x + margin.x, point.y + margin.y + lineMetrics.getAscent()
              - glyphMetrics.ascent);
        }
        int lineIndex = layout.getLineIndex(buttonOffset);
        FontMetrics lineMetrics = layout.getLineMetrics(lineIndex);
        Point point = layout.getLocation(buttonOffset, false);
        GlyphMetrics glyphMetrics = layout.getStyle(buttonOffset).metrics;
        button.setLocation(point.x + margin.x, point.y + margin.y + lineMetrics.getAscent()
            - glyphMetrics.ascent);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    layout.dispose();
    for (int i = 0; i < images.length; i++) {
      images[i].dispose();
    }
    display.dispose();
  }
}





Subscript and superscript: using the rise field of a TextStyle

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * TextLayout example snippet: using the rise field of a TextStyle.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutSubscriptSuperscript {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Modify Rise");
    FontData data = display.getSystemFont().getFontData()[0];
    Font font = new Font(display, data.getName(), 24, SWT.NORMAL);
    Font smallFont = new Font(display, data.getName(), 8, SWT.NORMAL);
    GC gc = new GC(shell);
    gc.setFont(smallFont);
    FontMetrics smallMetrics = gc.getFontMetrics();
    final int smallBaseline = smallMetrics.getAscent() + smallMetrics.getLeading();
    gc.setFont(font);
    FontMetrics metrics = gc.getFontMetrics();
    final int baseline = metrics.getAscent() + metrics.getLeading();
    gc.dispose();
    final TextLayout layout0 = new TextLayout(display);
    layout0.setText("SubscriptScriptSuperscript");
    layout0.setFont(font);
    TextStyle subscript0 = new TextStyle(smallFont, null, null);
    TextStyle superscript0 = new TextStyle(smallFont, null, null);
    superscript0.rise = baseline - smallBaseline;
    layout0.setStyle(subscript0, 0, 8);
    layout0.setStyle(superscript0, 15, 25);
    final TextLayout layout1 = new TextLayout(display);
    layout1.setText("SubscriptScriptSuperscript");
    layout1.setFont(font);
    TextStyle subscript1 = new TextStyle(smallFont, null, null);
    subscript1.rise = -smallBaseline;
    TextStyle superscript1 = new TextStyle(smallFont, null, null);
    superscript1.rise = baseline;
    layout1.setStyle(subscript1, 0, 8);
    layout1.setStyle(superscript1, 15, 25);
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        Display display = event.display;
        GC gc = event.gc;
        Rectangle rect0 = layout0.getBounds();
        rect0.x += 10;
        rect0.y += 10;
        gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
        gc.fillRectangle(rect0);
        layout0.draw(gc, rect0.x, rect0.y);
        gc.setForeground(display.getSystemColor(SWT.COLOR_MAGENTA));
        gc.drawLine(rect0.x, rect0.y, rect0.x + rect0.width, rect0.y);
        gc.drawLine(rect0.x, rect0.y + baseline, rect0.x + rect0.width, rect0.y + baseline);
        gc.drawLine(rect0.x + rect0.width / 2, rect0.y, rect0.x + rect0.width / 2, rect0.y
            + rect0.height);
        Rectangle rect1 = layout1.getBounds();
        rect1.x += 10;
        rect1.y += 20 + rect0.height;
        gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
        gc.fillRectangle(rect1);
        layout1.draw(gc, rect1.x, rect1.y);
        gc.setForeground(display.getSystemColor(SWT.COLOR_MAGENTA));
        gc.drawLine(rect1.x, rect1.y + smallBaseline, rect1.x + rect1.width, rect1.y
            + smallBaseline);
        gc.drawLine(rect1.x, rect1.y + baseline + smallBaseline, rect1.x + rect1.width, rect1.y
            + baseline + smallBaseline);
        gc.drawLine(rect1.x + rect1.width / 2, rect1.y, rect1.x + rect1.width / 2, rect1.y
            + rect1.height);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    layout0.dispose();
    layout1.dispose();
    smallFont.dispose();
    font.dispose();
    display.dispose();
  }
}





TextLayout: Font, Style and Color

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * TextLayout example snippet: draw internationalized styled text on a shell
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutStyleColor {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Font font1 = new Font(display, "Tahoma", 14, SWT.BOLD);
    Font font2 = new Font(display, "MS Mincho", 20, SWT.ITALIC);
    Font font3 = new Font(display, "Arabic Transparent", 18, SWT.NORMAL);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Color green = display.getSystemColor(SWT.COLOR_GREEN);
    Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
    Color gray = display.getSystemColor(SWT.COLOR_GRAY);
    final TextLayout layout = new TextLayout(display);
    TextStyle style1 = new TextStyle(font1, yellow, blue);
    TextStyle style2 = new TextStyle(font2, green, null);
    TextStyle style3 = new TextStyle(font3, blue, gray);
    layout.setText("English 1234567890asdfasdfasdf");
    layout.setStyle(style1, 0, 6);
    layout.setStyle(style2, 8, 10);
    layout.setStyle(style3, 13, 21);
    shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        layout.draw(event.gc, 10, 10);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    font1.dispose();
    font2.dispose();
    font3.dispose();
    layout.dispose();
    display.dispose();
  }
}





TextLayout: strikeout

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutStrikeout {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Underline, Strike Out");
    Font font = shell.getFont();
    String text = "text.text.text.";
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    TextStyle style1 = new TextStyle(font, null, null);
    style1.strikeout = true;
    layout.setStyle(style1, 3, 5);
    
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        Point point = new Point(10, 10);
        int width = shell.getClientArea().width - 2 * point.x;
        layout.setWidth(width);
        layout.draw(event.gc, point.x, point.y);
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    layout.dispose();
    display.dispose();
  }
}





TextLayout: underline

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutUnderline {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Underline, Strike Out");
    Font font = shell.getFont();
    String text = "text.text.text.";
    final TextLayout layout = new TextLayout(display);
    layout.setText(text);
    TextStyle style1 = new TextStyle(font, null, null);
    style1.underline = true;
    layout.setStyle(style1, 3, 5);
    
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        Point point = new Point(10, 10);
        int width = shell.getClientArea().width - 2 * point.x;
        layout.setWidth(width);
        layout.draw(event.gc, point.x, point.y);
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    layout.dispose();
    display.dispose();
  }
}





TextLayout Unicode

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * TextLayout example snippet: draw internationalized styled text on a shell
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutUnicode {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Font font1 = new Font(display, "Tahoma", 14, SWT.BOLD);
    Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
    
    final TextLayout layout = new TextLayout(display);
    TextStyle style1 = new TextStyle(font1, yellow, blue);
    layout.setText("English \u65E5\u672C\u8A9E  \u0627\u0644\u0639\u0631\u0628\u064A\u0651\u0629");
    layout.setStyle(style1, 8, 21);
    shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        layout.draw(event.gc, 10, 10);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    font1.dispose();
    layout.dispose();
    display.dispose();
  }
}





TextLayout: using TextLayout justify, alignment and indent

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * TextLayout example snippet: using TextLayout justify, alignment and indent 
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.TextLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class TextLayoutJustifyAlignmentIndent {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setText("Indent, Justify, Align");
    String[] texts = {
        "Plans do not materialize out of nowhere, nor are they entirely static. To ensure the planning process is transparent and open to the entire Eclipse community, we (the Eclipse PMC) post plans in an embryonic form and revise them throughout the release cycle.",
        "The first part of the plan deals with the important matters of release deliverables, release milestones, target operating environments, and release-to-release compatibility. These are all things that need to be clear for any release, even if no features were to change.",
        "The remainder of the plan consists of plan items for the various Eclipse subprojects. Each plan item covers a feature or API that is to be added to Eclipse, or some aspect of Eclipse that is to be improved. Each plan item has its own entry in the Eclipse bugzilla database, with a title and a concise summary (usually a single paragraph) that explains the work item at a suitably high enough level so that everyone can readily understand what the work item is without having to understand the nitty-gritty detail.", };
    int[] alignments = { SWT.LEFT, SWT.CENTER, SWT.RIGHT };
    
    final TextLayout[] layouts = new TextLayout[texts.length];
    
    for (int i = 0; i < layouts.length; i++) {
      TextLayout layout = new TextLayout(display);
      layout.setText(texts[i]);
      layout.setIndent(30);
      layout.setJustify(true);
      layout.setAlignment(alignments[i]);
      layouts[i] = layout;
    }
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event event) {
        Point point = new Point(10, 10);
        int width = shell.getClientArea().width - 2 * point.x;
        for (int i = 0; i < layouts.length; i++) {
          TextLayout layout = layouts[i];
          layout.setWidth(width);
          layout.draw(event.gc, point.x, point.y);
          point.y += layout.getBounds().height + 10;
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    for (int i = 0; i < layouts.length; i++) {
      layouts[i].dispose();
    }
    display.dispose();
  }
}