Required value for the move command

Assignment Help Basic Computer Science
Reference no: EM13162687

Shape and color validation should be accomplished by using arrays containing strings that describe valid shape (i.e., "square" and "circle") and color choices (i.e., "red" and "blue"). So, instead of using string literals, your program traverse through the appropriate validation array, testing each element in the array to see if it is present in the command string. The validation arrays should be declared as class variables for easy access in your methods.

Shape data will be saved in a set of arrays in addition to drawing the shape to the screen (the same as in Project 2). You will also need to create an initialization method for the shape data arrays. This method will be called when your program starts. The shape data arrays should also be class variables to make it easier for your methods to access them.

The loop for obtaining drawing commands should be changed to a command processing loop that now reacts to different commands that the user can give. If the user types "quit" or "exit", the program will exit. If the user types "animate", the program will draw the shapes one at a time, pausing between each. All other commands will be passed to drawShape (the same as in Project 2).

Methods

Implementing this project can proceed as follows:

Panel Width and Height Class Variables

Declare and initialize class variables for PANEL_WIDTH and PANEL_HEIGHT and use those when you create your DrawingPanel object in your main. Using class variables will make it easier for your other methods to know the width and height of the drawing area.

Shape and Color Validation Arrays

Declare and initialize

a validation array of Strings for shapes, containing "square" and "circle" as a class variable
and a validation array of Strings for color, containing "red" and "blue" as a class variable

The findShape Method

This method should be changed to use an array to determine if a valid shape choice is present in the String parameter, cmd. You should traverse the array using a for loop, checking if any of the elements are present ("square" or "circle"). The method can return as soon as one of the elements is detected. As before, the method should return the element found (if any) or "UNKNOWN" if none of the validation array elements are found.

To traverse a validation array, use a for loop that iterates from 0 to the last element in the validation array. Recall that the last element in array can be accessed using the array's length attribute - 1.

The findColor Method

This method should be changed to use an array to determine if a valid color choice is present in the String parameter, cmd. You should traverse the array using a for loop, checking if any of the elements are present ("red" or "blue"). As before, the method should return the element found (if any) or "UNKNOWN" if none of the validation array elements are found.

Shape Data Arrays

Declare as class variables:

an array of Strings for shape type
an array of Colors for shape colors
an int array for shape size
an int array for shape x coordinates
an int array for shape y coordinates

You will also need an index that keeps track of the next available index in which to store shape data (the first shape data will be stored as index 0). You should also declare a class constant MAX_SHAPES that describes the maximum number of shapes that may be stored and the size of each of these five arrays.

The initShapeArrays Method

This method should use a for loop to iterate through each element in the shape data arrays and intialize Strings to empty strings (""), Colors to null, and ints to 0. This method should be called before your main method enters the command processing loop. Iterating through all elements in the shape arrays is accomplished using a for loop that iterates from 0 (the first element) to MAX_SHAPES - 1 (the last element). You may also use one of the array's .length values instead of MAX_SHAPES. If you do so, don't forget to subtract 1 from the length for your test.

The drawShape Method

This method should be modified to save shape data into the shape data arrays once all fields are validated. This can occur before or after drawing the shape indrawShape. You should save the type of shape ("square" or "circle") in the shape type array, the shape's color in the shape color array (Color.RED or Color.BLUE), and the size, x, and y in the appropriate shape int arrays. The index to use for saving the shape data is determined by the current shape index mentioned above. The index should be incremented after shape data are saved and the index should wrap around once the index is equal to MAX_SHAPES - 1.

The current index variable should be used as a simple array index in each of the shape data arrays where the data should be saved. Once all of the shape data are saved in the elements pointed to by the current index, the current index should then be incremented to point to the next spot in the arrays. This is where the next shape will saved. Please note that once the current index equals MAX_SHAPES, it should be reset to 0. This allows the user to overwrite previous shape data with new shape data. I.e., once MAX_SHAPES have been input, new shapes will overwrite the oldest shapes.

Like in Project 2, you can use Project3.getValue(cmd, index) to get the value after a feature.

The main Method

The main method should be changed to use a while loop and boolean flag for a command input and processing loop. If the user types "quit" or "exit", the program will exit. If the user types "animate", the program will clear the window and then draw all shapes in the shape data arrays one at a time. All other commands will be processed as shape drawing commands using drawShape. Ill-formatted commands will be ignored. Note: Remove NUM_COMMANDS as there is now no limit to the number of commands a user may input, only a maximum number of shapes that may be stored.

Your command processing loop should continue to loop as long as your boolean flag evaluates to true. If the user enters a command that equals "quit" or "exit", you should set your flag to false so that the loop will exit and the program will end. If the user enters "animate" followed by some number, the command processor will call the animate method and pass the user's command string (see the below description of animate for more details). If the user enters any other string, the command string will be passed to drawShape.

The animate Method

The animate method has a single integer field when called. The field accompanying animate is the number of cycles to make through the drawing data. For example, typing "animate 2" will make two drawing passes through the shape data. A single drawing pass proceeds as follows: the first shape in the arrays will be drawn immediately. Your method should then sleep for 500 ms, then clear the first shape and draw the second shape, then sleep for 500 ms, clear the second shape and draw the third shape, and so on. Any shape type that is an empty string (use the string's equals method or length method to test this) will not be drawn.

The user must provide the animate command with an integer value representing the number of animation cycles. For example, if the user enters "animate 5", then your animate method will draw each shape, one after another, 5 times. I.e., it will cycle through the shape data and draw each shape five times. If no integer value is provided with the animate command then animate will draw the shapes once (i.e., a single pass or cycle through the shapes). If the value provided with the animate command is something other than an integer, your method should print "Ill-formatted command" and return. You must also check that the integer representing the number of animation cycles is > 0. Hint: use your findFeature method as a simple way to obtain the integer value of the animate argument.

The animate method will clear the panel, draw a shape, wait for half a second (i.e., 500 ms), erase the shape, and then draw the next shape. It will do this a number of times equal to the number of cycles included with the animate command (or a single time if no argument was provided with animate). A simple algorithm for animate is:

for each animation cycle (or pass)

Clear the screen by drawing a filled rectangle that fills the entire drawing panel (hint: use PANEL_WIDTH and PANEL_HEIGHT)
Loop through all shape elements stored in your shape data arrays

If the shape is not a valid shape (i.e., contains initialization values) then skip it
If drawing any shape other than the first shape, then erase the previous shape
Draw the shape
Wait for 500 ms (hint: use your panel object's sleep method)

starter code:

import java.awt.*;
import java.util.*;

public class ShapeOnDemandExt {

public static void main(String[] args){
  
DrawingPanel panel = new DrawingPanel(500, 500);
Graphics g = panel.getGraphics();
  
//use this for testing
Project3.initShapes();
String s = "red square size 100 x 10 y 10";
Project3.drawShape(panel, g, s);
s = "circle blue size 200 x 300 y 100";
Project3.drawShape(panel, g, s);
s = "red circle size 67 x 250 y 250";
Project3.drawShape(panel, g, s);
s = "blue square size 120 x 32 y 345";
Project3.drawShape(panel, g, s);
s = "animate 2";
Project3.animate(panel, g, s);
  
}
}

Project 2 code:

import java.awt.*;
import java.util.Scanner;
public class ShapeOnDemand{
public static int NUM_COMMANDS = 4;
public static Scanner KB = new Scanner(System.in);
public static void main(String[] args){
  
DrawingPanel panel = new DrawingPanel(500,500);
Graphics g = panel.getGraphics();

for (int i=0; i<NUM_COMMANDS; i++){
System.out.println("Please input a drawing command");
String cmd = KB.nextLine();
drawShape(panel, g, cmd);
}
}
  
//TO DO: implement the following methods
public static void drawShape(DrawingPanel p, Graphics g, String command){
boolean correctCommand = true;
int shape=0, size=-1, x=0, y=0;
Color c = Color.BLACK;
  
//First, find out the shape
String shapeString = findShape(command);
if (shapeString.equals("circle") ){
shape = 1;
}else if (shapeString.equals("square") ){
//to draw a square
shape = 2;
}else {
correctCommand = false;
}
  
String colorString = findColor(command);
  
//either blue or red
if ( colorString.equals("red")){
c = Color.RED;
}else if ( colorString.equals("blue")){
c = Color.BLUE;
}else{
correctCommand = false;
}
  
//3, find out size
size = findFeature("size", command);
if (size==-1) correctCommand = false;
  
//4, find out x
x = findFeature("x", command);
if (x==-1) correctCommand = false;
  
//5, find out y
y = findFeature("y", command);
if (y == -1) correctCommand = false;

//finally, report errors or draw the shape
if (correctCommand == false){
System.out.println("Ill-formed command!!!");
}else{
g.setColor(c);
if (shape == 1){
System.out.println("Drawing a " + colorString + " circle with size of "+ size +" at location ("+x+","+y+")!");
g.drawOval(x,y,size,size);
}else{
System.out.println("Drawing a " + colorString + " square with size of "+ size +" at location ("+x+","+y+")!");
g.drawRect(x,y,size,size);
}
}
}
public static String findShape(String cmd){
String resStr;
if (cmd.indexOf("circle")!=-1) {
resStr = "circle";
}else if (cmd.indexOf("square")!=-1) {
resStr = "square";
}else resStr = "UNKNOWN";
return resStr;
}

public static String findColor(String cmd){
String resStr;
if (cmd.indexOf("red")!=-1) {
resStr = "red";
}else if (cmd.indexOf("blue")!=-1) {
resStr = "blue";
}else resStr = "UNKNOWN";
return resStr;
}

public static int findFeature(String feature, String cmd){
if (cmd.indexOf(feature)==-1)
return -1;
int pos = cmd.indexOf(feature);
int value = getValue(cmd, pos + feature.length());
return value;
}
  
//this method will be provided; -1 means did not find the value
public static int getValue(String cmd, int pos){
String subcmd = cmd.substring(pos);
Scanner input = new Scanner (subcmd);
if (input.hasNextInt()){
return input.nextInt();
}else
return -1;
}
}

Extra:

Implement and test the following additional method in your project 3:

public static void moveShape(DrawingPanel p, Graphics g, String cmd) {

The moveShape command allows the user to change the x and/or y coordinate values of a specified shape. The command should validate the command, erase the previously drawn shape, change the shape's x and/or y values in the data arrays, and draw the shape in its new position. The syntax for calling the command is:

move <shape_index> [x <new_x_coordinate_value>] [y <new_y_coordinate_value>]

shape_index is a required value for the move command. It is the index of the shape in the shape data arrays that you wish to move. It should range between 0 and MAX_SHAPES - 1. Be sure to validate the index. If the index value is between the actual number of shapes in the data arrays and MAX_SHAPES - 1, then the move command will have no discernable effect. If the user fails to provide a valid shape index, then the command should print "Ill-formatted command" and return (hint: you can use findFeature from Project 2 to help you find the shape index, as well as x and y values).

You may include the argument "x" in the move command to provide a new x coordinate value. If you incude "x", then you must provide an integer argument following it, separated with a space. You may also provide a "y" argument along with an integer following it which represents the new y coordinate value. If neither x nor y coordinate values are provided, then the command should have no discernable effect.

To achieve the full extra credit for the move command, you should:

  1. Modify the command loop in your main method to recognize a move command and call the moveShape method
  2. Write your moveShape method
    1. Validate the move command's shape index and return if the command is ill-formatted.
    2. Determine if an x coordinate change is required.
    3. Determine if an y coordinate change is required.
    4. Erase the previously drawn shape indicated by the shape index (use white for the erasure color)
    5. Draw the new shape
    6. For fun: when testing your move command, call it inside a loop and use the loop's control variable as an offset to the x and/or y coordinates to be passed in the move command. Add a panel.sleep call to pause for 10 or 20 ms and you will have a simple motion loop for animating the movement of a single shape. You can then try it with moving two or more shapes in the same loop.

this is drawing panel given to us:

/*
Stuart Reges and Marty Stepp
February 24, 2007
Some modifications by Tom Bylander in 2010

The DrawingPanel class provides a simple interface for drawing persistent
images using a Graphics object. An internal BufferedImage object is used
to keep track of what has been drawn. A client of the class simply
constructs a DrawingPanel of a particular size and then draws on it with
the Graphics object, setting the background color if they so choose.

To ensure that the image is always displayed, a timer calls repaint at
regular intervals.
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;

public class DrawingPanel implements ActionListener {
private static final int DELAY = 100; // delay between repaints in millis
private static final boolean PRETTY = false; // true to anti-alias

private int width, height; // dimensions of window frame
private JFrame frame; // overall window frame
private JPanel panel; // overall drawing surface
private BufferedImage image; // remembers drawing commands
private Graphics2D g2; // graphics context for painting
private JLabel statusBar; // status bar showing mouse position

// construct a drawing panel of given width and height enclosed in a window
public DrawingPanel(int width, int height) {
this.width = width;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

statusBar = new JLabel(" ");
statusBar.setBorder(BorderFactory.createLineBorder(Color.BLACK));

panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(width, height));
panel.add(new JLabel(new ImageIcon(image)));

// listen to mouse movement
MouseInputAdapter listener = new MouseInputAdapter() {
public void mouseMoved(MouseEvent e) {
statusBar.setText("(" + e.getX() + ", " + e.getY() + ")");
}

public void mouseExited(MouseEvent e) {
statusBar.setText(" ");
}
};
panel.addMouseListener(listener);
panel.addMouseMotionListener(listener);

g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.BLACK);
if (PRETTY) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1.1f));
}

frame = new JFrame("Drawing Panel");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.getContentPane().add(statusBar, "South");
frame.pack();
frame.setVisible(true);
toFront();

// repaint timer so that the screen will update
new Timer(DELAY, this).start();
}

// used for an internal timer that keeps repainting
public void actionPerformed(ActionEvent e) {
panel.repaint();
}

// obtain the Graphics object to draw on the panel
public Graphics2D getGraphics() {
return g2;
}

// set the background color of the drawing panel
public void setBackground(Color c) {
panel.setBackground(c);
}

// show or hide the drawing panel on the screen
public void setVisible(boolean visible) {
frame.setVisible(visible);
}

// makes the program pause for the given amount of time,
// allowing for animation
public void sleep(int millis) {
panel.repaint();
try {
Thread.sleep(millis);
} catch (InterruptedException e) {}
}

// makes drawing panel become the frontmost window on the screen
public void toFront() {
frame.toFront();
}
}

 

Reference no: EM13162687

Questions Cloud

Draw a production system : Suppose that you must perform three tasks, X, Y, and Z. You can only do one task at a time, and once that task is done, you never redo it.
Reverses the characters in a character array : Write the function reverseit that reverses the characters in a character array. You must also write main that calls reverseit.
Write a program to accept a number representing : Write a program to accept a number representing how many first names the user willenter from the command line (5 names maximum), and the actual first names, fromthe command line. Create an array dynamically that has the same size as number ofnames.
Add a sentinel command to stop the loop : add a sentinel command to stop the loop. When the program is compiled, It'll ask the user to type in a palindrome. I want the user to type EXIT to end the loop.
Required value for the move command : Shape_index is a required value for the move command. It is the index of the shape in the shape data arrays that you wish to move. It should range between 0 and MAX_SHAPES - 1. Be sure to validate the index.
Write a function that takes a list of integers as argument : Write a function that takes a list of integers as argument and returns a pair consisting of the sum of the even positions and the sum of the odd positions of the list. You should not use any auxiliary functions.
Receiver decide which symbol was originally sent : How can a receiver decide which symbol was originally sent when a received "point" lies somewhere in between other points in the diagram?
Program to translates a number into the closest letter grade : Write a program that translates a number into the closest letter grade. For example, the number 2.8 (which might have been the average of several grades) would be converted to B-. Break ties in favor of the better grade
Program should assign a seat in the first class : If a person enters 1, your program should assign a seat in the first class (rows 1 - 3). If a person enters 2, your program should assign a seat in business class (rows 4 - 7). If a person enters 3, your program should assign a seat in economy class ..

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Leverage business intelligent-create meaningful information

Include how we can leverage business intelligent to create meaningful information and decision making systems to help keep cost and profits.

  Verify local police department-s findings on current case

Your computer investigation firm has been hired to verify local police department's findings on current case. Tension over the case is running high in the city.

  Explaining competition in early years of telephone industry

Was regulation alternative to competition in early years of telephone industry?

  Information technology support company-s business processes

How can information Technology support a company's business processes and decision making and give it competitive advantage? Give examples to illustrate your answer.

  How dui charges of domestic violence and influence career

Sensitive information and may end up in court as technical or expert witness. How can things like a DUI, charges of domestic violence and other items influence your career?

  Identify and explore challenges and opportunities

LO2 Identify and explore contemporary challenges and opportunities in information systems and to formulate an opinion or judgement and offer possible solutions.

  Techniques in discovering requirements for a system

What are some of the techniques in discovering requirements for a system? Which ones work best? Which ones are the most economical?

  Solve reasoning problem

If Abby is communicating, so is Randy. Vijay and Kevin are either both communicating or neither is. If Heather is communicating, then so are Abby and Kevin. Describe your reasoning.

  Explaining multiple client computers and servers

In network with multiple client computers, servers, switches and wireless access points, write down resources must be scanned for possible vulnerabilities.

  Explain professional responsibility to complete installation

You are an engineer involved in the development of a financial system. During installations, you discover that this system will make a significant number of people redundant. It is your professional responsibility to complete the installation as..

  What factors are involved in selecting architecture

Under what architecture would you classify technologies such as virtualization, cloud computing, and Web-based applications? Last, what factors are involved in selecting the architecture that is right for your organization?

  Determine decimal value on big-endian machine

A 32-bit word on the little-endian computer has decimal value of 261. Determine its decimal value on big-endian machine?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd