Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » creating image from text
creating image from text [message #540671] Wed, 16 June 2010 18:47 Go to next message
Lidder is currently offline Lidder
Messages: 46
Registered: July 2009
Member
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 Go to previous messageGo to next message
Vijay Raj
Messages: 608
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 Go to previous messageGo to next message
Eclipse User
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 #540973 is a reply to message #540699] Thu, 17 June 2010 16:44 Go to previous messageGo to next message
Lidder is currently offline Lidder
Messages: 46
Registered: July 2009
Member
Changing the imagedata width and height has no effect in my case. I am implementing this for drag and drop. I am changing the image that gets dragged from my tree. I don't understand why it works in your case.
Re: creating image from text [message #540974 is a reply to message #540789] Thu, 17 June 2010 16:48 Go to previous messageGo to next message
Lidder is currently offline Lidder
Messages: 46
Registered: July 2009
Member
Hi Lakshmi,

Thanks for your answer. The problem I have is that the string is not coming from a text box. I am getting it from label provider of my tree for the item that is being dragged. When the item gets dragged, I want to change the image that is shown during the drag operation. By default it is showing the whole row. I want to trim the width to the width of the text string being dragged. How do I get the gc in this case? I have access to the treeviewer and the element that is being dragged.

Like you said changing the imagedata's width and height has no affect on the image itself. I guess at this point I could create a new image with this image data and destroy the old one.

Thanks,
Upkar.
Re: creating image from text [message #541019 is a reply to message #540974] Fri, 18 June 2010 01:55 Go to previous messageGo to next message
Vijay Raj
Messages: 608
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 Smile


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: creating image from text [message #541020 is a reply to message #541019] Fri, 18 June 2010 02:01 Go to previous message
Lidder is currently offline Lidder
Messages: 46
Registered: July 2009
Member
Thanks for the quick answer Vijay Smile.
Previous Topic:Adding animation to Button
Next Topic:Sluggish scrolling of Table widget with custom controls (GTK)
Goto Forum:
  


Current Time: Wed Jul 30 09:14:23 EDT 2014

Powered by FUDForum. Page generated in 0.07508 seconds