Back to 111 Main Page

Mobius strip

Assignment 8

Task

Write a user interface class that lets a non-programmer use wheels to draw shapes on a frame.

Details:

Wheels

Use the most recent version of wheels: intwheels.jar

DrawingFrame.java

A DrawingFrame should have a wheels Frame instance variable, which is initialized in DrawingFrame's constructor.

Then write a drawShape() method, which takes no parameters:

public boolean drawShape()

This method will ask the user to enter all the details of the shape they want to draw: shape, x-coordinate, y-coordinate, width, height, and color. They will enter this information, separated by spaces, in a single line at the console. This method will then check that all the entered details are valid. If not, it will print out exactly what is incorrect (and return false). If everything is correct, it will draw the shape on the Frame (and return true).

Here are the steps involved in writing this method:

  • Prompt the user to enter a shape (and remind them of the required entry format).
  • Read in the line using a Scanner object. Assign each token to a variable, something like this:
        String shape = input.next();
        int xPos = input.nextInt();
        int yPos = input.nextInt();
        int width = input.nextInt();
        int height = input.nextInt();
        String color = input.next();
        input.nextLine();  //clear input stream
    
    You can assume that you will receive the correct data types from the user (ie, a word, 4 integers, and another word), but they might not be correct values (hence the need for error checking).
  • Now, using a series of if statements, verify each item.
    • Shape: can only be "rectangle" or "ellipse". When you are comparing strings, you need to use the equals method, not ==. Also, you should ignore case. (Hint: there's a equalsIgnoreCase method.)
    • xPos: should be >= 0 and < Frame._dp.getWidth()
    • yPos: should be >= 0 and < Frame._dp.getHeight()
    • width and height: should be > 0
    • color: should be "black", "red", "blue", "green", or "custom". In the first four cases, you can use the existing constant in the Color class to color the drawn shape. In the last case ("custom"), you should ask the user to enter a RGB value, and use this to create a new color. (Only do this if there no other errors in the user's shape details, though.) Valid color values are from 0 to 255.
    You may want to declare a local boolean variable named error initialized to false. For each error detected (using a conditional test), print out an error message and set error to true (in the body of that if statement).
  • After validating all the input (and creating a custom color, if necessary), draw the shape on the screen if there are no errors. (That is, error == false.)
  • Return the opposite of error

Now add a main method. This will print out the basic instructions of how to draw shapes to the user, create an instance of DrawingFrame, and call drawShape() once, giving the user a chance to specify a shape. (If you wanted the user to draw 3 different shapes, you would call drawShape() three times.)

Examples

Here is an example of the program running:

This program draws shapes for you on a graphical frame.
You can draw rectangles or ellipses. The frame is 700 by 500.
Possible colors include black, red, blue, green, and custom.

Enter a shape to draw.
Format: shape xPos yPos width height color
:circle -5 1000 5 -5 purple
Error: "circle" is an unrecognized shape.
Error: x coordinate is not within the bounds of the drawing frame (Max width: 700)
Error: y coordinate is not within the bounds of the drawing frame (Max height: 500).
Error: height must be greater than 0.
Error: "purple" is an unrecognized color choice.

The first three lines were printed in main; the rest come from drawShape(). The line in green is what was typed in by the user. No shape was drawn on the frame in this case.


Another example:

This program draws shapes for you on a graphical frame.
You can draw rectangles or ellipses. The frame is 700 by 500.
Possible colors include black, red, blue, green, and custom.

Enter a shape to draw.
Format: shape xPos yPos width height color
:RectanglE 0 0 100 100 custom
Enter the R G B color values (0-255), separated by spaces
: 100 0 100
Shape drawn.

This demonstrates how the custom color part should work. This draws a purple square (rectangle of 100 x 100) in the upper left corner of the frame.


Remember to check the RGB values:

Enter a shape to draw.
Format: shape xPos yPos width height color
:ELLIPSE 100 100 20 5 custom
Enter the R G B color values (0-255), separated by spaces
: 0 300 -2
Error: Color value out of range (0-255).


Note here that, though there was a request for a custom color, the user was not prompted for the RGB because there was already an error somewhere else:

Enter a shape to draw.
Format: shape xPos yPos width height color
:ellipse 10 10 10 -10 custom
Error: height must be greater than 0.

What to Submit

Attach your DrawingFrame.java file to an email.

FAQ

Where's the sample code from lab?
Here: NameGuesser.java
Is there a max value that you want the width and height to reach so it can still be visible in the Frame?
No max value; I don't mind if part of the shape goes off-screen. As long as the shape's upper left corner (the location of xPos and yPos) is on the screen, the shape should be visible regardless of its size. (Well, there are exceptions with certain ellipses, but we'll ignore that.)

Originally, I was going to have you calculate, based on both the position and the size, whether the shape would be partly visible. So "rectangle -50 100 100 100 blue" would be acceptable but "rectangle -50 100 10 100 blue" would not be. But that made things a bit more complicated than they needed to be. :)

My program crashes with an IllegalArgumentException if the user gives invalid RGB color values. Is this okay?
No. Only create a new Color instance if the given values are valid.

Basically, the only reason your program should be able to crash is if the user gives you characters when you ask for a number. (And I'll talk on Friday how to prevent even this.)

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
1 - Coding standards
1 - Inferface is understandable
Prompts and responses are relatively comprehensible to the uninitiated. (You are welcome to copy my prompts and error messages from the example above.)
5 - Correct implementation of above specifications
1 - Correct names
Class name: DrawingFrame. Method signature: public boolean drawShape()
1 - Main method


~ztomasze Index : TA Details: ICS111: Assignment 8
http://www2.hawaii.edu/~ztomasze
Last Edited: 01 Nov 2007
©2007 by Z. Tomaszewski.