Instructions
Objective
Write a java homework program to create footshop management system.
Requirements and Specifications
Source Code
COLOR IMAGE
import java.awt.*;
import java.awt.image.*;
public class ColorImage extends BufferedImage
{
/**
* Create a ColorImage copied from a BufferedImage
* Convert to 24-bit direct colour
* @param image The image to copy
*/
public ColorImage(BufferedImage image)
{
super(image.getWidth(), image.getHeight(), TYPE_INT_RGB);
int width = image.getWidth();
int height = image.getHeight();
for (int y=0; y
for (int x=0; x
setRGB(x, y, image.getRGB(x,y));
}
/**
* Create a ColorImage with specified size and 24-bit direct colour
* @param width The width of the image
* @param height The height of the image
*/
public ColorImage(int width, int height)
{
super(width, height, TYPE_INT_RGB);
}
/**
* Set a given pixel of this image to a specified color.
* The color is represented as an (r,g,b) value.
* @param x The x position of the pixel
* @param y The y position of the pixel
* @param col The color of the pixel
*/
public void setPixel(int x, int y, Color col)
{
int pixel = col.getRGB();
setRGB(x, y, pixel);
}
/**
* Get the color value at a specified pixel position
* @param x The x position of the pixel
* @param y The y position of the pixel
* @return The color of the pixel at the given position
*/
public Color getPixel(int x, int y)
{
int pixel = getRGB(x, y);
return new Color(pixel);
}
}
EDITOR
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
/**
* This class is the main processing class of the Fotoshop application.
* Fotoshop is a very simple image editing tool. Users can apply a number of
* filters to an image. That's all. It should really be extended to make it more
* useful!
*
* To edit an image, create an instance of this class and call the "edit"
* method.
*
* This main class creates and initialises all the others: it creates the parser
* and evaluates and executes the commands that the parser returns.
*
* @author Joseph Williams
* @version 09.01.2022
*/
public class Editor {
Parser parser;
String name;
List filters;
// Create a list of ImageCache
List cache;
// Now, instead of using a ColorImage to store the current image, we will use a ImageCache object
ImageCache currentCache;
Editor() {
filters = new ArrayList();
cache = new ArrayList();
}
/**
* Create the editor and initialise its parser.
*/
public void set() {
parser = new Parser();
}
/**
* Main edit routine. Loops until the end of the editing session.
*/
public void edit() {
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the editing session is over.
boolean finished = false;
while (!finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for using Fotoshop. Good bye.");
System.exit(0);
}
/**
* Print out the opening message for the user.
*/
private void printWelcome() {
System.out.println();
System.out.println("Welcome to Fotoshop!");
System.out.println("Fotoshop is an amazing new, image editing tool.");
System.out.println("Type 'help' if you need help.");
System.out.println();
if(currentCache != null) {
System.out.println("The current image is " + currentCache.getName());
System.out.print("Filters applied: ");
for(String f: filters) {
System.out.println(f + " ");
}
}
System.out.println();
}
/**
* Given a command, edit (that is: execute) the command.
*
* @param command The command to be processed.
* @return true If the command ends the editing session, false otherwise.
*/
private boolean processCommand(Command command) {
boolean wantToQuit = false;
if (command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
String commandWord = command.getCommandWord();
if (commandWord.equals("help")) {
printHelp();
} else if (commandWord.equals("open")) {
open(command);
} else if (commandWord.equals("save")) {
save(command);
} else if (commandWord.equals("mono")) {
mono(command);
} else if (commandWord.equals("rot90")) {
rot90(command);
} else if (commandWord.equals("look")) {
look(command);
} else if(commandWord.equals("put")) {
put(command);
} else if(commandWord.equals("get")) {
get(command);
} else if (commandWord.equals("script")) {
wantToQuit = script(command);
} else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
return wantToQuit;
}
//----------------------------------
// Implementations of user commands:
//----------------------------------
/**
* Print out some help information. Here we print some useless, cryptic
* message and a list of the command words.
*/
private void printHelp() {
System.out.println("You are using Fotoshop.");
System.out.println();
System.out.println("Your command words are:");
System.out.println(" open save look mono flipH rot90 help quit get put");
}
/**
* Load an image from a file.
* @param name The name of the image file
* @return a ColorImage containing the image
*/
private ColorImage loadImage(String name) {
ColorImage img = null;
try {
img = new ColorImage(ImageIO.read(new File(name)));
} catch (IOException e) {
System.out.println("Cannot find image file, " + name);
System.out.println("cwd is " + System.getProperty("user.dir"));
}
return img;
}
/**
* "open" was entered. Open the file given as the second word of the command
* and use as the current image.
* @param command the command given.
*/
private void open(Command command) {
if (!command.hasSecondWord()) {
// if there is no second word, we don't know what to open...
System.out.println("open what?");
return ;
}
String inputName = command.getSecondWord();
ColorImage img = loadImage(inputName);
if (img == null) {
printHelp();
} else {
name = inputName;
filters.clear();
currentCache = new ImageCache(img, name);
System.out.println("Loaded " + name);
}
}
/**
* "save" was entered. Save the current image to the file given as the
* second word of the command.
* @param command the command given
*/
private void save(Command command) {
/*if (currentImage == null) {
printHelp();
return;
}*/
if(currentCache == null) {
printHelp();
return;
}
if (!command.hasSecondWord()) {
// if there is no second word, we don't know where to save...
System.out.println("save where?");
return ;
}
String outputName = command.getSecondWord();
try {
File outputFile = new File(outputName);
//ImageIO.write(currentImage, "jpg", outputFile);
ImageIO.write(currentCache.getImage(), "jpg", outputFile);
System.out.println("Image saved to " + outputName);
} catch (IOException e) {
System.out.println(e.getMessage());
printHelp();
}
}
/**
* Check the status of the application
* -1 is a fatal error
* 0 is status as normal
* 1 system is terminating
*
*
*/
// void checkStatus(int statusCode) {
// if(statusCode == -1) {
// System.err.println("FATAL ERROR!");
// System.exit(0);
// }
// }
/**
* "look" was entered. Report the status of the work bench.
* @param command the command given.
*/
private void look(Command command) {
if(currentCache != null) {
System.out.println("The current image is " + currentCache.getName());
}
if(filters.size() > 0)
{
System.out.print("Filters applied: ");
for(String f: filters) {
System.out.println(f + " ");
}
}
// if there are images stored in cache, display them
if(cache.size() > 0) {
System.out.println("Current images in cache: ");
for(ImageCache ic: cache) {
System.out.println("\t" + ic.getCacheName());
}
}
System.out.println();
}
/**
* "mono" was entered. Convert the current image to monochrome.
* @param command the command given.
*/
private void mono(Command command) {
//ColorImage tmpImage = new ColorImage(currentImage);
ColorImage tmpImage = new ColorImage(currentCache.getImage());
//Graphics2D g2 = currentImage.createGraphics();
int height = tmpImage.getHeight();
int width = tmpImage.getWidth();
for (int y=0; y
for (int x=0; x
Color pix = tmpImage.getPixel(x, y);
int lum = (int) Math.round(0.299*pix.getRed()
+ 0.587*pix.getGreen()
+ 0.114*pix.getBlue());
tmpImage.setPixel(x, y, new Color(lum, lum, lum));
}
}
currentCache.setImage(tmpImage);
//currentImage = tmpImage;
filters.add("mono");
System.out.println("Monochromatic filter applied.");
}
/**
* "rot90" was entered. Rotate the current image 90 degrees.
* @param command the command given.
*/
private void rot90(Command command) {
// R90 = [0 -1, 1 0] rotates around origin
// (x,y) -> (-y,x)
// then transate -> (height-y, x)
//int height = currentImage.getHeight();
//int width = currentImage.getWidth();
int height = currentCache.getImage().getHeight();
int width = currentCache.getImage().getWidth();
ColorImage rotImage = new ColorImage(height, width);
for (int y=0; y
for (int x=0; x
//Color pix = currentImage.getPixel(x,y);
Color pix = currentCache.getImage().getPixel(x,y);
rotImage.setPixel(height-y-1,x, pix);
}
}
currentCache.setImage(rotImage);
//currentImage = rotImage;
filters.add("flipH");
System.out.println("Image rotated 90 degrees clockwise.");
}
/**
* The 'script' command runs a sequence of commands from a
* text file.
*
* IT IS IMPORTANT THAT THIS COMMAND WORKS AS IT CAN BE USED FOR QUICKLY TESTING
*
* @param command the script command, second word of which is the name of
* the script file.
* @return whether to quit.
*/
private boolean script(Command command) {
if (!command.hasSecondWord()) {
// if there is no second word, we don't know what to open...
System.out.println("which script");
return false;
}
String scriptName = command.getSecondWord();
Parser scriptParser = new Parser();
try (FileInputStream inputStream = new FileInputStream(scriptName)) {
scriptParser.setInputStream(inputStream);
boolean finished = false;
while (!finished) {
try {
Command cmd = scriptParser.getCommand();
finished = processCommand(cmd);
} catch (Exception ex) {
return finished;
}
}
return finished;
}
catch (FileNotFoundException ex) {
System.out.println("Cannot find " + scriptName);
return false;
}
catch (IOException ex) {
throw new RuntimeException("Panic: script barfed!");
}
}
/**
* "Quit" was entered. Check the rest of the command to see whether we
* really quit the editor.
* @param command the command given.
* @return true, if this command quits the editor, false otherwise.
*/
private boolean quit(Command command) {
if (command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
} else {
return true; // signal that we want to quit
}
}
/**
* "put" was entered. Add the current image to the cache
* @param command the command given
*/
private void put(Command command) {
if(currentCache == null) {
printHelp();
return;
}
if (!command.hasSecondWord()) {
// if there is no second word, we don't know where to save...
System.out.println("put with what name?");
return ;
}
String cacheName = command.getSecondWord();
// Check that the current cacheName does no exists in the cache list
boolean exists = false;
for(ImageCache ic: cache) {
if(ic.getCacheName().equals(cacheName)) {
exists = true;
break;
}
}
if(exists) {
System.out.println("There is already an image saved with that name in the cache.");
return;
}
currentCache.setCacheName(cacheName);
cache.add(currentCache);
currentCache = null;
name = "";
System.out.println("Image stored in cache with the name: " + cacheName);
}
/**
* "get" was entered. Get an image from cache and set it as current image (cache)
* @param command the command given
*/
private void get(Command command) {
if (!command.hasSecondWord()) {
// if there is no second word, we don't know where to save...
System.out.println("get with what name?");
return ;
}
String cacheName = command.getSecondWord();
// Check that the current cacheName does no exists in the cache list
boolean exists = false;
int idx = 0;
for(ImageCache ic: cache) {
if(ic.getCacheName().equals(cacheName)) {
exists = true;
break;
}
idx++;
}
if(!exists) {
System.out.println("There is no image saved with that name in the cache.");
return;
}
currentCache = cache.remove(idx);
System.out.println("Image with name '" + currentCache.getCacheName() + "' set as current working image.");
}
}
PARSER
import java.io.FileInputStream;
import java.util.Scanner;
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input
public Parser() {
this.commands = new CommandWords();
this.reader = new Scanner(System.in);
}
public void setInputStream(FileInputStream str) {
reader = new Scanner(str);
}
/**
* @return The next command from the user.
*/
public Command getCommand()
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
String word3 = null;
System.out.print("> "); // print prompt
inputLine = reader.nextLine();
// Find up to two words on the line.
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next(); // get second word
}
if(tokenizer.hasNext()) {
word3 = tokenizer.next(); // get second word
// note: we just ignore the rest of the input line.
}
}
tokenizer.close();
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if(commands.isCommand(word1)) {
return new Command(word1, word2, word3);
}
else {
return new Command(null, word2, word3);
}
}
}
Similar Samples
Visit ProgrammingHomeworkHelp.com to explore our curated collection of sample programming assignments. These examples highlight our expertise in solving diverse coding problems across various languages and complexities. Whether you're a student seeking clarity or a professional in need of reliable solutions, our samples showcase our commitment to delivering top-quality programming assistance tailored to your academic or professional needs.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java