Using ImageRegionSelectors to Control an Applet
Estimated Time Required: 10 mins
This sample shows an applet that is controlled by two ImageRegionSelectors. The applet is used by people searching for a new house by allowing them to specify the area (either a county or a town) that they would like to live in and the type of house required. These variables are captured with separate ImageRegionSelector components - with the list of suitable houses updated whenever the selection in either component is changed
The source is in the irsexample.java file in the ImageRegionSelector Bean \samples\irsexample directory.
If your default browser supports Java 1.1, you can see the applet running below. Please note it may take a few seconds to load.
As ImageRegionSelector 1.0 uses Swing you will need to have swing.jar from Swing 1.0.3 in your classpath. Also, if you are using Netscape, you need version 4.06, or higher.
You can also run the sample in the Appletviewer or in VisualAge for Java.
To run the applet using Appletviewer:
To run the applet using VisualAge for Java:
The applet lets users specify two pieces of data. The map ImageRegionSelector allows you to specify the area in which you are interested in finding a house. You can see that as the mouse rolls over selectable regions their color lightens since "Lighten" is specified for the rollover emphasis style. Upon selection of such a region you can see that the cutaway image is displayed for that region. You can also see that details of available houses in that region have been displayed on the right had side of the applet
The ImageRegionSelector that is displaying a row of houses allows you to specify the type of house that is being looked for. The display image shows terraced housing, semi-detached housing, flats and detached housing. Again, when the mouse rolls over these different types of houses the regions are lightened. Upon selection a cutaway image is again shown to emphasise the selection. This also updates the house details shown as only houses of the type selected are displayed.
The lines of code relevant to the use of the ImageRegionSelector are described below.
import com.ibm.eou.swingimageregionselector.*;
import java.awt.event.*;
public class Irsexample extends com.sun.java.swing.JApplet implements ItemListener
private String[] mapRegionNames = {"Lincolnshire", "Lincoln", ....., "Colchester"};
private String[] mapTooltips = {"Click here for the county of Lincolnshire", ....., "Click here for the town of Colchester"};
private String[] houseRegionNames = {"Terraced", "Semi", "Flat", "Detached"};
private String[] houseTooltips = {"Click here to select terraced housing", "Click here to select semi-detached housing",
"Click here to select flats", "Click here to select detached housing" };
private ImageRegionSelector iMapSelector;
private ImageRegionSelector iHouseSelector;
private String regionSelected = null;
private String houseType = null;
iMapSelector = new ImageRegionSelector();
iMapSelector.setLocation(0, 20);
iMapSelector.setDisplayImage("/com/ibm/eou/swingimageregionselectorsamples/anglia.gif");
iMapSelector.setColorMapImage("/com/ibm/eou/swingimageregionselectorsamples/anglia_map.gif");
iMapSelector.setCutawayImage("/com/ibm/eou/swingimageregionselectorsamples/anglia_cutaway.gif");
iMapSelector.setSelectionEmphasisStyle(ImageRegionSelector.CUTAWAY);
iMapSelector.setRolloverEmphasisStyle(ImageRegionSelector.LIGHTEN);
iMapSelector.setRegionNames(mapRegionNames);
iMapSelector.setTooltips(mapTooltips);
iMapSelector.addItemListener(this);
getContentPane().add(iMapSelector);
public void itemStateChanged(ItemEvent e)
{
if(e.getItem() == iMapSelector)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
regionSelected = (String)iMapSelector.getSelectedItem();
}
else
{
regionSelected = null;
}
}
if(e.getItem() == iHouseSelector)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
houseType = (String)iHouseSelector.getSelectedItem();
}
else
{
houseType = null;
}
}
displayHouses();
}