Widget["IndexedImagePanel"]
represents an indexed image panel.
"IndexedImagePanel"
Widget["IndexedImagePanel"]
represents an indexed image panel.
Details
- To use Widget["IndexedImagePanel"], you first need to load GUIKit using Needs["GUIKit`"].
- By default, the image is displayed centered and nonscaled in the panel; however, you can also allow the image to scale to fit the entire size of the panel.
- The following properties are available:
-
"imageWidth" 1 width of the image "imageHeight" 1 height of the image "imagePixelSize" 1 pixel size (an integer) "imageColorMapSize" 0 size of the color map "imageColorComponents" Null a list of the components of each color in the color map "imagePixels" Null the indices of each pixel "preferredSize" the display size of the image - The following event is available:
-
"mouseClicked" the event triggered when the mouse is clicked anywhere within the panel - The following method is available:
-
InvokeMethod["getImagePixelCoordinatesAt", mouseEvent] returns the image coordinates of the mouse event position - By default, most methods do not auto-repaint, so you can call a version of the methods taking True or False as the last argument. This allows you to make multiple calls to various locations on the image and then call InvokeMethod[{panel,"repaint"}] or a method with True as the last argument.
Examples
open all close allBasic Examples (1)
Needs["GUIKit`"]Create a 5×5 image with five indexed colors, with all pixels set to black:
ref = GUIRun[Widget["IndexedImagePanel", {
"preferredSize" -> Widget["Dimension", {"width" -> 20 * 5, "height" -> 20 * 5}],
"imageWidth" -> 5,
"imageHeight" -> 5,
"imagePixelSize" -> 20,
"imageColorMapSize" -> 5,
"imageColorComponents" -> Flatten[
{{0, 0, 0}, {255, 255, 255}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}}],
"imagePixels" -> Flatten[ Table[0, {5}, {5}]]}, Name -> "imagePanel"]
]GUIScreenShot[ref]ref @ SetPropertyValue[{"imagePanel", "imagePixels"},
Flatten[ Table[1, {5}, {5}]]]GUIScreenShot[ref]Loop 100 times, setting all pixels to random values:
Do[
ref @ SetPropertyValue[{"imagePanel", "imagePixels"},
Flatten[ Table[Random[Integer, {0, 4}], {5}, {5}]]],
{100}]GUIScreenShot[ref]Loop 100 times, setting a random cell in the first row to a random value within each loop:
Do[
pos = Random[Integer, {0, 4}];
ind = Random[Integer, {0, 4}];
ref @ InvokeMethod[{"imagePanel", "setImagePixel"}, pos, 0, ind];
, {100}]GUIScreenShot[ref]Repaint the image to see the result:
ref @ InvokeMethod[{"imagePanel", "repaint"}]GUIScreenShot[ref]Combine the previous two method calls by giving True as the last argument to "setImagePixel":
Do[
pos = Random[Integer, {0, 4}];
ind = Random[Integer, {0, 4}];
ref @ InvokeMethod[{"imagePanel", "setImagePixel"}, pos, 0, ind, True];
, {100}]GUIScreenShot[ref]Loop 100 times, setting a random column to a random value:
Do[
coords = Transpose[{Table[#, {5}]& @ Random[Integer, {0, 4}], Range[0, 4]}];
ind = Table[#, {5}]& @ Random[Integer, {0, 4}];
ref @ InvokeMethod[{"imagePanel", "setImagePixelArray"}, coords, ind, True];
, {100}]GUIScreenShot[ref]ref @ SetPropertyValue[{"imagePanel", "imageGrid"}, True]ref @ InvokeMethod[{"imagePanel", "repaint"}]GUIScreenShot[ref]Set the grid color to a custom value:
ref @ Script[
SetPropertyValue[{"imagePanel", "imageGridColor"},
Widget["Color", InitialArguments -> {255, 0, 0}]]
]ref @ InvokeMethod[{"imagePanel", "repaint"}]GUIScreenShot[ref]Scale the image to fit the size of the panel (after resizing):
ref @ SetPropertyValue[{"imagePanel", "scaleImage"}, True]ref @ InvokeMethod[{"imagePanel", "repaint"}]GUIScreenShot[ref]ref @ SetPropertyValue[{"imagePanel", "preserveImageAspectRatio"}, True]ref @ InvokeMethod[{"imagePanel", "repaint"}]GUIScreenShot[ref]Change the origin of the image:
ref @ SetPropertyValue[{"imagePanel", "centerImage"}, False]ref @ InvokeMethod[{"imagePanel", "repaint"}]GUIScreenShot[ref]The method "fillImagePixels" fills individual pixels of the image:
ref @ InvokeMethod[{"imagePanel", "fillImagePixels"}, Random[Integer, {0, 4}], True]GUIScreenShot[ref]The method "fillImagePixelArray" fills an array of explicit locations:
ref @ InvokeMethod[{"imagePanel", "fillImagePixelArray"}, {{0, 0}, {1, 1}, {1, 2}}, Random[Integer, {0, 4}], True]GUIScreenShot[ref]It also fills a region given by an origin {x,y} value coupled with relative offset locations:
ref @ InvokeMethod[{"imagePanel", "fillImagePixelArray"}, 2, 2, {{0, 0}, {1, 1}, {1, 2}}, Random[Integer, {0, 4}], True]GUIScreenShot[ref]ref @ InvokeMethod[{"imagePanel", "fillImagePixelArray"}, 0, 3, {{0, 0}, {1, 1}, {1, 2}}, Random[Integer, {0, 4}], True]GUIScreenShot[ref]The method "fillImagePixelRect" fills a rectangular region:
ref @ InvokeMethod[{"imagePanel", "fillImagePixelRect"}, 1, 1, 2, 3, Random[Integer, {0, 4}], True]GUIScreenShot[ref]With "setImagePixelRect", the region can be also be specified by its top-left location:
ref @ InvokeMethod[{"imagePanel", "setImagePixelRect"}, 1, 1, {{0, 1, 0}, {1, 2, 1}}, True]GUIScreenShot[ref]Interactive Examples (2)
Needs["GUIKit`"]Create an image in which each cell is filled with a random color when clicked:
ref = GUIRun[
Widget["IndexedImagePanel", {
"preferredSize" -> Widget["Dimension", {"width" -> 20 * 5, "height" -> 20 * 5}],
"imageWidth" -> 5,
"imageHeight" -> 5,
"imagePixelSize" -> 20,
"imageColorMapSize" -> 5,
"imageColorComponents" -> Flatten[
{{0, 0, 0}, {255, 255, 255}, {255, 0, 0}, {0, 255, 0}, {0, 0, 255}}],
"imagePixels" -> Flatten[ Table[0, {5}, {5}]],
BindEvent["mouseClicked",
Script[
coords = InvokeMethod[{"imagePanel", "getImagePixelCoordinatesAt"},
WidgetReference["#"]];
InvokeMethod[{"imagePanel", "setImagePixel"}, coords, Random[Integer, {0, 4}], True];
]
]
}, Name -> "imagePanel",
WidgetLayout -> {"Stretching" -> {Maximize, Maximize}}]
]GUIScreenShot[ref]GUIScreenShot[ref]Needs["GUIKit`"]Create a three-color image panel in which clicking a gray square turns it black and subsequent clicks toggle between white and black:
ref = GUIRun[
Widget["Frame", {"title" -> "IndexedImage Demo",
"resizable" -> True,
Widget["IndexedImagePanel", {
"preferredSize" -> Widget["Dimension", {"width" -> 10 * 20, "height" -> 10 * 15}],
"imageWidth" -> 20,
"imageHeight" -> 15,
"imagePixelSize" -> 10,
"imageColorMapSize" -> 3,
"imageGrid" -> True,
"scaleImage" -> True,
"preserveImageAspectRatio" -> True,
"imageColorComponents" -> Flatten[
{{0, 0, 0}, {255, 255, 255}, {128, 128, 128}}],
"imagePixels" -> Flatten[ Table[2, {20}, {15}]],
BindEvent["mouseClicked",
Script[
mouseEvent = WidgetReference["#"];
coords = InvokeMethod[{"imagePanel", "getImagePixelCoordinatesAt"},
mouseEvent];
If[(coords[[1]] ≥ 0) && ( coords[[2]] ≥ 0),
vl = InvokeMethod[{"imagePanel", "getImagePixelAt"},
mouseEvent];
InvokeMethod[{"imagePanel", "setImagePixel"}, coords,
If[vl =!= 0, 0, 1], True];
];
]
]
}, Name -> "imagePanel",
WidgetLayout -> {"Stretching" -> {Maximize, Maximize}}]
}]
]GUIScreenShot[ref]After a few random clicks and reclicks:
GUIScreenShot[ref]