Login  Register

Re: Display an image on a JPanel

Posted by Robert Lockwood on Oct 24, 2013; 8:02pm
URL: http://imagej.273.s1.nabble.com/Display-an-image-on-a-JPanel-tp5005268p5005311.html

I found the error, the JPanel that the JImagePanel was added to had a
layout set to null, when I commented out that line the errors went away but
my GUI is messed up.  Some how there was a design error that didn't show
until I tried to add the JImagePanel or I screwed something up ...

Thanks Curtis and Johannes.


On Thu, Oct 24, 2013 at 12:39 PM, Robert Lockwood <[hidden email]>wrote:

> Thanks Curtis, I have minimal code that re-creates the problem with an
> application class, TestApp, and a single class, JImagePanel.
>
> I have no idea how to get them into github.  I've never had any
> cooperators with whom to collaborate so I'm absolutely ignorant of how to
> use github.
>
>
> On Thu, Oct 24, 2013 at 11:10 AM, Curtis Rueden <[hidden email]> wrote:
>
>> Hi Robert,
>>
>> Can you please post a complete example demonstrating the problem?
>> GitHub.com is a nice place to do this. It will make it much easier for
>> people to help you.
>>
>> Regards,
>> Curtis
>>
>>
>> On Thu, Oct 24, 2013 at 12:25 PM, Robert Lockwood <[hidden email]
>> >wrote:
>>
>> > In order to have an ImagePanel for my application I downloaded the
>> > JImagePanel from GitHub in response to Johannes reply, created a class
>> for
>> > it in my project changing the package to my package in Eclipse Keppler.
>> >
>> > In my GUI initialization where the GUI is built I have:
>> >
>> >         jImagePanel = new JImagePanel();
>> >         jImagePanel.setName("jImagePanel");
>> >         jImagePanel.setBorder(new LineBorder(new Color(0, 0, 0)));
>> >         jImagePanel.setBounds(10, 10, 641, 481);
>> >         jImagePanel.setBackground(Color.WHITE);
>> >         jImagePanel.setOpaque(false);
>> >         pnlBase.add(jImagePanel);
>> >
>> > when the image is read I display it with:
>> >
>> >         if (!jImagePanel.isOpaque()) {
>> >             jImagePanel.setOpaque(true);
>> >         }
>> >
>> >         jImagePanel.updateImage(imp);
>> >
>> > When I shift from Source to Design Google Builder crashes indicating
>> > problems in JImagePanel's method paintCompnent().
>> >
>> > When I attempt to run it the GUI does not display properly, the
>> contents of
>> > the JFrame are gray.  Moving the cursor over the contents exposes all
>> the
>> > JButtons, but nothing else, and clicking on the button results in the
>> > expected display of my file selector.  If the file is selected the image
>> > displays correctly as do all the other visual objects in the JFrame.
>> >
>> > I get NullPointerException errors that finger the same line that trying
>> to
>> > move to Display fingers.
>> >
>> > I modified the below code to check that imp was not null but it had no
>> > effect.
>> >
>> > Johannes gave another URL for the code but I don't know how to get that
>> > code.
>> >
>> > I guess I'm partway there ...
>> >
>> > From JImagePanel:
>> >
>> >     @Override
>> >     public void paintComponent(Graphics g) {
>> >         super.paintComponent(g);
>> >         try {
>> >             if (imageUpdated) {
>> >                 imageUpdated = false;
>> >                 imp.updateImage();
>> >             }
>> >             Java2.setBilinearInterpolation(g,
>> > Prefs.interpolateScaledImages);
>> >
>> >             Image img = imp.getProcessor().createImage();  // THIS IS
>> THE
>> > ERROR LINE
>> >
>> >             if (img != null) {
>> >                 waitForImage(img);
>> >                 int displayWidth = (int) (srcRect.width *
>> magnification);
>> >                 int displayHeight = (int) (srcRect.height *
>> magnification);
>> >                 Dimension size = getSize();
>> >                 int offsetX = (size.width - displayWidth) / 2;
>> >                 int offsetY = (size.height - displayHeight) / 2;
>> >                 g.translate(offsetX, offsetY);
>> >                 g.drawImage(img, 0, 0, displayWidth, displayHeight,
>> > srcRect.x,
>> >                         srcRect.y, srcRect.x + srcRect.width, srcRect.y
>> >                                 + srcRect.height, null);
>> >             }
>> >             drawOverlay(g);
>> >         } catch (OutOfMemoryError e) {
>> >             IJ.outOfMemory("Paint");
>> >         }
>> >     }
>> >
>> >
>> >
>> >
>> > On Wed, Oct 23, 2013 at 7:31 AM, Johannes Schindelin <
>> > [hidden email]> wrote:
>> >
>> > > Hi Robert,
>> > >
>> > > On Tue, 22 Oct 2013, Robert Lockwood wrote:
>> > >
>> > > > I've an application that collects a monochrome "image" as an array
>> of
>> > > > "unsigned short" (short[]), saves it as a TIFF (thanks for that
>> help)
>> > > > and displays it on JPanel.  I'd rather use the ImageJ methods etc.
>> to
>> > > > display the image on the JPanel and reduce my code.
>> > > >
>> > > > Is this possible?  How?
>> > >
>> > > Unfortunately, the ImageCanvas -- ImageJ 1.x' AWT component intended
>> to
>> > > display images -- is tightly bound to the ImageWindow class and does
>> not
>> > > play well with other components in the same AWT container.
>> > >
>> > > Besides, all the claims that AWT and Swing mix well now seem to be
>> > > premature still.
>> > >
>> > > Therefore we integrated Simon Andrews' JImagePanel into Fiji:
>> > >
>> > >
>> > >
>> >
>> https://github.com/fiji/fiji/blob/master/src-plugins/fiji-lib/src/main/java/fiji/util/gui/JImagePanel.java
>> > >
>> > > It should be relatively easy to integrate into your software: it is
>> > > contained in the fiji-lib artifact available at
>> http://maven.imagej.net/
>> > .
>> > >
>> > > You could also extract the file and insert into your own source code,
>> of
>> > > course, but you'd ask for diverging versions.
>> > >
>> > > Ciao,
>> > > Johannes
>> > >
>> >
>> >
>> >
>> > --
>> > When I was 12 I thought I would live forever.
>> > So far, so good.
>> >
>> > --
>> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>> >
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
>
>
> --
> When I was 12 I thought I would live forever.
> So far, so good.
>



--
When I was 12 I thought I would live forever.
So far, so good.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html