This code can make a text box that can be use for different user inputs.
/**
/**
*[TextBox]
*
*
*NAME: [Gunarath A. M. Raveen]
*Assignment: []
*QUESTION: []
*
*PURPOSE: [This is a subclass of the GUIelement which will make a input and output
* text boxes]
*/
public class TextBox extends GUIelement {
private static final double MARGIN = 0.005;
private boolean isInputBox;
//constructor that will initialize the superclass constructor with highlighted to false
//and initialize the isInputBox variable.
public TextBox(double xc,double yc,double hw,double hh,
String txt,boolean inp){
super(xc,yc,hw,hh,txt,false);
isInputBox = inp;
}//constructor
//This will use the superclass draw methos to draw a rectangle with text inside the box.
public void draw(){
super.draw();
StdDraw.textLeft(xCentre-halfWidth+MARGIN,yCentre,text);
}
//If the mouse click is on the textbox and it is a input box, text inside the box should
//be cleared and return true.Otherwise it should return false.
public boolean handleClick(double x, double y){
if(super.handleClick(x,y)){
if(isInputBox){
text = "";
draw();
return true;
}
else
return false;
}
else
return false;
}
//If the bow is a input box, this should add typed char to the end of the text.
public boolean handleCharTyped(char c){
if (isInputBox){
text +=c;
draw();
return true;
}
else
return false;
}
//This will change the text in the box to given string and redraw the box.
public void displayText(String s){
text = s;
draw();
}
}
No comments:
Post a Comment