Create a Remote Control object class in Java Processing (.pde): Remote Control object is a Composite Object
Question:
Create a Remote Control object class in Java Processing (.pde):
Remote Control object is a Composite Object made up of:
- 1 Button object named on
- 1 Button object named off
- 1 ScrollBar object named as sbVol //for volume control
- x,y,w,h - these 4 float values are to store the location of Remote Control object
- boolean status - (if the value is true, it implies the on Button has been pressed and false implies off Button has been pressed).
- int vol - this stores the value of sbVol's mapped Value
It has following methods:
constructor : takes in 4 parameters that tell where it needs to appear and it's dimensions.
show :
- no input values
- does not return anything
- job
- draw the rect at the location it has to appear with given dimensions and then
- call show methods of on, off & sbVol.
clickedOnMe:
- no input values
- does not return anything
- job
- check if on was clicked and update the value of the attribute status accordingly.
- check if off was clicked and update the value of the attribute status accordingly
- check if sbVol was clicked and update value of the attribute vol with the value returned by sbVol's getMappedValue.
Button object - you can use this code (Button.pde), no change required:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
y //yo store the y coordinate of where to appear
buttonWidth // stores how wide the button needs to be
buttonHeight //stores how tall the button needs to be
caption // a String to store the caption or text that needs to be displayed
*/
float x, y;
float buttonWidth, buttonHeight;
String caption;
/*
Define a constructor takes 4 float values and a String value as input values :
the incoming values need to be stored in appropriate attributes*/
Button(float argX, float argY, float argbWidth, float argbHeight, String argCaption) {
x = argX;
y = argY;
buttonWidth = argbWidth;
buttonHeight = argbHeight;
caption = argCaption;
}
/*it does not take in any input values
does not return any value.
goal is to display a rectangle based on the attributes.
Button's caption should appear in the middle of button (not on top).
*/
void show() {
fill(255);
rect(x, y, buttonWidth, buttonHeight);
textSize(30);
fill(0);
text(caption, x+buttonWidth/2, y+buttonHeight/2);
}
/*Add the method clickedOnMe() inside Button.pde of this sketch :
returns Boolean value true if that particular Button has been clicked
returns false otherwise.
*/
boolean clickedOnMe() {
if ( (x-buttonWidth/2 <mouseX && mouseX<x+buttonWidth/2) &&
(y-buttonHeight/2 <mouseY && mouseY<y+buttonHeight/2) ) {
return true;
} else {
return false;
}
}
String getCaption(){
return caption;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ScrollBar object - you can start with the code below (ScrollBar.pde) and add the following method:
- method name : getMappedValue :
- it does not take in any parameters;
- it's goal is to return the mappedValue.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class ScrollBar {
float x, y, sbW, sbH;
float currMin, currMax, desiredMin, desiredMax;
float markerPos;
int mappedValue;
color clr;
//Needs all *attributes as input and assigns to the appropriate variables
ScrollBar(float argX, float argY, float argsbW, float argsbH,
float argcurrMin, float argcurrMax, float argdesiredMin, float argdesiredMax,
color argColor) {
x = argX;
y = argY;
sbW = argsbW;
sbH = argsbH;
currMin = argcurrMin;
currMax = argcurrMax;
desiredMin = argdesiredMin;
desiredMax = argdesiredMax;
markerPos = x; //assume that the initial place for marker is going to be top left
mappedValue = int(currMin);
clr = argColor;
}
//Show : code to display the scrollbar
void show(){
fill(clr);
strokeWeight(1);
rect(x, y, sbW, sbH);
stroke(0);
strokeWeight(3);
line(markerPos, y, markerPos, y+sbH);
}
boolean clickedOnMe(){
if( (x <= mouseX && mouseX<= x+sbW) &&
(y <= mouseY && mouseY <= y+sbH) ){
markerPos = mouseX;
show();
mappedValue = int(map(mouseX, currMin, currMax, desiredMin, desiredMax));
return true;
}else{
return false;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Usage file for RemoteControl object :
Declare a RemoteControl object named rc.
Inside setup:
- set size of the canvas,
- create a new RemoteControl object by sending positional values (your choice for where you want to place the remote control object, how big it needs to be) and assign it to rc.
- call rc's show method.
Inside mousePressed :
- call rc's clickedOnMe
Inside draw :
- check on rc's status
- if true, draw a white rectangle/square to denote a TV and indicate the current volume level using rc's vol
- if false, draw a black rectangle/square to denote the TV being off.