Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ILL RATED YOU HIGH! HELP, USING DR.JAVA create a drawBoxAtOffset method in drawBoxAtOffset.java. You should not modify this method; just copy it into the Picture.java

ILL RATED YOU HIGH!

HELP, USING DR.JAVA

create a drawBoxAtOffset method in drawBoxAtOffset.java. You should not modify this method; just copy it into the Picture.java file. We have also given you PSA7A.java, which implements the basic functionality of prompting the user for input to draw a box 3 times. You will need to modify the main method in the PSA7A class so that invalid entries for x,y coordinates and/or size cause the user to be re-prompted to enter valid values. Find a way to alter PSA7A.java using if-else statements in order to allow the user to input ANY numbers for a box to draw. Do not use exception handling! For example, if the user input (100,100) as the (x,y) coordinate for an image that is only 50x50, your program should print: ERROR: The specified range is out of bounds. Please choose a range that is within (0,0) and (width,height). Note that you should print the actual width and height of the picture chosen to alter (e.g. print out (600,250), not (width,height)). If the user enters constraints that will not cause an out of bounds error, then draw the box on the picture and continue. If the user enters constraints that will cause an out of bounds error, then re-prompt them. This invalid entry should not count towards the three times the box is drawn. The user should be prompted to enter four inputs each time: x, y, the width, and the height. If any of them are problematic, you should re-prompt them to input all four inputs, not just the one(s) that caused the problem. There should still be a total of three boxes drawn.

JAVA PSA7

/* * PSA7A.java * * Created by: Partner Name 1 * * Date: * * Description: This method... * */ import java.util.Scanner; import java.awt.Color; public class PSA7A { public static void main(String[] args) { /* Connecting a Scanner object to the keyboard */ Scanner keyboard = new Scanner(System.in); Color[] boxColors = {Color.RED, Color.GREEN, Color.BLUE}; /* Choosing a picture and initializing variables. */ Picture pic = new Picture(FileChooser.pickAFile()); int x, y, width, height; pic.show(); int pictureWidth = pic.getWidth(); int pictureHeight = pic.getHeight(); System.out.println("Picture loaded - width: " + pictureWidth +" height: "+ pictureHeight); // Attempt to draw 3 boxes int boxesDrawn = 0; while (boxesDrawn < 3) { /* Prompting the user for coordinates. */ String prompt = "Please enter the upper left corner (first x, then y) of "; String prompt2 = "the box to draw."; System.out.println(prompt + prompt2); /* Converting coordinates to ints. */ x = keyboard.nextInt(); y = keyboard.nextInt(); /* Prompting the user for width and height. */ System.out.println("Please enter the width of the box to flip."); width = keyboard.nextInt(); System.out.println("Please enter the height of the box to flip."); height = keyboard.nextInt(); pic.drawBoxAtOffset(x, y, width, height, boxColors[boxesDrawn]); /* Repainting the picture with the box drawn. */ pic.repaint(); boxesDrawn++; } } }

-------------------------------------

public void drawBoxAtOffset(int xStart, int yStart, int width, int height, Color color) { Pixel p = null; for (int x = xStart; x < xStart + width; x++) { for (int y = yStart; y < yStart + height; y++) { p = getPixel(x, y); p.setColor(color); } } }

------------------------------

Picture.java

import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.*; import java.util.*; import java.util.List; // resolves problem with java.awt.List and java.util.List

/** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } /** * Constructor that takes a buffered image * @param image the buffered image to use */ public Picture(BufferedImage image) { super(image); } ////////////////////// methods /////////////////////////////////////// /** * Method to return a string with information about this picture. * @return a string with information about the picture such as fileName, * height and width. */ public String toString() { String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth(); return output; } public static void main(String[] args) { String fileName = FileChooser.pickAFile(); Picture pictObj = new Picture(fileName); pictObj.explore(); } } // this } is the end of class Picture, put all new methods before this

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions

Question

What was Chester Barnards contribution to organization theory?

Answered: 1 week ago