Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help Im trying to get my program to change color when a Tshirt is dropped into the right or wrong basket. package tShirtPackage; import objectdraw.*;

Help Im trying to get my program to change color when a Tshirt is dropped into the right or wrong basket.

package tShirtPackage; import objectdraw.*; import java.awt.*;

/** Include your name, lab section and a description of the program here.

*/ public class TShirtSorter extends WindowController { // place constant and variable declarations here private final int WHITE_BASKET_X_POSITION = 20; private final int WHITE_BASKET_Y_POSITION = 200; private final int BASKET_SIZE = 50; private final int BASKET_SPACING = 100; private final int TSHIRT_X_POSITION = 60; private final int TSHIRT_Y_POSITION = 20; Tshirt Tshirt = new Tshirt (TSHIRT_X_POSITION, TSHIRT_Y_POSITION, canvas); private final Location RIGHT_TEXT_POSITION = new Location ( 20, 300); private final Location WRONG_TEXT_POSITION = new Location ( 100, 300);

private FramedRect whiteBasket; private FramedRect darksBasket; private FramedRect colorsBasket;

private FramedRect correctBasket;

private Text rightCountText, wrongCounterText; private int rightCounter = 0; private int wrongCounter = 0; private RandomIntGenerator generator = new RandomIntGenerator ( 1,3);

private boolean dragging = false; private Location initialPoint;

public void begin() { whiteBasket = new FramedRect (WHITE_BASKET_X_POSITION, WHITE_BASKET_Y_POSITION, BASKET_SIZE, BASKET_SIZE, canvas); darksBasket = new FramedRect (WHITE_BASKET_X_POSITION + BASKET_SPACING, WHITE_BASKET_Y_POSITION, BASKET_SIZE, BASKET_SIZE, canvas); colorsBasket = new FramedRect (WHITE_BASKET_X_POSITION + 2* BASKET_SPACING, WHITE_BASKET_Y_POSITION, BASKET_SIZE, BASKET_SIZE, canvas); Tshirt = new Tshirt(TSHIRT_X_POSITION, TSHIRT_Y_POSITION, canvas); // SET UP THE DIRTY BASKET

// generate labels for the baskets new Text ( "whites", WHITE_BASKET_X_POSITION + 10, WHITE_BASKET_Y_POSITION + 20, canvas); new Text ( "darks", WHITE_BASKET_X_POSITION + BASKET_SPACING + 10, WHITE_BASKET_Y_POSITION + 20, canvas); new Text ( "colors", WHITE_BASKET_X_POSITION + 2* BASKET_SPACING + 10, WHITE_BASKET_Y_POSITION + 20, canvas);

// initialize the target basket to the white basket correctBasket = whiteBasket;

// display initial counters rightCountText= new Text ( "correct = 0", RIGHT_TEXT_POSITION, canvas); wrongCounterText = new Text ( "incorrect = 0", WRONG_TEXT_POSITION, canvas); }

public void onMousePress(Location point) { if ( Tshirt.contains (point) ) { dragging = true; initialPoint = point; } }

public void onMouseDrag(Location point) { if ( dragging) { Tshirt.move ( point.getX() - initialPoint.getX(), point.getY() - initialPoint.getY () ); Tshirt.move (point.getX() - initialPoint.getX(), point.getY() - initialPoint.getY () ); initialPoint = point; } } public void onMouseRelease(Location point) { if ( dragging) { dragging = false; Tshirt.moveTo(TSHIRT_X_POSITION, TSHIRT_Y_POSITION); Tshirt.moveTo (TSHIRT_X_POSITION, TSHIRT_Y_POSITION );

if ( correctBasket.contains (point)) { rightCounter = rightCounter + 1; rightCountText.setText ( "correct = " + rightCounter); int next = generator.nextValue (); if ( next == 1) { Tshirt.setColor (Color.white); correctBasket = whiteBasket; } else if ( next == 2) { Tshirt.setColor (Color.black); correctBasket = darksBasket; } else { Tshirt.setColor (Color.red) ; correctBasket = colorsBasket; } } else { wrongCounter = wrongCounter + 1; wrongCounterText.setText ( "incorrect = " + wrongCounter); } }

}

}

package tShirtPackage; import objectdraw.*; import java.awt.*;

public class Tshirt { 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;

// horizontal inset of body from sleeve private static final double BODY_INSET = 0.2*SIZE;

// size of neck private static final double NECK_WIDTH = 0.3*SIZE; private static final double NECK_HEIGHT = 0.06*SIZE;

// horizontal inset of neck from sleeve private static final double NECK_INSET = 0.35*SIZE;

private FramedRect sleeveTrim, bodyTrim; // rectangles that form a private FramedOval neckTrim; // border around the shirt

private FilledRect body, sleeves; // rectangles that form the private FilledOval neck; // interior color of the shirt

// 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 to white body.setColor(Color.white); neck.setColor(Color.white); sleeves.setColor(Color.white); }

public void setColor (Color newColor) { }

// 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); }

// 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); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions