Java 将带有styleranges的SWT StyledText内容转换为图像

Java 将带有styleranges的SWT StyledText内容转换为图像,java,swt,styledtext,Java,Swt,Styledtext,我想将Styledtext(带有StyleRanges)的内容打印/转换为图像。我不知道该怎么对付 任何人请引导我这样做。给你: public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setText("Widget"); shell.setLayout(new Grid

我想将Styledtext(带有StyleRanges)的内容打印/转换为图像。我不知道该怎么对付

任何人请引导我这样做。

给你:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Widget");
    shell.setLayout(new GridLayout(1, false));

    final StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("Text to capture");
    text.setStyleRange(new StyleRange(0, text.getText().length(), display.getSystemColor(SWT.COLOR_RED), display.getSystemColor(SWT.COLOR_BLACK)));
    text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.addListener(SWT.Selection, new Listener()
    {
        public void handleEvent(Event event)
        {
            Point tableSize = text.getSize();
            GC gc = new GC(text);
            final Image image = new Image(display, tableSize.x, tableSize.y);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[] {image.getImageData()};
            loader.save("swt.png", SWT.IMAGE_PNG);

            image.dispose();
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
这就是
Shell
的外观:

这就是拍摄的图像的样子:

给你:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Widget");
    shell.setLayout(new GridLayout(1, false));

    final StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("Text to capture");
    text.setStyleRange(new StyleRange(0, text.getText().length(), display.getSystemColor(SWT.COLOR_RED), display.getSystemColor(SWT.COLOR_BLACK)));
    text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.addListener(SWT.Selection, new Listener()
    {
        public void handleEvent(Event event)
        {
            Point tableSize = text.getSize();
            GC gc = new GC(text);
            final Image image = new Image(display, tableSize.x, tableSize.y);
            gc.copyArea(image, 0, 0);
            gc.dispose();

            ImageLoader loader = new ImageLoader();
            loader.data = new ImageData[] {image.getImageData()};
            loader.save("swt.png", SWT.IMAGE_PNG);

            image.dispose();
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
这就是
Shell
的外观:

这就是拍摄的图像的样子:


亲爱的Baz,非常感谢您的代码。。我可以将StyledText转换为图像,但如果内容更多,并且使用了scrollpane,则只打印暴露的内容。@CarlJohn实现这一点并不容易。您可以尝试使用
GC
将其自己“绘制”成
图像,但这听起来需要做很多工作。亲爱的Baz,非常感谢您提供的代码。。我可以将StyledText转换为图像,但如果内容更多,并且使用了scrollpane,则只打印暴露的内容。@CarlJohn实现这一点并不容易。您可以尝试使用
GC
将其自己“绘制”到
图像中,但这听起来需要做很多工作。