[vtkusers] Java remove vtkImageViewer2 not working
Jonathan Morra
jonmorra at gmail.com
Fri Dec 3 01:12:04 EST 2010
I apologize for the length of this example (it's a little long), but it is
totally self contained and shows my the error that I'm having. Please let
me know if you need any more information, or the tweaks to make what I have
work.
import vtk.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class VTKSwingError {
static {
System.loadLibrary("vtkCommonJava");
System.loadLibrary("vtkFilteringJava");
}
static JPanel topPanel;
static JPanel bottomPanel;
static JFrame frame;
// This is just creating two different images so we can tell when
they're swapped
public static JPanel createImagePanel(boolean useMod0) {
vtkImageData image = new vtkImageData();
image.SetSpacing(1, 1, 1);
image.SetOrigin(0, 0, 0);
image.SetExtent(0, 511, 0, 511, 0, 0);
image.AllocateScalars();
for (int x=0; x<512; x++) {
for (int y=0; y<512; y++) {
if ((useMod0 && y%2 == 0) || (!useMod0 && y%2 == 1))
image.SetScalarComponentFromDouble(x, y, 0, 0, 1);
}
}
vtkRenderWindowPanel renWin = new vtkRenderWindowPanel();
renWin.setInteractorStyle(new vtkInteractorStyleImage());
vtkImageViewer2 imageViewer = new vtkImageViewer2();
imageViewer.SetInput(image);
imageViewer.SetColorLevel(0.5);
imageViewer.SetColorWindow(1);
imageViewer.SetSliceOrientationToXY();
imageViewer.SetSlice(1);
imageViewer.SetRenderWindow(renWin.GetRenderWindow());
imageViewer.GetRenderer().ResetCamera();
JPanel renWinPanel = new JPanel(new BorderLayout());
renWinPanel.add(renWin, BorderLayout.CENTER);
return renWinPanel;
}
public static void main(String[] args) {
topPanel = new JPanel(new BorderLayout());
topPanel.setMinimumSize(new Dimension(400, 200));
topPanel.setPreferredSize(new Dimension(400, 200));
topPanel.add(createImagePanel(true), BorderLayout.CENTER);
bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setMinimumSize(new Dimension(400, 200));
bottomPanel.setPreferredSize(new Dimension(400, 200));
bottomPanel.add(createImagePanel(false), BorderLayout.CENTER);
JButton switchButton = new JButton("Switch Panels");
switchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// This is where VTK is going haywire
Component oldTop = topPanel.getComponent(0);
Component oldBottom = bottomPanel.getComponent(0);
topPanel.remove(oldTop);
bottomPanel.remove(oldBottom);
topPanel.add(oldBottom, BorderLayout.CENTER);
bottomPanel.add(oldTop, BorderLayout.CENTER);
frame.getContentPane().validate();
frame.getContentPane().repaint();
}
});
frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(topPanel, BorderLayout.NORTH);
frame.getContentPane().add(bottomPanel, BorderLayout.CENTER);
frame.getContentPane().add(switchButton, BorderLayout.SOUTH);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
On Thu, Dec 2, 2010 at 8:10 PM, Jim Peterson <jimcp at cox.net> wrote:
> Jonathan Morra wrote:
>
>>
>> I have a java swing JPanel that contains another JPanel that contains a
>> vtkRenderWindowPanel which is hooked up to a vtkImageViewer2. In the course
>> of my program I want to remove the inner JPanel from the outer JPanel with
>> JPanel.remove(). I then want to add the inner JPanel to a different outer
>> JPanel. However when I do this VTK throws the following exception
>>
>> ERROR: In ..\..\src\Rendering\vtkWin32OpenGLRenderWindow.cxx, line 247
>> vtkWin32OpenGLRenderWindow (00000004C8E4830): wglMakeCurrent failed in
>> MakeCurrent(), error: The handle is invalid.
>>
>> This seems like a very simple swing operation that I'm trying to do, but
>> it's failing. Does anyone know what I'm doing wrong?
>> Thanks
>>
>> Jonathan,
> you do understand that the vtk classes are JNI interfaces to the shared
> library version of vtk. I suspect you are causing Java to free resources
> that are still referenced by the memory instantiated by the still active
> structures outside Java in vtk. Without an example it is pretty hard to try
> to visualize the java and native system memory from your description. What
> you are doing might be sensible with all java objects, but when JNI objects
> are involved memory references may not be as clear as desired. are not
> always completely in sync.
> Chances are if you coded the same kind of operation in C++ the make current
> routine would have detected the different potential interaction source and
> issued this message: (from the MakeCurrent() function in
> vtkWin32OpenGLRenderWindow.Cxx)
>
> if(this->IsPicking && current)
> {
> vtkErrorMacro("Attempting to call MakeCurrent for a different window"
> " than the one doing the picking, this can causes
> crashes"
> " and/or bad pick results");
> }
>
> Unless you want to post an example, I would say "If it hurts, don't do
> that".
>
> Hope that helps,
> Jim
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20101202/ce46c57d/attachment.htm>
More information about the vtkusers
mailing list