Tuesday, February 7, 2017

Java Code to Make a Rectangular Button

/**
*[Button]
*
*NAME:       [Gunarath A. M. Raveen]
*Assignment: []
*QUESTION:   []
*
*PURPOSE:    [This is a subclass of the GUIelement that will make a rectangular button]
*/
public class Button extends GUIelement {

  //constructor that will initialize the superclass constructor with highlighted to false.
  public Button(double xc,double yc,double hw,double hh,String title){
    super(xc,yc,hw,hh,title,false);
  }//constructor
  
  //This will overide the superclass method and draw a rectangular button and if the button 
  // is highlighted, it should be gray and otherwise it is white. 
  public void draw(){
    if (super.isHighlighted()){
      StdDraw.setPenColor(StdDraw.LIGHT_GRAY);
      StdDraw.filledRectangle(xCentre,yCentre,halfWidth,halfHeight);
      StdDraw.setPenColor(StdDraw.BLACK);
      StdDraw.setPenRadius(); 
      StdDraw.rectangle(xCentre,yCentre,halfWidth,halfHeight);
      StdDraw.text(xCentre,yCentre,text);
    }
    else{
      StdDraw.setPenColor(StdDraw.WHITE);
      StdDraw.filledRectangle(xCentre,yCentre,halfWidth,halfHeight);
      StdDraw.setPenColor(StdDraw.BLACK);
      StdDraw.setPenRadius(); 
      StdDraw.rectangle(xCentre,yCentre,halfWidth,halfHeight);
      StdDraw.text(xCentre,yCentre,text);
    }
  }
  //If the mouse click is on the button, button should highlight and redraw. This should
  //remain this way until user release the button and true should be returned. Otherwise
  //false should be returned.
  public boolean handleClick(double x, double y){
    if(super.handleClick(x,y)){
        highlighted = true;
        draw();
        Utilities.waitMouseUp();
        highlighted =false;
        draw();
        return true;
    }
    else
      return false;

No comments:

Post a Comment