|
| Re: creating image from text [message #540699 is a reply to message #540671] |
Thu, 17 June 2010 02:26   |
Vijay Raj Messages: 604 Registered: July 2009 |
Senior Member |
|
|
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
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
[Updated on: Thu, 17 June 2010 08:17] Report message to a moderator
|
|
|
| Re: creating image from text [message #540789 is a reply to message #540671] |
Thu, 17 June 2010 07:23   |
|
Originally posted by: lakshmip.xxx.com
Lidder wrote:
> 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.
Hi Lidder,
Modifying the ImageData returned by image.getImageData() will not affect
the image.
You could try to set the required size while creating the image itself.
1) Measure the size of the text string before creating the Image.
GC gc = new GC(TextBox);
Point size = gc.textExtent(text);
gc.dispose();
2) then use size.x and size.y as width and height while creating the Image.
HTH,
Lakshmi Shanmugam
|
|
|
|
|
| Re: creating image from text [message #541019 is a reply to message #540974] |
Fri, 18 June 2010 01:55   |
Vijay Raj Messages: 604 Registered: July 2009 |
Senior Member |
|
|
Pls see the note added.
the textbox label contraption was only for achieving the solution,
they are not the part of the solution,whichout them also u can create an image and save it in the file system.
in u r case also u can create GC anywhere,
and about image i think u have to do some trial and error.
if already have a image e.image,discard that,create a new one and assignit to u r e.image....
or just copy past u r code and we may debug it
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
|
|
|
|
Powered by
FUDForum. Page generated in 0.01843 seconds