Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Map problem
Map problem [message #446922] |
Mon, 06 December 2004 13:58 |
hortiz Mising name Messages: 96 Registered: July 2009 |
Member |
|
|
hi,
I've got a problem while trying to map a point from one coordinate system
to another.
The code below illustrates my problem (don't pay attention to width and
height hints) : I would like to know the y coordinate of the bottom left
corner of the third composite in the coordinate system defined by :
1. the composite named "second composite",
2. then the composite named "first composite".
Could you give me the correct uses of the map function for these cases ?
Thanks a lot
Helene
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MapComposites {
private static Display display;
private static Shell shell;
public static void createComposites() {
Composite firstComposite = new Composite(shell, SWT.BORDER);
GridData gridData = new GridData();
gridData.widthHint = 800;
gridData.heightHint = 400;
firstComposite.setLayoutData(gridData);
firstComposite.setLayout(new GridLayout());
Label label = new Label(firstComposite, SWT.NONE);
label.setText("First composite");
label.setLayoutData(new GridData());
firstComposite.setBackground(new Color(display, new RGB(205, 0,
0)));
Composite secondComposite = new Composite(firstComposite,
SWT.BORDER);
gridData = new GridData();
gridData.widthHint = 600;
gridData.heightHint = 300;
secondComposite.setLayoutData(gridData);
secondComposite.setLayout(new GridLayout());
Label label2 = new Label(secondComposite, SWT.NONE);
label2.setText("Second composite");
label2.setLayoutData(new GridData());
secondComposite.setBackground(new Color(display, new RGB(155, 0,
0)));
Composite thirdComposite = new Composite(secondComposite,
SWT.BORDER);
gridData = new GridData();
gridData.verticalAlignment = GridData.FILL_BOTH;
gridData.widthHint = 300;
gridData.heightHint = 150;
thirdComposite.setLayoutData(gridData);
thirdComposite.setLayout(new GridLayout());
Label label3 = new Label(thirdComposite, SWT.NONE);
label3.setText("Third composite");
label3.setLayoutData(new GridData());
thirdComposite.setBackground(new Color(display, new RGB(105, 0,
0)));
}
public static void main(String[] args) {
display = new Display ();
shell = new Shell (display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
shell.setLayout(gridLayout);
shell.setBackground(new Color(display, new RGB(255, 0, 0)));
createComposites();
shell.pack();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
display.dispose ();
}
}
|
|
|
Re: Map problem [message #446983 is a reply to message #446922] |
Tue, 07 December 2004 13:14 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
See println's below. Note that you were leaking color resources in you
rexample.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
shell.setLayout(gridLayout);
Color color1 = new Color(display, 255, 0, 0);
shell.setBackground(color1);
Composite firstComposite = new Composite(shell, SWT.BORDER);
GridData gridData = new GridData();
gridData.widthHint = 800;
gridData.heightHint = 400;
firstComposite.setLayoutData(gridData);
firstComposite.setLayout(new GridLayout());
Label label = new Label(firstComposite, SWT.NONE);
label.setText("First composite");
label.setLayoutData(new GridData());
Color color2 = new Color(display, 205, 0, 0);
firstComposite.setBackground(color2);
Composite secondComposite = new Composite(firstComposite,
SWT.BORDER);
gridData = new GridData();
gridData.widthHint = 600;
gridData.heightHint = 300;
secondComposite.setLayoutData(gridData);
secondComposite.setLayout(new GridLayout());
Label label2 = new Label(secondComposite, SWT.NONE);
label2.setText("Second composite");
label2.setLayoutData(new GridData());
Color color3 = new Color(display, 155, 0, 0);
secondComposite.setBackground(color3);
Composite thirdComposite = new Composite(secondComposite,
SWT.BORDER);
gridData = new GridData();
gridData.verticalAlignment = GridData.FILL_BOTH;
gridData.widthHint = 300;
gridData.heightHint = 150;
thirdComposite.setLayoutData(gridData);
thirdComposite.setLayout(new GridLayout());
Label label3 = new Label(thirdComposite, SWT.NONE);
label3.setText("Third composite");
label3.setLayoutData(new GridData());
Color color4 = new Color(display, 105, 0, 0);
thirdComposite.setBackground(color4);
shell.pack();
shell.open();
Rectangle thirdBounds = thirdComposite.getBounds(); // bounds are
relative to parent of thirdComposite which is secondComposite
System.out.println("y coordinate of the bottom left corner of the
third composite in the coordinate system defined by the composite named
second composite = "+
(thirdBounds.y+thirdBounds.height));
Point pt = display.map(secondComposite, firstComposite,
thirdBounds.x, thirdBounds.y+thirdBounds.height);
System.out.println("y coordinate of the bottom left corner of the
third composite in the coordinate system defined by the composite named
first composite = "+
pt.y);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
color1.dispose();
color2.dispose();
color3.dispose();
color4.dispose();
display.dispose();
}
"hortiz" <hortiz@nospam.com> wrote in message
news:cp1oha$38j$1@www.eclipse.org...
> hi,
>
> I've got a problem while trying to map a point from one coordinate system
> to another.
>
> The code below illustrates my problem (don't pay attention to width and
> height hints) : I would like to know the y coordinate of the bottom left
> corner of the third composite in the coordinate system defined by :
> 1. the composite named "second composite",
>
> 2. then the composite named "first composite".
>
> Could you give me the correct uses of the map function for these cases ?
> Thanks a lot
>
> Helene
>
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Color;
> import org.eclipse.swt.graphics.RGB;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;
>
> public class MapComposites {
> private static Display display;
> private static Shell shell;
> public static void createComposites() {
>
> Composite firstComposite = new Composite(shell, SWT.BORDER);
> GridData gridData = new GridData();
> gridData.widthHint = 800;
> gridData.heightHint = 400;
> firstComposite.setLayoutData(gridData);
> firstComposite.setLayout(new GridLayout());
> Label label = new Label(firstComposite, SWT.NONE);
> label.setText("First composite");
> label.setLayoutData(new GridData());
> firstComposite.setBackground(new Color(display, new RGB(205, 0,
> 0)));
> Composite secondComposite = new Composite(firstComposite,
> SWT.BORDER);
> gridData = new GridData();
> gridData.widthHint = 600;
> gridData.heightHint = 300;
> secondComposite.setLayoutData(gridData);
> secondComposite.setLayout(new GridLayout());
> Label label2 = new Label(secondComposite, SWT.NONE);
> label2.setText("Second composite");
> label2.setLayoutData(new GridData());
> secondComposite.setBackground(new Color(display, new RGB(155, 0,
> 0)));
> Composite thirdComposite = new Composite(secondComposite,
> SWT.BORDER);
> gridData = new GridData();
> gridData.verticalAlignment = GridData.FILL_BOTH;
> gridData.widthHint = 300;
> gridData.heightHint = 150;
> thirdComposite.setLayoutData(gridData);
> thirdComposite.setLayout(new GridLayout());
> Label label3 = new Label(thirdComposite, SWT.NONE);
> label3.setText("Third composite");
> label3.setLayoutData(new GridData());
> thirdComposite.setBackground(new Color(display, new RGB(105, 0,
> 0)));
> }
> public static void main(String[] args) {
> display = new Display ();
> shell = new Shell (display);
>
> GridLayout gridLayout = new GridLayout();
> gridLayout.numColumns = 1;
> shell.setLayout(gridLayout);
> shell.setBackground(new Color(display, new RGB(255, 0, 0)));
>
> createComposites();
>
> shell.pack();
> shell.open();
>
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ())
> display.sleep ();
> }
> display.dispose ();
> }
> }
>
>
|
|
|
Re: Map problem [message #446994 is a reply to message #446983] |
Tue, 07 December 2004 14:19 |
hortiz Mising name Messages: 96 Registered: July 2009 |
Member |
|
|
Hi Veronika,
Thanks for your answer.
I've just post another question about it as I've not seen your answer yet.
What do you mean by "you were leaking color resources in your example." ?
Helene
Veronika Irvine wrote:
> See println's below. Note that you were leaking color resources in you
> rexample.
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> GridLayout gridLayout = new GridLayout();
> gridLayout.numColumns = 1;
> shell.setLayout(gridLayout);
> Color color1 = new Color(display, 255, 0, 0);
> shell.setBackground(color1);
> Composite firstComposite = new Composite(shell, SWT.BORDER);
> GridData gridData = new GridData();
> gridData.widthHint = 800;
> gridData.heightHint = 400;
> firstComposite.setLayoutData(gridData);
> firstComposite.setLayout(new GridLayout());
> Label label = new Label(firstComposite, SWT.NONE);
> label.setText("First composite");
> label.setLayoutData(new GridData());
> Color color2 = new Color(display, 205, 0, 0);
> firstComposite.setBackground(color2);
> Composite secondComposite = new Composite(firstComposite,
> SWT.BORDER);
> gridData = new GridData();
> gridData.widthHint = 600;
> gridData.heightHint = 300;
> secondComposite.setLayoutData(gridData);
> secondComposite.setLayout(new GridLayout());
> Label label2 = new Label(secondComposite, SWT.NONE);
> label2.setText("Second composite");
> label2.setLayoutData(new GridData());
> Color color3 = new Color(display, 155, 0, 0);
> secondComposite.setBackground(color3);
> Composite thirdComposite = new Composite(secondComposite,
> SWT.BORDER);
> gridData = new GridData();
> gridData.verticalAlignment = GridData.FILL_BOTH;
> gridData.widthHint = 300;
> gridData.heightHint = 150;
> thirdComposite.setLayoutData(gridData);
> thirdComposite.setLayout(new GridLayout());
> Label label3 = new Label(thirdComposite, SWT.NONE);
> label3.setText("Third composite");
> label3.setLayoutData(new GridData());
> Color color4 = new Color(display, 105, 0, 0);
> thirdComposite.setBackground(color4);
> shell.pack();
> shell.open();
> Rectangle thirdBounds = thirdComposite.getBounds(); // bounds are
> relative to parent of thirdComposite which is secondComposite
> System.out.println("y coordinate of the bottom left corner of the
> third composite in the coordinate system defined by the composite named
> second composite = "+
> (thirdBounds.y+thirdBounds.height));
> Point pt = display.map(secondComposite, firstComposite,
> thirdBounds.x, thirdBounds.y+thirdBounds.height);
> System.out.println("y coordinate of the bottom left corner of the
> third composite in the coordinate system defined by the composite named
> first composite = "+
> pt.y);
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> color1.dispose();
> color2.dispose();
> color3.dispose();
> color4.dispose();
> display.dispose();
> }
> "hortiz" <hortiz@nospam.com> wrote in message
> news:cp1oha$38j$1@www.eclipse.org...
>> hi,
>>
>> I've got a problem while trying to map a point from one coordinate system
>> to another.
>>
>> The code below illustrates my problem (don't pay attention to width and
>> height hints) : I would like to know the y coordinate of the bottom left
>> corner of the third composite in the coordinate system defined by :
>> 1. the composite named "second composite",
>>
>> 2. then the composite named "first composite".
>>
>> Could you give me the correct uses of the map function for these cases ?
>> Thanks a lot
>>
>> Helene
>>
>>
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.graphics.Color;
>> import org.eclipse.swt.graphics.RGB;
>> import org.eclipse.swt.layout.GridData;
>> import org.eclipse.swt.layout.GridLayout;
>> import org.eclipse.swt.widgets.Composite;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Label;
>> import org.eclipse.swt.widgets.Shell;
>>
>> public class MapComposites {
>> private static Display display;
>> private static Shell shell;
>> public static void createComposites() {
>>
>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>> GridData gridData = new GridData();
>> gridData.widthHint = 800;
>> gridData.heightHint = 400;
>> firstComposite.setLayoutData(gridData);
>> firstComposite.setLayout(new GridLayout());
>> Label label = new Label(firstComposite, SWT.NONE);
>> label.setText("First composite");
>> label.setLayoutData(new GridData());
>> firstComposite.setBackground(new Color(display, new RGB(205, 0,
>> 0)));
>> Composite secondComposite = new Composite(firstComposite,
>> SWT.BORDER);
>> gridData = new GridData();
>> gridData.widthHint = 600;
>> gridData.heightHint = 300;
>> secondComposite.setLayoutData(gridData);
>> secondComposite.setLayout(new GridLayout());
>> Label label2 = new Label(secondComposite, SWT.NONE);
>> label2.setText("Second composite");
>> label2.setLayoutData(new GridData());
>> secondComposite.setBackground(new Color(display, new RGB(155, 0,
>> 0)));
>> Composite thirdComposite = new Composite(secondComposite,
>> SWT.BORDER);
>> gridData = new GridData();
>> gridData.verticalAlignment = GridData.FILL_BOTH;
>> gridData.widthHint = 300;
>> gridData.heightHint = 150;
>> thirdComposite.setLayoutData(gridData);
>> thirdComposite.setLayout(new GridLayout());
>> Label label3 = new Label(thirdComposite, SWT.NONE);
>> label3.setText("Third composite");
>> label3.setLayoutData(new GridData());
>> thirdComposite.setBackground(new Color(display, new RGB(105, 0,
>> 0)));
>> }
>> public static void main(String[] args) {
>> display = new Display ();
>> shell = new Shell (display);
>>
>> GridLayout gridLayout = new GridLayout();
>> gridLayout.numColumns = 1;
>> shell.setLayout(gridLayout);
>> shell.setBackground(new Color(display, new RGB(255, 0, 0)));
>>
>> createComposites();
>>
>> shell.pack();
>> shell.open();
>>
>> while (!shell.isDisposed ()) {
>> if (!display.readAndDispatch ())
>> display.sleep ();
>> }
>> display.dispose ();
>> }
>> }
>>
>>
|
|
|
Re: Map problem [message #447007 is a reply to message #446994] |
Tue, 07 December 2004 17:32 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
Your example has code like:
firstComposite.setBackground(new Color(display, new RGB(205, 0, 0)));
You have created a Color object which is a system resource and you have not
kept a reference to it so that you can dispose of it when you are done.
This is bad.
See:
http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml
"hortiz" <hortiz@nospam.com> wrote in message
news:cp4e57$n9r$1@www.eclipse.org...
> Hi Veronika,
>
> Thanks for your answer.
> I've just post another question about it as I've not seen your answer yet.
> What do you mean by "you were leaking color resources in your example." ?
>
> Helene
>
> Veronika Irvine wrote:
>
>> See println's below. Note that you were leaking color resources in you
>> rexample.
>
>> public static void main(String[] args) {
>
>> Display display = new Display();
>> Shell shell = new Shell(display);
>
>> GridLayout gridLayout = new GridLayout();
>> gridLayout.numColumns = 1;
>> shell.setLayout(gridLayout);
>> Color color1 = new Color(display, 255, 0, 0);
>> shell.setBackground(color1);
>
>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>> GridData gridData = new GridData();
>> gridData.widthHint = 800;
>> gridData.heightHint = 400;
>> firstComposite.setLayoutData(gridData);
>> firstComposite.setLayout(new GridLayout());
>> Label label = new Label(firstComposite, SWT.NONE);
>> label.setText("First composite");
>> label.setLayoutData(new GridData());
>> Color color2 = new Color(display, 205, 0, 0);
>> firstComposite.setBackground(color2);
>
>> Composite secondComposite = new Composite(firstComposite,
>> SWT.BORDER);
>> gridData = new GridData();
>> gridData.widthHint = 600;
>> gridData.heightHint = 300;
>> secondComposite.setLayoutData(gridData);
>> secondComposite.setLayout(new GridLayout());
>> Label label2 = new Label(secondComposite, SWT.NONE);
>> label2.setText("Second composite");
>> label2.setLayoutData(new GridData());
>> Color color3 = new Color(display, 155, 0, 0);
>> secondComposite.setBackground(color3);
>
>> Composite thirdComposite = new Composite(secondComposite,
>> SWT.BORDER);
>> gridData = new GridData();
>> gridData.verticalAlignment = GridData.FILL_BOTH;
>> gridData.widthHint = 300;
>> gridData.heightHint = 150;
>> thirdComposite.setLayoutData(gridData);
>> thirdComposite.setLayout(new GridLayout());
>> Label label3 = new Label(thirdComposite, SWT.NONE);
>> label3.setText("Third composite");
>> label3.setLayoutData(new GridData());
>> Color color4 = new Color(display, 105, 0, 0);
>> thirdComposite.setBackground(color4);
>
>> shell.pack();
>> shell.open();
>
>> Rectangle thirdBounds = thirdComposite.getBounds(); // bounds are
>> relative to parent of thirdComposite which is secondComposite
>> System.out.println("y coordinate of the bottom left corner of the
>> third composite in the coordinate system defined by the composite named
>> second composite = "+
>> (thirdBounds.y+thirdBounds.height));
>
>> Point pt = display.map(secondComposite, firstComposite,
>> thirdBounds.x, thirdBounds.y+thirdBounds.height);
>> System.out.println("y coordinate of the bottom left corner of the
>> third composite in the coordinate system defined by the composite named
>> first composite = "+
>> pt.y);
>
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch())
>> display.sleep();
>> }
>> color1.dispose();
>> color2.dispose();
>> color3.dispose();
>> color4.dispose();
>> display.dispose();
>> }
>> "hortiz" <hortiz@nospam.com> wrote in message
>> news:cp1oha$38j$1@www.eclipse.org...
>>> hi,
>>>
>>> I've got a problem while trying to map a point from one coordinate
>>> system to another.
>>>
>>> The code below illustrates my problem (don't pay attention to width and
>>> height hints) : I would like to know the y coordinate of the bottom left
>>> corner of the third composite in the coordinate system defined by :
>>> 1. the composite named "second composite",
>>>
>>> 2. then the composite named "first composite".
>>>
>>> Could you give me the correct uses of the map function for these cases ?
>>> Thanks a lot
>>>
>>> Helene
>>>
>>>
>>> import org.eclipse.swt.SWT;
>>> import org.eclipse.swt.graphics.Color;
>>> import org.eclipse.swt.graphics.RGB;
>>> import org.eclipse.swt.layout.GridData;
>>> import org.eclipse.swt.layout.GridLayout;
>>> import org.eclipse.swt.widgets.Composite;
>>> import org.eclipse.swt.widgets.Display;
>>> import org.eclipse.swt.widgets.Label;
>>> import org.eclipse.swt.widgets.Shell;
>>>
>>> public class MapComposites {
>>> private static Display display;
>>> private static Shell shell;
>>> public static void createComposites() {
>>>
>>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>>> GridData gridData = new GridData();
>>> gridData.widthHint = 800;
>>> gridData.heightHint = 400;
>>> firstComposite.setLayoutData(gridData);
>>> firstComposite.setLayout(new GridLayout());
>>> Label label = new Label(firstComposite, SWT.NONE);
>>> label.setText("First composite");
>>> label.setLayoutData(new GridData());
>>> firstComposite.setBackground(new Color(display, new RGB(205, 0,
>>> 0)));
>>> Composite secondComposite = new Composite(firstComposite,
>>> SWT.BORDER);
>>> gridData = new GridData();
>>> gridData.widthHint = 600;
>>> gridData.heightHint = 300;
>>> secondComposite.setLayoutData(gridData);
>>> secondComposite.setLayout(new GridLayout());
>>> Label label2 = new Label(secondComposite, SWT.NONE);
>>> label2.setText("Second composite");
>>> label2.setLayoutData(new GridData());
>>> secondComposite.setBackground(new Color(display, new RGB(155, 0,
>>> 0)));
>>> Composite thirdComposite = new Composite(secondComposite,
>>> SWT.BORDER);
>>> gridData = new GridData();
>>> gridData.verticalAlignment = GridData.FILL_BOTH;
>>> gridData.widthHint = 300;
>>> gridData.heightHint = 150;
>>> thirdComposite.setLayoutData(gridData);
>>> thirdComposite.setLayout(new GridLayout());
>>> Label label3 = new Label(thirdComposite, SWT.NONE);
>>> label3.setText("Third composite");
>>> label3.setLayoutData(new GridData());
>>> thirdComposite.setBackground(new Color(display, new RGB(105, 0,
>>> 0)));
>>> }
>>> public static void main(String[] args) {
>>> display = new Display ();
>>> shell = new Shell (display);
>>>
>>> GridLayout gridLayout = new GridLayout();
>>> gridLayout.numColumns = 1;
>>> shell.setLayout(gridLayout);
>>> shell.setBackground(new Color(display, new RGB(255, 0, 0)));
>>>
>>> createComposites();
>>>
>>> shell.pack();
>>> shell.open();
>>>
>>> while (!shell.isDisposed ()) {
>>> if (!display.readAndDispatch ())
>>> display.sleep ();
>>> }
>>> display.dispose ();
>>> }
>>> }
>>>
>>>
>
>
|
|
|
Re: Map problem [message #447065 is a reply to message #447007] |
Wed, 08 December 2004 09:31 |
hortiz Mising name Messages: 96 Registered: July 2009 |
Member |
|
|
Ok, this code was just an example to show the problem I had with map
function.
In my "real" application, I use a class called ColorFactory which defined
standard colors used in the whole program :
private static final RGB backgroundRGB = new RGB(220,220,220);
private static final RGB commandBackgroundRGB = new RGB(255,255,255);
private static final RGB buttonBackgroundRGB = new RGB(220,220,220);
...
For each of these colors, I defined a get function :
public static Color getBackgroundColor() {
return new Color(ColorFactory.device, backgroundRGB);
}
which I call like this :
label.setBackground(ColorFactory.getBackgroundColor());
Where should I dispose them to respect dispose policy ?
Thanks,
Helene
Veronika Irvine wrote:
> Your example has code like:
> firstComposite.setBackground(new Color(display, new RGB(205, 0, 0)));
> You have created a Color object which is a system resource and you have not
> kept a reference to it so that you can dispose of it when you are done.
> This is bad.
> See:
> http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml
> "hortiz" <hortiz@nospam.com> wrote in message
> news:cp4e57$n9r$1@www.eclipse.org...
>> Hi Veronika,
>>
>> Thanks for your answer.
>> I've just post another question about it as I've not seen your answer yet.
>> What do you mean by "you were leaking color resources in your example." ?
>>
>> Helene
>>
>> Veronika Irvine wrote:
>>
>>> See println's below. Note that you were leaking color resources in you
>>> rexample.
>>
>>> public static void main(String[] args) {
>>
>>> Display display = new Display();
>>> Shell shell = new Shell(display);
>>
>>> GridLayout gridLayout = new GridLayout();
>>> gridLayout.numColumns = 1;
>>> shell.setLayout(gridLayout);
>>> Color color1 = new Color(display, 255, 0, 0);
>>> shell.setBackground(color1);
>>
>>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>>> GridData gridData = new GridData();
>>> gridData.widthHint = 800;
>>> gridData.heightHint = 400;
>>> firstComposite.setLayoutData(gridData);
>>> firstComposite.setLayout(new GridLayout());
>>> Label label = new Label(firstComposite, SWT.NONE);
>>> label.setText("First composite");
>>> label.setLayoutData(new GridData());
>>> Color color2 = new Color(display, 205, 0, 0);
>>> firstComposite.setBackground(color2);
>>
>>> Composite secondComposite = new Composite(firstComposite,
>>> SWT.BORDER);
>>> gridData = new GridData();
>>> gridData.widthHint = 600;
>>> gridData.heightHint = 300;
>>> secondComposite.setLayoutData(gridData);
>>> secondComposite.setLayout(new GridLayout());
>>> Label label2 = new Label(secondComposite, SWT.NONE);
>>> label2.setText("Second composite");
>>> label2.setLayoutData(new GridData());
>>> Color color3 = new Color(display, 155, 0, 0);
>>> secondComposite.setBackground(color3);
>>
>>> Composite thirdComposite = new Composite(secondComposite,
>>> SWT.BORDER);
>>> gridData = new GridData();
>>> gridData.verticalAlignment = GridData.FILL_BOTH;
>>> gridData.widthHint = 300;
>>> gridData.heightHint = 150;
>>> thirdComposite.setLayoutData(gridData);
>>> thirdComposite.setLayout(new GridLayout());
>>> Label label3 = new Label(thirdComposite, SWT.NONE);
>>> label3.setText("Third composite");
>>> label3.setLayoutData(new GridData());
>>> Color color4 = new Color(display, 105, 0, 0);
>>> thirdComposite.setBackground(color4);
>>
>>> shell.pack();
>>> shell.open();
>>
>>> Rectangle thirdBounds = thirdComposite.getBounds(); // bounds are
>>> relative to parent of thirdComposite which is secondComposite
>>> System.out.println("y coordinate of the bottom left corner of the
>>> third composite in the coordinate system defined by the composite named
>>> second composite = "+
>>> (thirdBounds.y+thirdBounds.height));
>>
>>> Point pt = display.map(secondComposite, firstComposite,
>>> thirdBounds.x, thirdBounds.y+thirdBounds.height);
>>> System.out.println("y coordinate of the bottom left corner of the
>>> third composite in the coordinate system defined by the composite named
>>> first composite = "+
>>> pt.y);
>>
>>> while (!shell.isDisposed()) {
>>> if (!display.readAndDispatch())
>>> display.sleep();
>>> }
>>> color1.dispose();
>>> color2.dispose();
>>> color3.dispose();
>>> color4.dispose();
>>> display.dispose();
>>> }
>>> "hortiz" <hortiz@nospam.com> wrote in message
>>> news:cp1oha$38j$1@www.eclipse.org...
>>>> hi,
>>>>
>>>> I've got a problem while trying to map a point from one coordinate
>>>> system to another.
>>>>
>>>> The code below illustrates my problem (don't pay attention to width and
>>>> height hints) : I would like to know the y coordinate of the bottom left
>>>> corner of the third composite in the coordinate system defined by :
>>>> 1. the composite named "second composite",
>>>>
>>>> 2. then the composite named "first composite".
>>>>
>>>> Could you give me the correct uses of the map function for these cases ?
>>>> Thanks a lot
>>>>
>>>> Helene
>>>>
>>>>
>>>> import org.eclipse.swt.SWT;
>>>> import org.eclipse.swt.graphics.Color;
>>>> import org.eclipse.swt.graphics.RGB;
>>>> import org.eclipse.swt.layout.GridData;
>>>> import org.eclipse.swt.layout.GridLayout;
>>>> import org.eclipse.swt.widgets.Composite;
>>>> import org.eclipse.swt.widgets.Display;
>>>> import org.eclipse.swt.widgets.Label;
>>>> import org.eclipse.swt.widgets.Shell;
>>>>
>>>> public class MapComposites {
>>>> private static Display display;
>>>> private static Shell shell;
>>>> public static void createComposites() {
>>>>
>>>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>>>> GridData gridData = new GridData();
>>>> gridData.widthHint = 800;
>>>> gridData.heightHint = 400;
>>>> firstComposite.setLayoutData(gridData);
>>>> firstComposite.setLayout(new GridLayout());
>>>> Label label = new Label(firstComposite, SWT.NONE);
>>>> label.setText("First composite");
>>>> label.setLayoutData(new GridData());
>>>> firstComposite.setBackground(new Color(display, new RGB(205, 0,
>>>> 0)));
>>>> Composite secondComposite = new Composite(firstComposite,
>>>> SWT.BORDER);
>>>> gridData = new GridData();
>>>> gridData.widthHint = 600;
>>>> gridData.heightHint = 300;
>>>> secondComposite.setLayoutData(gridData);
>>>> secondComposite.setLayout(new GridLayout());
>>>> Label label2 = new Label(secondComposite, SWT.NONE);
>>>> label2.setText("Second composite");
>>>> label2.setLayoutData(new GridData());
>>>> secondComposite.setBackground(new Color(display, new RGB(155, 0,
>>>> 0)));
>>>> Composite thirdComposite = new Composite(secondComposite,
>>>> SWT.BORDER);
>>>> gridData = new GridData();
>>>> gridData.verticalAlignment = GridData.FILL_BOTH;
>>>> gridData.widthHint = 300;
>>>> gridData.heightHint = 150;
>>>> thirdComposite.setLayoutData(gridData);
>>>> thirdComposite.setLayout(new GridLayout());
>>>> Label label3 = new Label(thirdComposite, SWT.NONE);
>>>> label3.setText("Third composite");
>>>> label3.setLayoutData(new GridData());
>>>> thirdComposite.setBackground(new Color(display, new RGB(105, 0,
>>>> 0)));
>>>> }
>>>> public static void main(String[] args) {
>>>> display = new Display ();
>>>> shell = new Shell (display);
>>>>
>>>> GridLayout gridLayout = new GridLayout();
>>>> gridLayout.numColumns = 1;
>>>> shell.setLayout(gridLayout);
>>>> shell.setBackground(new Color(display, new RGB(255, 0, 0)));
>>>>
>>>> createComposites();
>>>>
>>>> shell.pack();
>>>> shell.open();
>>>>
>>>> while (!shell.isDisposed ()) {
>>>> if (!display.readAndDispatch ())
>>>> display.sleep ();
>>>> }
>>>> display.dispose ();
>>>> }
>>>> }
>>>>
>>>>
>>
>>
|
|
|
Re: Map problem [message #447070 is a reply to message #447065] |
Wed, 08 December 2004 11:44 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
You should dispose them when you know they are no longer required.
"hortiz" <hortiz@nospam.com> wrote in message
news:cp6hm0$jsh$1@www.eclipse.org...
> Ok, this code was just an example to show the problem I had with map
> function.
> In my "real" application, I use a class called ColorFactory which defined
> standard colors used in the whole program :
>
> private static final RGB backgroundRGB = new RGB(220,220,220); private
> static final RGB commandBackgroundRGB = new RGB(255,255,255);
> private static final RGB buttonBackgroundRGB = new RGB(220,220,220); ...
>
> For each of these colors, I defined a get function :
>
> public static Color getBackgroundColor() {
> return new Color(ColorFactory.device, backgroundRGB);
> }
>
> which I call like this :
> label.setBackground(ColorFactory.getBackgroundColor());
>
> Where should I dispose them to respect dispose policy ?
> Thanks,
> Helene
>
>
>
> Veronika Irvine wrote:
>
>> Your example has code like:
>
>> firstComposite.setBackground(new Color(display, new RGB(205, 0, 0)));
>
>> You have created a Color object which is a system resource and you have
>> not kept a reference to it so that you can dispose of it when you are
>> done. This is bad.
>
>> See:
>
>> http://www.eclipse.org/articles/swt-design-2/swt-design-2.ht ml
>
>
>> "hortiz" <hortiz@nospam.com> wrote in message
>> news:cp4e57$n9r$1@www.eclipse.org...
>>> Hi Veronika,
>>>
>>> Thanks for your answer.
>>> I've just post another question about it as I've not seen your answer
>>> yet.
>>> What do you mean by "you were leaking color resources in your example."
>>> ?
>>>
>>> Helene
>>>
>>> Veronika Irvine wrote:
>>>
>>>> See println's below. Note that you were leaking color resources in you
>>>> rexample.
>>>
>>>> public static void main(String[] args) {
>>>
>>>> Display display = new Display();
>>>> Shell shell = new Shell(display);
>>>
>>>> GridLayout gridLayout = new GridLayout();
>>>> gridLayout.numColumns = 1;
>>>> shell.setLayout(gridLayout);
>>>> Color color1 = new Color(display, 255, 0, 0);
>>>> shell.setBackground(color1);
>>>
>>>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>>>> GridData gridData = new GridData();
>>>> gridData.widthHint = 800;
>>>> gridData.heightHint = 400;
>>>> firstComposite.setLayoutData(gridData);
>>>> firstComposite.setLayout(new GridLayout());
>>>> Label label = new Label(firstComposite, SWT.NONE);
>>>> label.setText("First composite");
>>>> label.setLayoutData(new GridData());
>>>> Color color2 = new Color(display, 205, 0, 0);
>>>> firstComposite.setBackground(color2);
>>>
>>>> Composite secondComposite = new Composite(firstComposite,
>>>> SWT.BORDER);
>>>> gridData = new GridData();
>>>> gridData.widthHint = 600;
>>>> gridData.heightHint = 300;
>>>> secondComposite.setLayoutData(gridData);
>>>> secondComposite.setLayout(new GridLayout());
>>>> Label label2 = new Label(secondComposite, SWT.NONE);
>>>> label2.setText("Second composite");
>>>> label2.setLayoutData(new GridData());
>>>> Color color3 = new Color(display, 155, 0, 0);
>>>> secondComposite.setBackground(color3);
>>>
>>>> Composite thirdComposite = new Composite(secondComposite,
>>>> SWT.BORDER);
>>>> gridData = new GridData();
>>>> gridData.verticalAlignment = GridData.FILL_BOTH;
>>>> gridData.widthHint = 300;
>>>> gridData.heightHint = 150;
>>>> thirdComposite.setLayoutData(gridData);
>>>> thirdComposite.setLayout(new GridLayout());
>>>> Label label3 = new Label(thirdComposite, SWT.NONE);
>>>> label3.setText("Third composite");
>>>> label3.setLayoutData(new GridData());
>>>> Color color4 = new Color(display, 105, 0, 0);
>>>> thirdComposite.setBackground(color4);
>>>
>>>> shell.pack();
>>>> shell.open();
>>>
>>>> Rectangle thirdBounds = thirdComposite.getBounds(); // bounds
>>>> are relative to parent of thirdComposite which is secondComposite
>>>> System.out.println("y coordinate of the bottom left corner of
>>>> the third composite in the coordinate system defined by the composite
>>>> named second composite = "+
>>>> (thirdBounds.y+thirdBounds.height));
>>>
>>>> Point pt = display.map(secondComposite, firstComposite,
>>>> thirdBounds.x, thirdBounds.y+thirdBounds.height);
>>>> System.out.println("y coordinate of the bottom left corner of
>>>> the third composite in the coordinate system defined by the composite
>>>> named first composite = "+
>>>> pt.y);
>>>
>>>> while (!shell.isDisposed()) {
>>>> if (!display.readAndDispatch())
>>>> display.sleep();
>>>> }
>>>> color1.dispose();
>>>> color2.dispose();
>>>> color3.dispose();
>>>> color4.dispose();
>>>> display.dispose();
>>>> }
>>>> "hortiz" <hortiz@nospam.com> wrote in message
>>>> news:cp1oha$38j$1@www.eclipse.org...
>>>>> hi,
>>>>>
>>>>> I've got a problem while trying to map a point from one coordinate
>>>>> system to another.
>>>>>
>>>>> The code below illustrates my problem (don't pay attention to width
>>>>> and height hints) : I would like to know the y coordinate of the
>>>>> bottom left corner of the third composite in the coordinate system
>>>>> defined by :
>>>>> 1. the composite named "second composite",
>>>>>
>>>>> 2. then the composite named "first composite".
>>>>>
>>>>> Could you give me the correct uses of the map function for these cases
>>>>> ?
>>>>> Thanks a lot
>>>>>
>>>>> Helene
>>>>>
>>>>>
>>>>> import org.eclipse.swt.SWT;
>>>>> import org.eclipse.swt.graphics.Color;
>>>>> import org.eclipse.swt.graphics.RGB;
>>>>> import org.eclipse.swt.layout.GridData;
>>>>> import org.eclipse.swt.layout.GridLayout;
>>>>> import org.eclipse.swt.widgets.Composite;
>>>>> import org.eclipse.swt.widgets.Display;
>>>>> import org.eclipse.swt.widgets.Label;
>>>>> import org.eclipse.swt.widgets.Shell;
>>>>>
>>>>> public class MapComposites {
>>>>> private static Display display;
>>>>> private static Shell shell;
>>>>> public static void createComposites() {
>>>>>
>>>>> Composite firstComposite = new Composite(shell, SWT.BORDER);
>>>>> GridData gridData = new GridData();
>>>>> gridData.widthHint = 800;
>>>>> gridData.heightHint = 400;
>>>>> firstComposite.setLayoutData(gridData);
>>>>> firstComposite.setLayout(new GridLayout());
>>>>> Label label = new Label(firstComposite, SWT.NONE);
>>>>> label.setText("First composite");
>>>>> label.setLayoutData(new GridData());
>>>>> firstComposite.setBackground(new Color(display, new RGB(205, 0,
>>>>> 0)));
>>>>> Composite secondComposite = new Composite(firstComposite,
>>>>> SWT.BORDER);
>>>>> gridData = new GridData();
>>>>> gridData.widthHint = 600;
>>>>> gridData.heightHint = 300;
>>>>> secondComposite.setLayoutData(gridData);
>>>>> secondComposite.setLayout(new GridLayout());
>>>>> Label label2 = new Label(secondComposite, SWT.NONE);
>>>>> label2.setText("Second composite");
>>>>> label2.setLayoutData(new GridData());
>>>>> secondComposite.setBackground(new Color(display, new RGB(155,
>>>>> 0, 0)));
>>>>> Composite thirdComposite = new Composite(secondComposite,
>>>>> SWT.BORDER);
>>>>> gridData = new GridData();
>>>>> gridData.verticalAlignment = GridData.FILL_BOTH;
>>>>> gridData.widthHint = 300;
>>>>> gridData.heightHint = 150;
>>>>> thirdComposite.setLayoutData(gridData);
>>>>> thirdComposite.setLayout(new GridLayout());
>>>>> Label label3 = new Label(thirdComposite, SWT.NONE);
>>>>> label3.setText("Third composite");
>>>>> label3.setLayoutData(new GridData());
>>>>> thirdComposite.setBackground(new Color(display, new RGB(105, 0,
>>>>> 0)));
>>>>> }
>>>>> public static void main(String[] args) {
>>>>> display = new Display ();
>>>>> shell = new Shell (display);
>>>>>
>>>>> GridLayout gridLayout = new GridLayout();
>>>>> gridLayout.numColumns = 1;
>>>>> shell.setLayout(gridLayout);
>>>>> shell.setBackground(new Color(display, new RGB(255, 0, 0)));
>>>>>
>>>>> createComposites();
>>>>>
>>>>> shell.pack();
>>>>> shell.open();
>>>>>
>>>>> while (!shell.isDisposed ()) {
>>>>> if (!display.readAndDispatch ())
>>>>> display.sleep ();
>>>>> }
>>>>> display.dispose ();
>>>>> }
>>>>> }
>>>>>
>>>>>
>>>
>>>
>
>
|
|
|
Goto Forum:
Current Time: Tue Sep 24 17:02:15 GMT 2024
Powered by FUDForum. Page generated in 0.04170 seconds
|