Graphics editor

Report a typo

Imagine you are writing a graphics editor. It will have a canvas and a set of tools to work with. Each tool will do a specific job and the editor will register whether the mouse buttons are pressed or released to decide which action must be done with the selected tool. Don't worry, you don't have to write a real graphics editor. In this code challenge, you just have to print the corresponding action to the terminal. Also, a mouseUp event will alsways follow a mouseDown event so you don't need to worry about their order either.

  • If the SELECTION tool is selected, print Started selection in response to a mouseDown event and Finished selection in response to a mouseUp event.
  • If the DRAW tool is selected, print Drawing a %color% line in response to a mouseDown event and Finished drawing a %color% line in response to a mouseUp event where %color% is the value of the color field in the Canvas class.
  • If the ERASER tool is selected, print Selecting figures to delete in response to a mouseDown event and Erased selected figures in response to a mouseUp event.

Use the provided template and the power of the State pattern to complete this task.

Sample Input 1:

draw green

Sample Output 1:

Drawing a green line
Finished drawing a green line

Sample Input 2:

selection

Sample Output 2:

Started selection
Finished selection
Write a program in Java 17
import java.util.Scanner;

interface Tool {

void onMouseUp();

void onMouseDown();
}

class Canvas {
private Tool tool;
private String color = "black";

void mouseDown() {

}

void mouseUp() {

}

public void setTool(Tool tool) {
this.tool = tool;
}

public Tool getTool() {
return tool;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}
___

Create a free account to access the full topic