Display sketch code: ClickablePanelDemo.pde. The code for ClickablePanel is in Clickable.pde
ClickablePanels can be used for drawing in response to clicks or they can be used as input device for clicks. They also can be "invisible" and the whole sketch can be used as a Clickable panel. This demo illustrates all of these capabilities. When you click on the demo's visible panel it does a very simple drawing and displays the coordinates of the point clicked together with a message about the point clicked. The second panel is invisible and called the secret panel. If you click on it, you will get a small disk centered on the click. If you click any other part of the sketch, it will draw a small square about the point and move the click point to point on the border of the visible ClickablePanel nearest the point clicked. That works because the third constructor makes the whole sketch a Clickable panel. But the order of the panels in the Clickable group gives priority to the panels constructed with the first and second constructors.
Each of the panels is created from its own extension of the ClickablePanel. Each of those classes implements its own drawOnPanel() method.
Fields x, y, w, and h are for location of the top left corner, width, and height of the button. If you want a transparent panel, use the second or third constructors or use color 0x00FFFFFF.
class ClickablePanel
extends Clickable
This class illustrates that Clickable items do not have to be based on buttons. In this case, the location of the click is recorded and can be obtained by getClickX() and getClickY(). A dummy function drawOnPanel() was added to make it easy to extend the panel to include some kind of drawing function. The items in ClickableMeter.pde and Dragger.pde extend this item.
int clickX, clickY - X and Y coordinates of the mouse click. boolean invisible - If true, the panel is transparent. boolean wholeSkectch - If true, panel occupies the entire sketch. |
** ClickablePanel(int leftX, int topY,
int theWidth, int theHeight, String theLabel, color stdColor); - Creates a panel at the specified location with the specified size and color. When clicked, it records the location of the mouse. It has a do nothing drawing method that can be overridden. "theLabel" is not displayed. |
** ClickablePanel(int leftX, int topY,
int theWidth, int theHeight, String theLabel); - Creates a panel at the specified location with the specified size but is invisible. When clicked, it records the location of the mouse. It has a do nothing drawing method that can be overridden. "theLabel" is not displayed. |
** ClickablePanel(String theLabel); - Creates a panel that fills the entire sketch but is invisible. When clicked, it records the location of the mouse. It has a do nothing drawing method that can be overridden. "theLabel" is not displayed. Important: Normally one wants any other Clickable item on top of the background and get any clicks. Hence, almost always, the object created with this constructor should be the first item in the Clickable group's array. Assuming this panel is created before sketch size is set, the setup method must use adjustPanel() to set the size of the panel and to exclude menu and ScrollSketch scrollbars, if used. |
* void adjustPanel(boolean
hasMenu, boolean hasScrollBars) - Normally required when the third constructor is used to set a full sketch ClickablePanel. It could be used with the second constructor to accomplish the same result. Unfortunately the sketch's width and height are not known until these values have been set at the beginning of the setup() method Then resets the size of the ClickablePanel to deleted the area used a menu and/or a ScrollSketch scrollbars. It could be used with constructor 1 but this combination is not normally useful. Use in the setup() method. |
* void display() - Draws the panel. The drawing method drawOnPanel() is called after the panel is drawn in case the class is extended with a meaningful drawing method. |
** int getClickX() - Returns clickX, that is the x value of the click relative to the whole sketch. |
** int getClickY() - Returns clickY, that is the y value of the click relative to the whole sketch. |
** int getPanelX() - Returns the x distance between the left side of the panel and the click. |
** int getPanelY() - Returns the y distance between the top of the panel and the click. |
** void setClickX(int newClickX) - Sets the clickX value but it is constrained to area of the panel. |
** void setClickY(int newClickY) - Sets the clickY value but it is constrained to area of the panel. |
** void isOver() - Determines if the cursor mouse is over the panel. If it is. then "over" is set true and it returns true. Always returns false if stillSearching_ is false. |
* boolean checkForClick() Returns true if the ClickablePanel has been clicked after click location is recorded in "mouseX" and "mouseY". |
** void setColor(color stdColor) - Sets the color of the panel. |
** void drawOnPanel() - If the user program wants to draw on the panel, it is best to extend this class and overwrite this method just in case any Clickable items overlap the panel. Doing so is the way to make sure that the drawing is not on top of those items. The default procedure does nothing. Note: The drawing is not automatically limited to the panel so some care may be needed in setting up the drawing. The extended class must include a constructor which calls super(...) to one of ClickablePanel's constructors and the revised drawOnPanel() method. |
Method use codes:
** This constructor or method is designed to be called in the user sketch.
* This method is normally not called in the user's sketch unless the Clickable item was not been included in a ClickableGroup. ClickableGroup automatically uses the method as needed. Exception: This method may be needed if the user is extending Clickable with a new class.
# This constructor or method is rarely used in the user program. In Java, it would probably marked "private". Clickable, ScrollBar, and some other abstract constructors would never be called directly by the user sketch because they do nothing useful unless the user is extending a class.
Inherited from Clickable
Fields
NOTHING_CLICKED,
COMBO_BUTTON,
DISABLED_COLOR,
borderColor,
c,
clicked,
clickedItemNum,
colorState,
enabled,
h,
label,
over,
overC,
scrollable,
selected,
selectedItemNum,
visible,
w,
x,
y
Constructors
Clickable
Methods
checkForClick,
display,
doNotSelect,
getClickedItemNum,
getColorState,
getH,
getItem,
getLabel,
getSelected,
getSelectedItem,
getSelectedItemLabel,
getSelectedItemNum,
getW,
getX,
getY,
hasBeenClicked,
hideSomething,
isOver,
setBorderColor,
setColorState,
setH,
setLabel,
setScrollable,
setSelected,
setSelectedItemNum,
setEnabled,
setVisible,
setW,
setX,
setY,
sX,
sY
Revised: 04/11/2014