Computer Science 252
Problem Solving with Java
Spring 2015, The College of Saint Rose
ShirtsAndPants Demo
A working demo of ShirtsAndPants will appear below. Click inside the applet to interact with it.
ShirtsAndPants BlueJ Project
Click here to download a BlueJ project for ShirtsAndPants.
ShirtsAndPants Source Code
The Java source code for ShirtsAndPants is below. Click on a file name to download it.
import objectdraw.*; import java.awt.*; import java.util.Random; /* * A fancier version of the laundry sorting simulation * that uses nicer laundry baskets and which can * use any laundry object which implements the * Laundry interface. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CSCI 134 * * $Id: ShirtsAndPants.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class ShirtsAndPants extends WindowController { // Constants controlling basket locations private static final int BASKETTOPS = 120; private static final int BASKETLEFT = 20; private static final int BASKETOFFSET = 100; // Constants controlling shirt's position and size private static final int ITEMLEFT = 115; private static final int ITEMTOP = 10; // Location where score should be displayed private static final int SCORELINE = 200; private static final int SCORELEFT = 40; //The piece of laundry to be sorted. private Laundry item; // baskets for white, dark, and color washing machines. private LaundryBasket white; private LaundryBasket dark; private LaundryBasket colors; // The basket corresponding to shirt's color private LaundryBasket correct; // Determines if next laundry should be a tshirt or a pair of pants private Random laundryGenerator = new Random(); // Previously noted position of mouse cursor private Location lastPoint; // counters to measure trainees accuracy private int numsorted, mistakes; // Display of current result private Text scoreDisplay; // Initializes the applet by constructing all the objects. public void begin(){ white =new LaundryBasket(BASKETLEFT + 0*BASKETOFFSET, BASKETTOPS,"whites",canvas); dark =new LaundryBasket(BASKETLEFT + 1*BASKETOFFSET, BASKETTOPS,"darks",canvas); colors=new LaundryBasket(BASKETLEFT + 2*BASKETOFFSET, BASKETTOPS,"colors",canvas); item= new TShirt(ITEMLEFT,ITEMTOP,canvas); numsorted = 0; mistakes = 0; scoreDisplay = new Text("Correct: " + numsorted + " Incorrect: " + mistakes, SCORELEFT,SCORELINE,canvas); scoreDisplay.setFontSize(13); } // Whenever mouse is depressed, note its location public void onMousePress(Location point) { lastPoint = point; } // If mouse is dragged from a position within the item, // move the item with it public void onMouseDrag(Location point) { if (item.contains(lastPoint)) { item.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY()); lastPoint = point; } } // Checks if the item has been place in the correct basket public void onMouseRelease(Location point){ if (item.contains(lastPoint)) { // Determine correct basket if ( item.colorValue() > 600 ) { correct=white; } else if ( item.colorValue() > 250 ) { correct=colors; } else { correct=dark; } } // if the object was dragged to the correct basket if (correct.contains(point)) { numsorted++; item.removeFromCanvas(); // Make a new item if (laundryGenerator.nextInt(2) == 1) { item = new TShirt(ITEMLEFT,ITEMTOP,canvas); } else { item = new Pants(ITEMLEFT,ITEMTOP,canvas); } } else { mistakes++; item.moveTo(ITEMLEFT, ITEMTOP); } scoreDisplay.setText("Correct: " + numsorted + " Incorrect: " + mistakes); } }
import objectdraw.*; /* * A Java interface that lists the methods required by * the "laundry" items in a more advanced version of * the laundry sorter program. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CSCI 134 * * $Id: Laundry.java 2218 2013-10-18 14:06:39Z terescoj $ */ public interface Laundry { // Move the laundry relative to its current position // Parameter: // xOffset - distance to move horizontally // yOffset - distance to move vertically public void move(double xOffset, double yOffset); // Move the laundry to a specific position: // x - x coordinate // y - y coordinate public void moveTo(double x, double y); // Remove the laundry from the canvas public void removeFromCanvas(); // Return true if the point is in the laundry // Parameters: // pt - the point to test for containment public boolean contains(Location pt); // Return the sum of the redness, blueness, and greenness // of the laundry item. public int colorValue(); }
import objectdraw.*; import java.awt.*; /* * A slightly fancier laundry basket object, encapsulating * the basket with its label. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CSCI 134 * * $Id: LaundryBasket.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class LaundryBasket { // Constant controlling basket sizes private static final int BASKETSIZE = 50; // Constants controlling text positioning private static final int TEXTDOWNSET = 30; private static final int TEXTINSET = 5; // boundary of basket private FramedRect box; // Create a new laundry basket // // x,y -- location of basket's upper left corner // label -- text to display within basket // canvas - the drawing canvas public LaundryBasket(double x, double y, String label , DrawingCanvas canvas) { box = new FramedRect(x, y, BASKETSIZE, BASKETSIZE, canvas); new Text(label, x + TEXTINSET, y + TEXTDOWNSET, canvas).setFontSize(13); } // Test to see if the basket contains a point public boolean contains(Location point) { return box.contains(point); } }
import objectdraw.*; import java.awt.*; import java.util.Random; /* * A graphical object that looks a bit like pants, * which implements the "Laundry" interface. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CSCI 134 * * $Id: Pants.java 2218 2013-10-18 14:06:39Z terescoj $ */ public class Pants implements Laundry { // constants to define the dimensions of the pants // components private static final double SIZE = 90; private static final double HIP_WIDTH = SIZE / 3; private static final double HIP_HEIGHT = SIZE / 3; private static final double LEG_WIDTH = HIP_WIDTH / 2 - 3; private static final double LEG_HEIGHT = SIZE - HIP_HEIGHT; // rectangles that form a border around private FramedRect leftLegTrim, rightLegTrim, hipTrim; // rectangles that form the interior color private FilledRect leftLeg, rightLeg, hips; // the initial location of the pants private double startX, startY; // Current color of the pants private Color hue; // Random number generator used to select colors private Random generator = new Random(); // create a new pair of pants with its upper left corner at (x,y) public Pants(double x, double y, DrawingCanvas canvas) { // create boundary rectangles leftLegTrim = new FramedRect(x, y + HIP_HEIGHT, LEG_WIDTH, LEG_HEIGHT, canvas); rightLegTrim = new FramedRect(x + HIP_WIDTH - LEG_WIDTH, y + HIP_HEIGHT, LEG_WIDTH, LEG_HEIGHT, canvas); hipTrim = new FramedRect(x, y, HIP_WIDTH, HIP_HEIGHT, canvas); // create interior rectangles leftLeg = new FilledRect(x+1, y+HIP_HEIGHT-1, LEG_WIDTH-1, LEG_HEIGHT+1, canvas); rightLeg = new FilledRect(x+HIP_WIDTH-LEG_WIDTH+1, y+HIP_HEIGHT-1, LEG_WIDTH-1, LEG_HEIGHT+1, canvas); hips = new FilledRect(x+1, y+1, HIP_WIDTH-1, HIP_HEIGHT-1, canvas); // remember the starting location for re-set startX = x; startY = y; setColor(); } // move the pants by specified offsets. public void move(double xOffset, double yOffset) { hips.move(xOffset,yOffset); leftLeg.move(xOffset,yOffset); rightLeg.move(xOffset,yOffset); hipTrim.move(xOffset,yOffset); leftLegTrim.move(xOffset,yOffset); rightLegTrim.move(xOffset,yOffset); } // move the pants to a specific position. public void moveTo(double x, double y) { move(x - hips.getX(), y - hips.getY()); } // remove the pants from the canvas public void removeFromCanvas() { hips.removeFromCanvas(); leftLeg.removeFromCanvas(); rightLeg.removeFromCanvas(); hipTrim.removeFromCanvas(); leftLegTrim.removeFromCanvas(); rightLegTrim.removeFromCanvas(); } // returns true if the pants contains the point; false otherwise public boolean contains(Location pt) { return hips.contains(pt) || leftLeg.contains(pt) || rightLeg.contains(pt); } // pick a new random color for the pants private void setColor() { hue = new Color(generator.nextInt(256), generator.nextInt(256), generator.nextInt(256)); hips.setColor(hue); leftLeg.setColor(hue); rightLeg.setColor(hue); } // get the sum of the components of the pant's color public int colorValue() { return hue.getRed() + hue.getBlue() + hue.getGreen(); } }
import objectdraw.*; import java.awt.*; import java.util.Random; /* * An alternate T-shirt object, this one implements the * "Laundry" interface. * * Jim Teresco, The College of Saint Rose, Fall 2013 * Based on example from Williams College CSCI 134 * * $Id: TShirt.java 2218 2013-10-18 14:06:39Z terescoj $ */ // by including "implements Laundry" here, we are promising // to provide implementations of all methods that are // listed in the Laundry interface specification. public class TShirt implements Laundry { // constants to define the size of the shirt // note how in this case, all are defined relative // to a single "SIZE" that we can change and // have all components change in an appropriate // proportion. private static final double SIZE = 120; private static final double SLEEVE_WIDTH = SIZE; private static final double SLEEVE_HEIGHT = 0.2*SIZE; private static final double BODY_WIDTH = 0.6*SIZE; private static final double BODY_HEIGHT = (0.65)*SIZE; private static final double BODY_INSET = 0.2*SIZE; private static final double NECK_WIDTH = 0.3*SIZE; private static final double NECK_HEIGHT = 0.06*SIZE; private static final double NECK_INSET = 0.35*SIZE; // rectangles that form a border around the shirt private FramedRect sleeveTrim, bodyTrim; private FramedOval neckTrim; // rectangles that form the interior color of the shirt private FilledRect body, sleeves; private FilledOval neck; // Current color of the shirt private Color hue; // Random number generator used to select next object private Random generator = new Random(); // create a new T-shirt with its upper left corner at (x,y). public TShirt(double x, double y, DrawingCanvas canvas) { // create boundary rectangles sleeveTrim = new FramedRect(x, y + NECK_HEIGHT/2, SLEEVE_WIDTH, SLEEVE_HEIGHT, canvas); bodyTrim = new FramedRect(x + BODY_INSET, y + NECK_HEIGHT/2, BODY_WIDTH, BODY_HEIGHT, canvas); // create interior rectangles sleeves = new FilledRect(x+1, y+NECK_HEIGHT/2+1, SLEEVE_WIDTH-1, SLEEVE_HEIGHT-1, canvas); body = new FilledRect(x+BODY_INSET+1, y+NECK_HEIGHT/2+1, BODY_WIDTH-1, BODY_HEIGHT-1, canvas); // give it a neck hole neck = new FilledOval(x + NECK_INSET, y, NECK_WIDTH, NECK_HEIGHT, canvas); neckTrim = new FramedOval(x + NECK_INSET, y, NECK_WIDTH, NECK_HEIGHT, canvas); // set the interior color setColor(); } // move the t-shirt by specified offsets. public void move(double xOffset, double yOffset) { body.move(xOffset,yOffset); neck.move(xOffset,yOffset); sleeves.move(xOffset,yOffset); bodyTrim.move(xOffset,yOffset); sleeveTrim.move(xOffset,yOffset); neckTrim.move(xOffset,yOffset); } // remove the t-shirt from the canvas. public void removeFromCanvas() { body.removeFromCanvas(); neck.removeFromCanvas(); sleeves.removeFromCanvas(); bodyTrim.removeFromCanvas(); sleeveTrim.removeFromCanvas(); neckTrim.removeFromCanvas(); } // move the t-shirt to a new upper-left coordinate position public void moveTo(double x, double y) { move(x - sleeves.getX(), y - neck.getY()); } // returns true if the t-shirt contains the point; false otherwise public boolean contains(Location pt) { return body.contains(pt) || sleeves.contains(pt) || neck.contains(pt); } // pick a new random color for the shirt private void setColor() { hue = new Color(generator.nextInt(256), generator.nextInt(256), generator.nextInt(256)); body.setColor(hue); sleeves.setColor(hue); neck.setColor(hue); } // get the sum of the components of the shirt's color public int colorValue() { return hue.getRed() + hue.getBlue() + hue.getGreen(); } }