Home » Eclipse Projects » GEF » Draw2d: Drawing Image in Thumbnail yelds NullPointerException
Draw2d: Drawing Image in Thumbnail yelds NullPointerException [message #19164] |
Mon, 09 September 2002 15:58  |
Eclipse User |
|
|
|
I strongly suspect that I'm going about this the wrong way. If so,
somebody please point me in the right direction.
I'm trying to draw an Image in a Figure. The closest Draw2d object I've
found for doing this is Thumbnail, and here's my test code for doing
this (based on the layout manager demo) with my minor changes hilighted:
public static Image image =
ImageDescriptor.createFromFile(Draw2dImage.class,
"icons/notes.png").createImage();
public static void main(String args[]){
Shell shell = new Shell();
shell.open();
shell.setText("Draw2d");
LightweightSystem lws = new LightweightSystem(shell);
IFigure panel = new Figure();
panel.setLayoutManager(new FlowLayout());
lws.setContents(panel);
Clickable button = new Button("Click me");
Clickable checkbox = new CheckBox("Check box");
Shape ellipse = new Ellipse();
ellipse.setBackgroundColor(ColorConstants.yellow);
Shape rectangle = new RectangleFigure();
rectangle.setBackgroundColor(ColorConstants.lightBlue);
// My new code is *HERE*--->>
Figure thumbnail = new Thumbnail() {
protected Image getThumbnailImage() {
return image;
}
};
panel.add(button);
panel.add(checkbox);
panel.add(ellipse);
panel.add(rectangle);
panel.add(thumbnail); // and *HERE* <<---
Display display = Display.getDefault();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
}
There *must* be an easier/better way than this?? Thank in advance for
your help.
Regards,
Dave
--
Dave Orme
Advanced Systems Concepts
http://www.asc-iseries.com
|
|
| |
Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerException [message #19169 is a reply to message #19167] |
Mon, 09 September 2002 16:42   |
Eclipse User |
|
|
|
David J. Orme wrote:
> David J. Orme wrote:
>
>> I strongly suspect that I'm going about this the wrong way. If so,
>> somebody please point me in the right direction.
>> <<snip>>
After taking a closer look at class Thumbnail, I noticed that there is a
setSource() method, making me think that this is probably the intended
way to use Thumbnail, not the way I was using Thumbnail. Since I
haven't been able to find a Figure that just represents a bitmapped
Image, I decided to try creating one. Here is the essence of my first
attempt:
1) I extended Figure and created a field with getters/setters for my
Image object.
2) I overrode the following methods:
/**
* @see org.eclipse.draw2d.Figure#getPreferredSize()
*/
public Dimension getPreferredSize() {
if (image != null) {
Rectangle rect = image.getBounds();
return new Dimension(rect.width, rect.height);
} else {
return new Dimension(0, 0);
}
}
/**
* @see org.eclipse.draw2d.Figure#paintClientArea(Graphics)
*/
protected void paintClientArea(Graphics g) {
g.drawImage(image, 0, 0);
}
I can verify that both methods run. The dimension being returned is
Dimension(16, 16), which is correct for the image I'm loading, so the
image appears to be loaded correctly.
My problem is that nothing gets displayed. Here is the new display code
(boiled down for compactness):
public static Image image =
ImageDescriptor.createFromFile(Draw2dImage.class,
"icons/notes.png").createImage();
public static void main(String args[]){
Shell shell = new Shell();
shell.open();
shell.setText("Draw2d");
LightweightSystem lws = new LightweightSystem(shell);
IFigure panel = new Figure();
panel.setLayoutManager(new FlowLayout());
lws.setContents(panel);
Figure bitmap = new Bitmap(image);
panel.add(bitmap);
Display display = Display.getDefault();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ())
display.sleep ();
}
}
--
Dave Orme
Advanced Systems Concepts
http://www.asc-iseries.com
|
|
|
Re: Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerExcep [message #19173 is a reply to message #19169] |
Mon, 09 September 2002 16:51   |
Eclipse User |
|
|
|
Have you tried to use a Label? You can create a Draw2d label with an image.
I'm not quite sure what you're trying to do, but I thought I'd bring it up.
Eric
"David J. Orme" <daveo@asc-iseries.com> wrote in message
news:3D7D07CC.6040003@asc-iseries.com...
> David J. Orme wrote:
> > David J. Orme wrote:
> >
> >> I strongly suspect that I'm going about this the wrong way. If so,
> >> somebody please point me in the right direction.
> >> <<snip>>
>
> After taking a closer look at class Thumbnail, I noticed that there is a
> setSource() method, making me think that this is probably the intended
> way to use Thumbnail, not the way I was using Thumbnail. Since I
> haven't been able to find a Figure that just represents a bitmapped
> Image, I decided to try creating one. Here is the essence of my first
> attempt:
>
> 1) I extended Figure and created a field with getters/setters for my
> Image object.
>
> 2) I overrode the following methods:
>
> /**
> * @see org.eclipse.draw2d.Figure#getPreferredSize()
> */
> public Dimension getPreferredSize() {
> if (image != null) {
> Rectangle rect = image.getBounds();
> return new Dimension(rect.width, rect.height);
> } else {
> return new Dimension(0, 0);
> }
> }
>
> /**
> * @see org.eclipse.draw2d.Figure#paintClientArea(Graphics)
> */
> protected void paintClientArea(Graphics g) {
> g.drawImage(image, 0, 0);
> }
>
> I can verify that both methods run. The dimension being returned is
> Dimension(16, 16), which is correct for the image I'm loading, so the
> image appears to be loaded correctly.
>
> My problem is that nothing gets displayed. Here is the new display code
> (boiled down for compactness):
>
> public static Image image =
> ImageDescriptor.createFromFile(Draw2dImage.class,
> "icons/notes.png").createImage();
>
> public static void main(String args[]){
> Shell shell = new Shell();
> shell.open();
> shell.setText("Draw2d");
> LightweightSystem lws = new LightweightSystem(shell);
> IFigure panel = new Figure();
> panel.setLayoutManager(new FlowLayout());
> lws.setContents(panel);
>
> Figure bitmap = new Bitmap(image);
>
> panel.add(bitmap);
>
> Display display = Display.getDefault();
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ())
> display.sleep ();
> }
> }
>
>
> --
> Dave Orme
> Advanced Systems Concepts
> http://www.asc-iseries.com
>
|
|
| | |
Re: Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerExcep [message #19182 is a reply to message #19169] |
Mon, 09 September 2002 16:58   |
Eclipse User |
|
|
|
Originally posted by: hudsonr.spam.com
> /**
> * @see org.eclipse.draw2d.Figure#paintClientArea(Graphics)
> */
> protected void paintClientArea(Graphics g) {
> g.drawImage(image, 0, 0);
> }
draw2d does not default to a relative coordinate system (therefore, absolute
coordinates are the default). Also, it is intended that you override
paintFigure(). Therefore, this method should read:
protected void paintFigure(Graphics g) {
Rectangle bounds = getBounds();
g.drawImage(image, bounds.x, bounds.y);
}
I have not run your code, so there may be additional issues. There is a
class called Label which will display an Image. It can also display text,
but you can use it just for an Image. Use Label, I just thought I'd point
out these problems anyway.
-Randy
[For those really interested in coordinate systems:]
Even if you override useLocalCoordinates for your subclass of Figure, it is
still necessary to paint the Image at bounds.x, bounds.y. This is for
consistency. A superclass can implement paintFigure(), and a subclass may
decide to use local coordinates without breaking the inherited behavior.
|
|
| | |
Re: Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerExcep [message #19194 is a reply to message #19182] |
Mon, 09 September 2002 18:37   |
Eclipse User |
|
|
|
Randy Hudson wrote:
> Therefore, this method should read:
>
> protected void paintFigure(Graphics g) {
> Rectangle bounds = getBounds();
> g.drawImage(image, bounds.x, bounds.y);
> }
Thanks for your advice. I have a couple more questions about how Draw2d
handles coordinate systems (this might be another good article topic, btw):
1) Does getBounds() always return absolute coordinates no matter how
many levels deep I am in containers? The example you gave above
definitely works because in the layout example there is only one
container--the page. But suppose, I had a subfigure at (50, 50) and
wanted to make all my Figures children of that. Will getBounds() return
the absolute coordinates in this case or will getBounds() return
absolute coordinates translated by (50, 50)?
2) Relative to question #1, I discovered that I needed to make the
coordinate system change you described while playing with my code.
However, I tried to translate the point to window coordinates using the
following code:
protected void paintClientArea(Graphics g) {
Point pos = new Point(0, 0);
translateToAbsolute(pos);
g.drawImage(image, pos);
}
This doesn't work. ie: the coordinates are still (0, 0) after calling
translateToAbsolute(), resulting in the bitmap being clipped out of
existence by its drawing area. (BTW, my code overrides
useLocalCoordinates() to return true.) Why doesn't this work? What do
I need to do to make it work?
Thanks in advance,
Dave
--
Dave Orme
Advanced Systems Concepts
http://www.asc-iseries.com
|
|
|
Re: Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerExcep [message #19197 is a reply to message #19194] |
Mon, 09 September 2002 18:46   |
Eclipse User |
|
|
|
David J. Orme wrote:
> Randy Hudson wrote:
>
>> Therefore, this method should read:
>>
>> protected void paintFigure(Graphics g) {
>> Rectangle bounds = getBounds();
>> g.drawImage(image, bounds.x, bounds.y);
>> }
>
>
> Thanks for your advice. I have a couple more questions about how Draw2d
> handles coordinate systems (this might be another good article topic, btw):
>
> 1) Does getBounds() always return absolute coordinates no matter how
> many levels deep I am in containers? The example you gave above
> definitely works because in the layout example there is only one
> container--the page. But suppose, I had a subfigure at (50, 50) and
> wanted to make all my Figures children of that. Will getBounds() return
> the absolute coordinates in this case or will getBounds() return
> absolute coordinates translated by (50, 50)?
>
> 2) Relative to question #1, I discovered that I needed to make the
> coordinate system change you described while playing with my code.
> However, I tried to translate the point to window coordinates using the
> following code:
>
> protected void paintClientArea(Graphics g) {
> Point pos = new Point(0, 0);
> translateToAbsolute(pos);
> g.drawImage(image, pos);
> }
>
> This doesn't work. ie: the coordinates are still (0, 0) after calling
> translateToAbsolute(), resulting in the bitmap being clipped out of
> existence by its drawing area. (BTW, my code overrides
> useLocalCoordinates() to return true.) Why doesn't this work? What do
> I need to do to make it work?
>
3) Does getLocation() always return coordinates relative to the same
coordinate system as getBounds()?
Thanks again,
Dave
--
Dave Orme
Advanced Systems Concepts
http://www.asc-iseries.com
|
|
|
Re: Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerExcep [message #19251 is a reply to message #19194] |
Tue, 10 September 2002 10:18   |
Eclipse User |
|
|
|
Originally posted by: hudsonr.us.eye-bee-em.com
> 1) Does getBounds() always return absolute coordinates no matter how
> many levels deep I am in containers? The example you gave above
> definitely works because in the layout example there is only one
> container--the page. But suppose, I had a subfigure at (50, 50) and
> wanted to make all my Figures children of that. Will getBounds() return
> the absolute coordinates in this case or will getBounds() return
> absolute coordinates translated by (50, 50)?
getBounds() returns the Rectangle which specifies a figure's location in
it's parent. Therefore, the Rectangle is in it's parent's coordinate
system. But most of the time figures do not have their own (relative)
coordinate system. So the parent's coordinate system is *its* parent's
coordinate system, etc, all the way up to the Canvas. So, getBounds() could
return the absolute location of the Figure on the screen.
> protected void paintClientArea(Graphics g) {
> Point pos = new Point(0, 0);
> translateToAbsolute(pos);
> g.drawImage(image, pos);
> }
If no figure in the parent chain has a relative coordinate system, translate
is a no-op. Therefore 0,0 returns 0,0.
That said, GEF applications often *do* have a figure in the parent change
which introduces a local coordinate system: the Viewport. When using only
absolute coordinates, moving a large sub-tree of figures (such as during
scrolling) is expensive because you must recursively update (translate) all
of the figures' bounds. So the Viewport figure has an option to use
"virtual" scrolling. When you scroll the viewport, none of the viewports
children are actually moved. Instead, just the coordinate system changes.
This is much faster because moving all of the contents often causes the
connections to be re-routed for no reason.
> This doesn't work. ie: the coordinates are still (0, 0) after calling
> translateToAbsolute(), resulting in the bitmap being clipped out of
> existence by its drawing area. (BTW, my code overrides
> useLocalCoordinates() to return true.) Why doesn't this work? What do
> I need to do to make it work?
See the original reply. useLocalCoordinates only affects the placement of
children, it does not affect the way you implement paintFigure().
So, why do we have absolute coordinates anyway? Well, we started off using
it exclusively. It made hit-testing simple, etc. Then we realized that
sometimes it was nice to have relative coordinates. We didn't switch
wholesale to relative because there are still times when absolute
coordinates are the easiest way to make something work. For example,
Connections have a bounds which are a function of their points. And, you
can decorate a connection with arrow tips and labels, and those children
will also affect the overall rectangular bounds of the connections. The
easiest way to implement this "wrapping" of the bounds around the contents
of the figure, is to make the figure be in the same coordinate system as its
parent. Then, you just place everything in the figure, and calculate the
bounds at the end.
|
|
|
Re: Figure: Unable to draw Image; was: Re: Draw2d: Drawing Image in Thumbnail yelds NullPointerExcep [message #19364 is a reply to message #19251] |
Tue, 10 September 2002 10:51  |
Eclipse User |
|
|
|
Randy Hudson wrote:
> <<SNIP>>
> getBounds() returns the Rectangle which specifies a figure's location in
> it's parent. Therefore, the Rectangle is in it's parent's coordinate
> system. But most of the time figures do not have their own (relative)
> coordinate system. So the parent's coordinate system is *its* parent's
> coordinate system, etc, all the way up to the Canvas. So, getBounds() could
> return the absolute location of the Figure on the screen.
Thanks, Randy! You've been a real help here. I guess I have one last
question:
Suppose I have a layout something like:
+--composite/canvas------------+
| |
| +---viewport-------------+ |
| | | |
| | +---shape1---------+ | |
| | | | | |
| | | +---shape2---+ | | |
| | | | | | | |
| | | | | | | |
| | | +---shape2---+ | | |
| | +---shape1---------+ | |
| | | |
| +---viewport-------------+ |
+--composite/canvas------------+
where "viewport" introduces a relative coordinate system. I want to code:
shape2.drawImage(image, pos);
In this case does "pos" have to be in viewport's coordinate system or in
composite/canvas's coordinate system?
Thanks again in advance!
Dave
--
Dave Orme
Advanced Systems Concepts
http://www.asc-iseries.com
|
|
|
Goto Forum:
Current Time: Thu May 08 01:55:21 EDT 2025
Powered by FUDForum. Page generated in 0.04788 seconds
|