Re: Repainting images in JyVisAD

Hi Chi-chi,

Your problem is due to the unusual layout you're attempting
to use for your Swing components.  You can "fix" the problem
by adding the following two lines as the last two lines of
your twist1 subroutine:

    frame.validate()
    frame.repaint()

However, you may notice that your old, unstretched display is
being crunched into the five-pixel gap between the (new) left
and right displays.  Certainly that is not the desired
behavior.

I suggest not creating new displays on button click. Instead,
create all desired displays, add them to the GUI, all at the
beginning of the program.  When the user clicks a button,
update the relevant displays as desired.  I'm not certain
whether your goal is to have four displays (an "original",
an "x2", an "x3" and an "x4"), or just one or two displays
that get updated when the buttons are pressed.  But either
one should be possible without needing to add displays later.
Adding components to a window that already onscreen can be
tricky, and should be avoided if possible.

Good luck,
-Curtis

At 09:14 AM 5/16/2003, Chi-chi Ekweozor wrote:
>Hi,
>Thanks for all your brill suggestions.  I've spent all day putting them to
>good use in every possible combination and can now safely say that... the
>problem persists :o'(.  The JFrame still needs to be maximised before the
>stretched image is displayed.
>
>Find pasted below the full source (imagereplace2.py) for your amusement.
>Replace "pinnaimage" with any old image before testing...
>
>A quick program recap for those who can't/won't:
>                                        -----oooo-----
>1. Load in an image.
>2. Extract its domain set
>3. Set up Twist_Grid 1: the image's first 'twisted' domain set
>4. Set up Twist_Grid 2: the image's 2nd 'twisted' domain set
>5. Def(ine) twist1() function for stretching the image by x2
>6. Def(ine) twist2() function for stretching the image by x3
>7. Map original image to a Display
>8. Add Display and some JButtons to a ContentPane via JPanels
>9. Add Pane to a JFrame.
>10. On clicking "x2" button:
>  Call twist1() to display the stretched image by mapping the first twisted
>domain set to a new display
>11.On "x3" button click:
>  Call twist2() to display the next stretched image by mapping the 2nd
>domain set to a the same display used for #10
>
>                                     -----oooo-----
>Your previous suggestions have helped speed up things a *tiny* bit
>(twist_grid is no longer computed within twist1() i.e a FlatField containing
>twist_grid is set up at program start up) but it's still baulking at
>updating the display properly.
>
>The Jython version of  points 10&11 pasted below:
>
>---start---
>#define twist events for stretching image in y direction
>def twist1(event):
>    dispt = subs.makeDisplay(twist_maps)
>    dispt.addData("twisted",twist)
>    dispt.setBoxSize(.5)
>    displayPanelOne = JPanel(border=border.TitledBorder("Display Panel"),
>layout=GridLayout(1,2,5,5))
>    displayPanelOne.add(dispt.getComponent())
>    displayPanelOne.add(plotd.getComponent())
>    pane.add("Center", displayPanelOne)     #will not display otherwise
>
>#twist event 2
>def twist2(event):
>    dispt = subs.makeDisplay(twist_maps2)
>    if ref is None:
>        ref=dispt.addData("twisted2", twist2)
>    else:
>        ref=dispt.setData(twist2)
>    dispt.setBoxSize(.5)
>    displayPanelTwo = JPanel(border=border.TitledBorder("Display Panel"),
>layout=GridLayout(1,2,5,5))
>    displayPanelTwo.add(dispt.getComponent())
>    displayPanelTwo.add(plotd.getComponent())
>    pane.add("Center", displayPanelTwo)    #will not display otherwise
>---end---
>
>Still hacking away at it.  Any suggestions welcome.   On running the program
>you'll notice that twist2() does not appear to be generating a new
>twist_grid (original image is still displayed) despite the fact that it is a
>duplicate of twist1() albeit with a slightly modified sampling domain.  I
>suspect it doesn't like having to addData to the same Display (dispt).
>Should get an UnboundLocal Error about "ref".
>
>Thanks for reading.
>Chi-chi
>
>------- imagereplace2.py begin: ---------------
>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")
>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)
>#some formatting for making this into an image
>twistdom = getDomainType(twist)
>twistrng = getRangeType(twist)
>twist_maps = subs.makeMaps(twistdom[0],'x', twistdom[1],'y',
>twistrng[0],'red',twistrng[1],'green',twistrng[2],'blue')
>
>#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)
>#some formatting for making this into an image
>twistdom2 = getDomainType(twist2)
>twistrng2 = getRangeType(twist2)
>twist_maps2 = subs.makeMaps(twistdom2[0],'x', twistdom2[1],'y',
>twistrng2[0],'red',twistrng2[1],'green',twistrng2[2],'blue')
>
>#define twist events for stretching image in y direction
>def twist1(event):
>    dispt = subs.makeDisplay(twist_maps)
>    dispt.addData("twisted",twist)
>    dispt.setBoxSize(.5)
>    displayPanelOne = JPanel(border=border.TitledBorder("Display Panel"),
>layout=GridLayout(1,2,5,5))
>    displayPanelOne.add(dispt.getComponent())
>    displayPanelOne.add(plotd.getComponent())
>    pane.add("Center", displayPanelOne)
>#    pane.add("South", buttonPanel)
>
>#twist event 2
>def twist2(event):
>    dispt = subs.makeDisplay(twist_maps2)
>    if ref is None:
>        ref=dispt.addData("twisted2", twist2)
>    else:
>        ref=dispt.setData(twist2)
>    dispt.setBoxSize(.5)
>    displayPanelTwo = JPanel(border=border.TitledBorder("Display Panel"),
>layout=GridLayout(1,2,5,5))
>    displayPanelTwo.add(dispt.getComponent())
>    displayPanelTwo.add(plotd.getComponent())
>    pane.add("Center", displayPanelTwo)
>    pane.add("South", buttonPanel)
>
># 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)
>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=twist1)
>stretchButton2=JButton("x3", preferredSize=(100,20), actionPerformed=twist2)
>
>buttonPanel.add(stretchButton, FlowLayout())
>buttonPanel.add(stretchButton2, FlowLayout())
>
>#add all contents to content pane
>pane.add("Center", displayPanel)
>pane.add("South", buttonPanel)
>
>frame.setSize(800, 500)
>frame.setVisible(1)
>------- imagereplace2.py end ---------------