comp sci intro course question https://www.dropbox.com/sh/oyyicwn4xgx3m26/AACeqj
ID: 3679234 • Letter: C
Question
comp sci intro course question
https://www.dropbox.com/sh/oyyicwn4xgx3m26/AACeqjFTcBEP585x301Li-vta?dl=0
Implement the Text Box class which will provide both input boxes into which the user can type input, and output boxes which simply display information. This class should have one extra instance variable: a boolean value indicating whether or not this is an input box that can process characters typed by the user, or an output box that only displays text. A constructor with parameters (xc,yc,hw,hh,txt,inp) where the first four arc the usual specifications for the rectangular area, txt is the text to be displayed in the box, highlighted is false, and inp is a boolean value indicating whether or not this is an input box. Write a draw() method which will use the superclass draw method to draw the rectangular outline of this box, and then display the text inside it. The text should be left-justified, starting a small distance from the left side of the box. Write a handleClick(double x, double y) method. As usual, the superclass handleClick method will tell you whether or not the click is inside this box, and whether you should return true or false. If the click is inside this box, and if it is an input box, then the click should clear the existing text from the box (make it an empty string) and set highlighted to true (meaning the box is now ready to acccpt input). If it isn't an input box, then clicks will be ignored. Write a handleCharTyped(char c) method. This is the only type of object that can handle a typed character. If the box is an input box, and it is currently highlighted, then it should handle the character as follows: If c is not the RETURN character (' '), then append the typed character to the end of the text in the box and redraw it. If c is the RETURN character, set highlighted to false. In either ease, return true since it has now handled the character. If it is not an input box, it should return false and ignore the character. Finally, write a display Text (String s) method which will change the text in the box to s, and redraw it.Explanation / Answer
a. Note:-import javax.swing.JOptionPane; //use this package in all .java programs.
package userinput;
import javax.swing.JOptionPane;
public class InputBoxes{
public static void main (string[] args){
}
}
Note:-place this code in checkbox.java and replace the checkbox.java with textbox.java.
b) place this code where these lines are used and remaining is same in GUIelement.java
public abstract class GUIelement {
//Six instance variables that all GUI elements need.
protected double xCentre;
protected double yCentre;
protected double halfWidth;
protected double halfHeight;
protected boolean highlighted;
protected String text;
protected boolean input;
public GUIelement(double xc, double yc, double hw, double hh,
String txt, boolean hilite, boolean inp ) {
xCentre = xc;
yCentre = yc;
halfWidth = hw;
halfHeight = hh;
text = txt;
highlighted = hilite;
input=inp;
}
public boolean isinput(){
return input;
}
c)In StdDraw.java place this code in rectangle.
ewAction.addActionListener(new ActionListener() {
d) public boolean handleClick(double x, double y) {
if(!super.handleClick(x,y)) //The superclass method is always used first.
return false; //Nothing to do with me. Not in my rectangle.
else {
highlighted = !highlighted; //Toggle the box's status
<FORM>
<INPUT TYPE="Text">
<P>
<INPUT TYPE="Submit" Value="Submit">
<INPUT TYPE="Text" Size="25" Value="Empty string">
</FORM>
return true; //I handled it. Nobody else should.
}//else
}//handleClick
e)public boolean handleCharTyped(char c){
return false;
else
}
}//use this code in GuIelement .java inplace of handle char typed(char c)
f)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.