Re: Repainting images in JyVisAD

Chi-chi:

Thanks for the illustrations of what you want your display to look like - they helped a lot. It appears that you want to have a single panel for displaying the image, another for a graph of some kind, and then (at least)one more for controls...and to put all these into one frame.

Because you want to have the 'twisted' images simply replace the one that is being shown, you can/should use a much simpler approach: you only need one, single Display for all your images (plus another for the graph).

As I mentioned in my previous email, the only thing that you need to do in the event handlers is to do a "ref.setData(twist)" ...or (twist2). Because the illustrations you sent along indicate that you want to actually replace the original image, the solution is even simpler, since the DataReference "ref" can be initially defined when you add your original image to the display (as in: ref=addData("original",img)).

One other thing to note, you used the name "twist2" for both the FlatField and the method name -- you probably should not do that.

Attached, you will find two .py files. The first one implements the ideas in this and my previous email, and otherwise preserves your use of Java swing stuff. I'm including it mostly for your benefit, since you will find that the "ref.setData()" does not have the performance you probably want.

The second .py file illustrates a different strategy: it uses VisAD's 'selectValue' mechanism through the helper class in subs.py: SelectField. For this, you make a SelectField by naming the 'selector' type, and pass in an array of your Fields (this only works if all the Fields in the array are of the same Type). As you can see in the code, the changing between the Fields is then just using "sf.showIt(index)" where 'sf' is the instance of the SelectField. The benefit is that the change happens very quickly (unlike the 'setData()' in the previous example).

I also added an "x1" button to show the original image...for illustration purposes.

I also took the liberty in the second example to remove your JFrame and all the extraneous layout, since it is perhaps not necessary (I left in all your imports,however). In Python, each of the 'showDisplay()' calls may contain key words you can use to do layout:

* panel=name_of_panel
     if this is present, then the display will be put into the named
     JPanel and not into a separate JFrame.  The idea is that you
     would then use this JPanel with a different Display...e.g.,
        graph_panel = JPanel()
        plotd.showDisplay(panel=graph_panel)

* top=name_of_panel, bottom=name_of_other_panel, etc.
    if this is present, then the named JPanel will be put into a
    BorderLayout location in the JFrame created for this display. In
    your case, I put the 'graph' into a JPanel, and then when I
    showed the Display for the images, I used:

disp.showDisplay(right=graph_panel, bottom=buttonPanel, height=400, width=800)

This meant that the image display ("disp") would be shown, but with the other panels attached to the JFrame.

Anyway, I hope these examples will help you (and others).

Take care,

tom
from visad import *
from visad.python.JPythonMethods import *
import graph, subs
import time
from java.lang import Boolean, System
from javax.swing import JPanel, JFrame, JButton, BorderFactory, border
from java.awt import BorderLayout, GridLayout, FlowLayout, Font

#img=load("pinnaimage.jpg")
img=load("Sim3DAudiov0.3.jpg")
set = img.getDomainSet()
lengths = set.getLengths()
width = lengths[0]
height = lengths[1]

#define twist grids for stretching image in pre-defined amounts (y direction 
only)
#Twist_Grid 1
twist_grid = [ [w for h in xrange(height) for w in xrange(width)],
[(w-(h*0.5)) for h in xrange(height) for w in xrange(width)]]
twist_set = Gridded2DSet(set.getType(), twist_grid, width, height, None,
None, None, 0)
twist = FlatField(img.getType(), twist_set)
twist.setSamples(img.getValues(Boolean(0)),0)

#Twist_Grid 2
twist_grid2 = [ [w for h in xrange(height) for w in xrange(width)],
[(w-(h*0.3)) for h in xrange(height) for w in xrange(width)]]
twist_set2 = Gridded2DSet(set.getType(), twist_grid2, width, height, None,
None, None, 0)
twist2 = FlatField(img.getType(), twist_set2)
twist2.setSamples(img.getValues(Boolean(0)),0)
ref = None

#define twist events for stretching image in y direction
def twist_1(event):
    ref.setData(twist)

#twist event 2
def twist_2(event):
    ref.setData(twist2)

# Display data for making an overall display...starting with the original image
imgdom = getDomainType(img)
imgrng = getRangeType(img)
maps = subs.makeMaps(imgdom[0],'x', imgdom[1],'y',
                imgrng[0],'red',imgrng[1],'green',imgrng[2],'blue')

disp = subs.makeDisplay(maps)
ref=disp.addData("original",img)
disp.setBoxSize(.5)

# subs for as yet unused plot panel
m2 = subs.makeMaps(imgdom[0],"x",imgrng[0],"y",imgdom[1],"selectvalue")
plotd = subs.makeDisplay(m2)
subs.setBoxSize(plotd,.60)

# change the scale label on x axis
xscale=AxisScale(m2[0],label="Intensity Profile")
showAxesScales(plotd,1)

frame = JFrame("Image Stretcher")
pane = frame.getContentPane()
pane.setLayout(BorderLayout())

displayPanel = JPanel(border=border.TitledBorder("Display Panel"),
layout=GridLayout(1,2,5,5))
#add original image first
displayPanel.add(disp.getComponent())
displayPanel.add(plotd.getComponent())

#some program calls to add stretched image when buttons are clicked
buttonPanel = JPanel(border=border.TitledBorder("Y-Stretch Controls"),
layout=FlowLayout())
stretchButton=JButton("x2", preferredSize=(100,20), actionPerformed=twist_1)
stretchButton2=JButton("x3", preferredSize=(100,20), actionPerformed=twist_2)

buttonPanel.add(stretchButton, FlowLayout())
buttonPanel.add(stretchButton2, FlowLayout())

#add all contents to content pane
pane.add("Center", displayPanel)
pane.add("South", buttonPanel)

frame.validate()
frame.setSize(800, 500)
frame.setVisible(1)



from visad import *
from visad.python.JPythonMethods import *
import graph, subs
import time
from java.lang import Boolean, System
from javax.swing import JPanel, JFrame, JButton, BorderFactory, border
from java.awt import BorderLayout, GridLayout, FlowLayout, Font

#img=load("pinnaimage.jpg")
img=load("Sim3DAudiov0.3.jpg")
set = img.getDomainSet()
lengths = set.getLengths()
width = lengths[0]
height = lengths[1]

#define twist grids for stretching image in pre-defined amounts (y direction 
only)
#Twist_Grid 1
twist_grid = [ [w for h in xrange(height) for w in xrange(width)],
[(w-(h*0.5)) for h in xrange(height) for w in xrange(width)]]
twist_set = Gridded2DSet(set.getType(), twist_grid, width, height, None,
None, None, 0)
twist = FlatField(img.getType(), twist_set)
twist.setSamples(img.getValues(Boolean(0)),0)

#Twist_Grid 2
twist_grid2 = [ [w for h in xrange(height) for w in xrange(width)],
[(w-(h*0.3)) for h in xrange(height) for w in xrange(width)]]
twist_set2 = Gridded2DSet(set.getType(), twist_grid2, width, height, None,
None, None, 0)
twist2 = FlatField(img.getType(), twist_set2)
twist2.setSamples(img.getValues(Boolean(0)),0)

def twist_0(event):
    sf.showIt(0)

#define twist events for stretching image in y direction
def twist_1(event):
    sf.showIt(1)

#twist event 2
def twist_2(event):
    sf.showIt(2)

sf = subs.SelectField('img_index',[img, twist, twist2])

# Display data for making an overall display...starting with the original image
imgdom = getDomainType(img)
imgrng = getRangeType(img)
maps = subs.makeMaps(imgdom[0],'x', imgdom[1],'y',
                imgrng[0],'red',imgrng[1],'green',imgrng[2],'blue')

# include the ScalarMap for the selectValue from SelectField.
maps.append(sf.getScalarMap())

disp = subs.makeDisplay(maps)

ref=disp.addData("images",sf.getSelectField())
disp.setBoxSize(.5)

# subs for as yet unused plot panel
m2 = subs.makeMaps(imgdom[0],"x",imgrng[0],"y",imgdom[1],"selectvalue")
plotd = subs.makeDisplay(m2)
subs.setBoxSize(plotd,.60)

# change the scale label on x axis
xscale=AxisScale(m2[0],label="Intensity Profile")
showAxesScales(plotd,1)

#some program calls to add stretched image when buttons are clicked
buttonPanel = JPanel(border=border.TitledBorder("Y-Stretch Controls"),
layout=FlowLayout())

originalButton=JButton("x1", preferredSize=(100,20), actionPerformed=twist_0)
stretchButton=JButton("x2", preferredSize=(100,20), actionPerformed=twist_1)
stretchButton2=JButton("x3", preferredSize=(100,20), actionPerformed=twist_2)

buttonPanel.add(originalButton)
buttonPanel.add(stretchButton)
buttonPanel.add(stretchButton2)

# make a JPanel for the graph...and put it there
graph_panel = JPanel()
plotd.showDisplay(panel=graph_panel)

# now show all three panels in one frame.
disp.showDisplay(right=graph_panel, bottom=buttonPanel, height=400,
width=800)

sf.showIt(0)



  • 2003 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: