Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this question, you will complete a set of 6 classes that will implement, in a

ID: 3677099 • Letter: I

Question

In this question, you will complete a set of 6 classes that will implement, in a StdDraw graphics window, four standard widgets that appear in graphical user interfaces (GUIs): buttons, checkboxes, radio buttons, and text boxes. (See the image on the next page.) Each will be written as a separate class (Button, Checkbox, RadioButton, and TextBox). These will all be subclasses of the superclass GUIelement. There will also be a “collection” class GUIgroup which will hold a list of GUIelement objects (and it will be a subclass of GUIelement, too). The two classes GUIelement and Checkbox will be given to you. You will write the other four. This will give you some idea of how real-life GUIs work, and how objects and OOP are used to create them. The instructions that follow may seem long, but they give a very detailed description of what each class does or needs, and it should make it easy to write them.

3.1 Look at the supplied superclass GUIelement.java. All other classes in this question will be subclasses of this one. This is an abstract class. There are 6 instance variables common to every GUIelement object: a. A set of four double values that define the rectangular area in the StdDraw window that will contain this object: xCentre, yCentre, halfWidth, and halfHeight.

b. A String variable text that will hold the contents of a text box, the label in a button, or the label beside a radio button or checkbox. Every subclass has a different use for this variable, but almost all of them will need a String for something.

c. A boolean variable highlighted which will indicate whether or not this item is selected/active/highlighted (the exact meaning depends on the type of object).

d. All 6 of these variables are protected variables because the subclasses will constantly require access to them (and it’s much easier than making them private and writing 12 get/set methods). There are the following public methods that every GUIelement will need: a. Two constructors: one which will initialize all 6 variables, and another with no parameters which leaves them at default values (0/null/false).

b. Accessor (get) methods that will return the values of the text and highlighted variables. There is no need for mutator (set) methods.

c. A void draw() method. Every GUIelement object will use this to draw itself in some appropriate way. The superclass method erases everything in its rectangle (draws a filled white rectangle over it), and then draws a thin black outline around it. Some subclasses will find this useful. Others will override it.

d. A boolean handleClick(double x, double y) method. This method is called whenever the user presses the mouse button at the point (x,y) in the StdDraw window. This method returns true if the point is within the rectangle for this object, and false otherwise. (The subclasses will all take advantage of this, but they must also take appropriate action to respond to the click.) e. A boolean handleCharTyped(char c) method. This method is called when the user types a character on the keyboard. This method always returns true if this GUIelement object knows what to do with a typed character, and false otherwise. The superclass method always returns false, which is the default action, since it has no appropriate way to use a character. Only textboxes will override this.

3.2 Look at the supplied class Checkbox.java which is a subclass of GUIelement. a. It has a constructor which accepts four parameters (xc,yc,size,title) where xc and yc are the coordinates of the centre of the box, size is both the half-height and half-width of the box (checkboxes are usually square), and title is the label that is shown to the right of the box.

b. The draw() method uses the superclass draw() method to draw the checkbox outline. It also draws its text (the title) just to the right of the box (leaving a little space between the box and the title). If the box is highlighted, it draws an X in the box (using two short lines).

c. The handleClick(double x, double y) method uses the superclass handleClick method to determine whether or not the mouse click was in this checkbox. If it was, the highlighted flag for this checkbox is toggled (changed from true to false or from false to true), the checkbox is re-drawn to show its new status, and true is returned (indicating “I handled it”).

public abstract class GUIelement {

protected double xCentre;
protected double yCentre;
protected double halfWidth;
protected double halfHeight;
protected boolean highlighted;
protected String text;

public GUIelement(double xc, double yc, double hw, double hh,
String txt, boolean hilite) {
xCentre = xc;
yCentre = yc;
halfWidth = hw;
halfHeight = hh;
text = txt;
highlighted = hilite;
}

public GUIelement(){ }
public void draw(){
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(xCentre,yCentre,halfWidth,halfHeight);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(); //Default thin line.
StdDraw.rectangle(xCentre,yCentre,halfWidth,halfHeight);
}
  
//All subclasses will need to know when a click is in the rectangle
//for this particulat GUI element.
public boolean handleClick(double x, double y) {
return (x >= xCentre-halfWidth) &&
(x <= xCentre+halfWidth) &&
(y >= yCentre-halfHeight) &&
(y <= yCentre+halfHeight);
}
public boolean handleCharTyped(char c){
return false;
}
  
public boolean isHighlighted(){
return highlighted;
}  
public String getText(){
return text;
}
}//GUIelement

public class Checkbox extends GUIelement {
  
//The small distance from the right edge of the box to the label.
private static final double MARGIN = 0.005;
  
//Constructor
public Checkbox(double xc, double yc, double size,
String txt) {
super(xc,yc,size,size,txt,false); //It's not highlighted to start with.
}//constructor
  
public void draw(){
StdDraw.setPenRadius(); //Make sure it's a standard narrow pen
super.draw(); //Let the superclass draw the outer square
StdDraw.textLeft(xCentre+halfWidth+MARGIN,yCentre,text); //Add the label
if(highlighted) {
//Draw two lines to make an X in the box.
StdDraw.line(xCentre-halfWidth,yCentre+halfHeight,
xCentre+halfWidth,yCentre-halfHeight);
StdDraw.line(xCentre-halfWidth,yCentre-halfHeight,
xCentre+halfWidth,yCentre+halfHeight);
}//if
}//draw
  
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
draw(); //And redraw it.
return true; //I handled it. Nobody else should.
}//else
}//handleClick

}//checkbox

public class Utilities {
public static void delay(){
//Delay for 1/25 of a second (40 milliseconds)
try {
Thread.sleep(40); //This will throw an exception if something
//interrupts this little nap.
}
catch(InterruptedException e){
//Catch that exception. Do nothing except return from the method.
}
}//delay
  
public static void waitMouseUp(){
//Do nothing while waiting for the mouse button to be released.
while(StdDraw.mousePressed())
delay();
}//waitMouseUp
  
}

That's the whole question.

Explanation / Answer

1. GUIgroup.java: See the code below:

----------------------------------------------

import java.util.ArrayList;
import java.util.Iterator;

/**
*
*/

/**
* GUIgroup class
*
*/
public class GUIgroup extends GUIelement{

   private ArrayList<GUIelement> GUIelements; //list of GUI elements
  
   /**
   * Default constructor
   */
   public GUIgroup()
   {
       GUIelements=new ArrayList<GUIelement>();
   }
   public void addElement(GUIelement e) {
       GUIelements.add(e);      
   }
   /* (non-Javadoc)
   * @see GUIelement#draw()
   */
   @Override
   public void draw() {
       // TODO Auto-generated method stub
       Iterator<GUIelement> iterator=GUIelements.iterator();
       while(iterator.hasNext())
       {
           GUIelement element=iterator.next();
           element.draw();
       }
   }
   /* (non-Javadoc)
   * @see GUIelement#handleClick(double, double)
   */
   @Override
   public boolean handleClick(double x, double y) {
       // TODO Auto-generated method stub
       Iterator<GUIelement> iterator=GUIelements.iterator();
       while(iterator.hasNext())
       {
           GUIelement element=iterator.next();
           if(element.handleClick(x, y))
               return true;
       }
       return false;
   }
   /* (non-Javadoc)
   * @see GUIelement#handleCharTyped(char)
   */
   @Override
   public boolean handleCharTyped(char c) {
       // TODO Auto-generated method stub
       Iterator<GUIelement> iterator=GUIelements.iterator();
       while(iterator.hasNext())
       {
           GUIelement element=iterator.next();
           if(element.handleCharTyped(c))
               return true;
       }
       return false;
   }
  
   /**
   * resetRadioButtons() method
   */
   public void resetRadioButtons()
   {
       Iterator<GUIelement> iterator=GUIelements.iterator();
       while(iterator.hasNext())
       {
           GUIelement element=iterator.next();
           if(element instanceof RadioButton)
           {
               RadioButton radioButton=(RadioButton)element;
               radioButton.reset();
           }
       }
   }
}

-----------------------------------------------------------

2. RadioButton.java: See the code below:

--------------------------------------------------------------

/**
*
* Radiobutton class
*
*/
public class RadioButton extends GUIelement {

   // The small distance from the right edge of the box to the label.
   private static final double MARGIN = 0.005;
   private GUIgroup group; // GUIgroup to which RadioButton belongs

   // Constructor
   public RadioButton(double xc, double yc, double radius, String title, boolean hilite, GUIgroup g) {
       super(xc, yc, radius, radius, title, hilite);
       group = g;
   }// constructor

   public void draw() {
       StdDraw.setPenRadius(); // Make sure it's a standard narrow pen
       StdDraw.circle(xCentre, yCentre, halfWidth);
       StdDraw.textLeft(xCentre + halfWidth + MARGIN, yCentre, text); // Add
                                                                       // the
                                                                       // label
       if (highlighted) {
           // Draw two lines to make an X in the box.
           StdDraw.circle(xCentre, yCentre, 1);
       } // if
   }// draw

   public void reset() {
       highlighted = false;
       draw();
   }

   public boolean handleClick(double x, double y) {
       // To be implemented
       if (!super.handleClick(x, y)) // The superclass method is always used
                                       // first.
           return false; // Nothing to do with me. Not in my rectangle.
       else {
           group.resetRadioButtons();
           highlighted = true;
           draw();
           return true;
       } // else
   }// handleClick

}// RadioButton

-------------------------------------------------------------------

Note: What is this StdDraw class. Where is its code? It is heavily used in the classes.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote