creating image from text [message #540671] |
Wed, 16 June 2010 18:47  |
Eclipse User |
|
|
|
Hello
I want to create a dynamic image from a java String. This is how I do it
if (text != null)
{
Display display = viewer.getTree().getDisplay();
final Image image = new Image(display, 210, 30);
GC gc = new GC(image);
gc.drawText(text, 0, 0, true);
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.dispose();
event.image = image;
}
The problem is that the image size is too big. How do I set it to the size of the string? I tried changing the imageData after creating the image ...
FontMetrics fm = gc.getFontMetrics();
int height = fm.getHeight();
int averageCharWidth = fm.getAverageCharWidth();
ImageData imageData = image.getImageData();
imageData.height = height;
imageData.width = averageCharWidth * text.length();
but it does not work.
|
|
|
Re: creating image from text [message #540699 is a reply to message #540671] |
Thu, 17 June 2010 02:26   |
Eclipse User |
|
|
|
A very intresting problem.
Here is u r solution,but i did not see any problem with the image size.
package org.eclipse.swt.snippets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Snippet
{
static Display display;
static Shell shell;
public static void main(String[] args)
{
display = new Display();
shell = new Shell(display);
shell.setSize(300, 300);
shell.open();
shell.setLayout(new GridLayout());
final Text textB = new Text(shell, SWT.MULTI | SWT.LEAD | SWT.BORDER);
final Label label = new Label(shell, SWT.NONE);
final GridData layoutData = new GridData();
label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
label.setLayoutData(layoutData);
textB.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
textB.addModifyListener(new ModifyListener()
{
@Override
public void modifyText(ModifyEvent e)
{
String text = textB.getText();
if (text != null)
{
Display display = Display.getDefault();
final Image image = new Image(display, 500, 500);
GC gc = new GC(image);
FontMetrics fm = gc.getFontMetrics();
int height = fm.getHeight();
int averageCharWidth = fm.getAverageCharWidth();
String[] split = text.split(Text.DELIMITER);
int maxl = getMaxLength(split);
ImageData imageData = image.getImageData();
imageData.height = height * split.length;
imageData.width = averageCharWidth * maxl;
int y = 0;
for (String string : split)
{
gc.drawText(string, 0, y, true);
y += height;
}
System.out.println(text);
gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
gc.dispose();
label.setImage(image);
layoutData.widthHint = imageData.width;
layoutData.heightHint = imageData.height;
shell.layout();
}
}
private int getMaxLength(String[] split)
{
int max = 0;
for (String string : split)
{
max = Math.max(string.length(), max);
}
return max;
}
});
shell.layout();
while (!shell.isDisposed())
{
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
One problem remains,if the text size goes beyond 500,500,
it will not be able to draw that.
the problem is finding the image size before creating the image.
the gc can only give fontmatric and for gc we need image.
first we can create a dummy gc and get the fontmatric and then create a new one with actual image size.
NOTE:text box is only for getting the text
[Updated on: Thu, 17 June 2010 08:17] by Moderator
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03692 seconds