Enter, Display, Modify List of Shapes

Solution

Main.java

importjava.awt.Color;

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.lang.reflect.Field;

importjava.nio.charset.Charset;

importjava.nio.file.Files;

importjava.nio.file.Path;

importjava.nio.file.Paths;

importjava.nio.file.StandardOpenOption;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.List;

importjava.util.Scanner;

importjavax.swing.SwingUtilities;

public class Main {

static String inst[];

staticint arg1[], arg2[], arg3[], arg4[];

staticintnumInstructions;

staticint xc=0, yc=0;

staticint status=0;

public static void main(String[] args) throws FileNotFoundException {

Scanner scanner = new Scanner( System.in );

ArrayList<TwoD_Shape> drawings=new ArrayList<TwoD_Shape>();

TwoD_Shape shape = null;

System.out.print(“Do you want to load previous file (yes/no)?”);

String input = scanner.nextLine();

if(input.equalsIgnoreCase(“yes”)) {

//Declarations

String instruction;

String components[];

String arguments[];

String params[];

boolean error=false;

int line=0;

//Open the file drawings.txt for reading

File infile=new File(“drawings.txt”);

Scanner file=new Scanner(infile);

//Check that num instruction exists

if(!file.hasNextInt())

{

System.out.println(“Error: Number of instructions is missing!!”);

System.exit(0);

}//if !input

//read number of instructions

numInstructions=file.nextInt();

//flush the rest of the line

instruction=file.nextLine();

//Allocate array of adequate size for instructions and arguments

inst=new String[numInstructions];

arg1=new int[numInstructions];

arg2=new int[numInstructions];

arg3=new int[numInstructions];

arg4=new int[numInstructions];

 

//Read drawings.txt line by line

while(file.hasNext())

{

//Read next instruction

instruction=file.nextLine();

components=instruction.split(“[(]”);

if(components.length>2)

{

System.out.println(“Error: Invalid instruction”);

error=true;

}//if

else

{

//Extract the arguments

arguments=components[1].split(“[)]”);

//Check if the list of arguments contains only 1 ‘)’

if(arguments.length>1)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

params=arguments[0].split(“,”);

//Check the general format of the instruction

switch(components[0].trim())

{

case “line”:

inst[line]=”line”;//create an object?

if(params.length!=4)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

else

{

arg1[line]=Integer.parseInt(params[0].trim());

arg2[line]=Integer.parseInt(params[1].trim());

arg3[line]=Integer.parseInt(params[2].trim());

arg4[line]=Integer.parseInt(params[3].trim());

}

break;

case “rect”:

inst[line]=”rect”;//create an object?

if(params.length!=4)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

else

{

arg1[line]=Integer.parseInt(params[0].trim());

arg2[line]=Integer.parseInt(params[1].trim());

arg3[line]=Integer.parseInt(params[2].trim());

arg4[line]=Integer.parseInt(params[3].trim());

}

break;

case “oval”:

inst[line]=”oval”;//create an object?

if(params.length!=4)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

else

{

arg1[line]=Integer.parseInt(params[0].trim());

arg2[line]=Integer.parseInt(params[1].trim());

arg3[line]=Integer.parseInt(params[2].trim());

arg4[line]=Integer.parseInt(params[3].trim());

}

break;

case “color”:

inst[line]=”color”;//create an object?

if(params.length!=3)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

else

{

arg1[line]=Integer.parseInt(params[0].trim());

arg2[line]=Integer.parseInt(params[1].trim());

arg3[line]=Integer.parseInt(params[2].trim());

}

break;

case “move”:

inst[line]=”move”;//create an object?

if(params.length!=2)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

else

{

arg1[line]=Integer.parseInt(params[0].trim());

arg2[line]=Integer.parseInt(params[1].trim());

}

break;

case “fill”:

inst[line]=”fill”;//create an object?

if(params.length!=1)

{

System.out.println(“Error: Invalid argument list”);

error=true;

}//if

else if((params[0].trim()).equals(“true”))

arg1[line]=1;

else

arg1[line]=0;

//                           {

//    arg1[line]=(params[0].equals(“true”)==true?1:0);

//                           }

break;

default:

System.out.println(“Instruction in line ” + (line+1) + “is not valid”);

}//switch

}//else

//Increment line number

line++;

}//while input

}

else if(input.equalsIgnoreCase(“no”)){

while(true) {

System.out.println(“Choose an option: “);

System.out.println(“1.To draw a new 2D shape”);

System.out.println(“2.To remove an existing shape”);

System.out.println(“3.To modify an existing shape”);

System.out.println(“4.To quit”);

String option = scanner.nextLine();

if(option.equals(“1”)) {

System.out.println(“Which object do you want to draw?”);

String object = scanner.nextLine();

System.out.print(“Color?”);

String text = scanner.nextLine();

Color color = null;

boolean fill;

Field field = null;

try {

field = Class.forName(“java.awt.Color”).getField(text.toLowerCase());

} catch (Exception e) {

e.printStackTrace();

}

try {

color = (Color)field.get(null);

} catch (Exception e) {

e.printStackTrace();

}

System.out.print(“Filled (yes/no)?”);

String f = scanner.nextLine();

if(f.equalsIgnoreCase(“yes”)) {

fill = true;

}

else {

fill = false;

}

if(object.equalsIgnoreCase(“line”)) {

shape = new TwoD_Shape();

shape.Filled = fill;

shape.color = color;

double value;

System.out.print(“Enter x1:”);

value = scanner.nextDouble();

shape.arguments = new double[4];

shape.arguments[0] = value;

System.out.print(“Enter y1:”);

value = scanner.nextDouble();

shape.arguments[1] = value;

System.out.print(“Enter x2:”);

value = scanner.nextDouble();

shape.arguments[2] = value;

System.out.print(“Enter y2:”);

value = scanner.nextDouble();

shape.arguments[3] = value;

}

else if(object.equalsIgnoreCase(“Rectangle”)) {

shape = new TwoD_Shape();

shape.Filled = fill;

shape.color = color;

double value;

System.out.print(“Enter top:”);

value = scanner.nextDouble();

shape.arguments = new double[4];

shape.arguments[0] = value;

System.out.print(“Enter left:”);

value = scanner.nextDouble();

shape.arguments[1] = value;

System.out.print(“Enter width:”);

value = scanner.nextDouble();

shape.arguments[2] = value;

System.out.print(“Enter height:”);

value = scanner.nextDouble();

shape.arguments[3] = value;

}

else if(object.equalsIgnoreCase(“Oval”)) {

shape = new TwoD_Shape();

shape.Filled = fill;

shape.color = color;

double value;

System.out.print(“Enter x:”);

value = scanner.nextDouble();

shape.arguments = new double[4];

shape.arguments[0] = value;

System.out.print(“Enter y:”);

value = scanner.nextDouble();

shape.arguments[1] = value;

System.out.print(“Enter Diameter1:”);

value = scanner.nextDouble();

shape.arguments[2] = value;

System.out.print(“Enter Diameter2:”);

value = scanner.nextDouble();

shape.arguments[3] = value;

}

else if(object.equalsIgnoreCase(“Arc”)) {

shape = new TwoD_Shape();

shape.Filled = fill;

shape.color = color;

double value;

System.out.print(“Enter left:”);

value = scanner.nextDouble();

shape.arguments = new double[4];

shape.arguments[0] = value;

System.out.print(“Enter top:”);

value = scanner.nextDouble();

shape.arguments[1] = value;

System.out.print(“Enter width:”);

value = scanner.nextDouble();

shape.arguments[2] = value;

System.out.print(“Enter height:”);

value = scanner.nextDouble();

shape.arguments[3] = value;

System.out.print(“Enter start angle:”);

value = scanner.nextDouble();

shape.arguments[4] = value;

System.out.print(“Enter delta angle:”);

value = scanner.nextDouble();

shape.arguments[5] = value;

}

else if(object.equalsIgnoreCase(“Polygon”)) {

shape = new TwoD_Shape();

shape.Filled = fill;

shape.color = color;

System.out.print(“Enter the number of coordinates:”);

int no = scanner.nextInt();

shape.arguments = new double[2*no – 1];

for(int i=0;i<no;i++) {

System.out.print(“Enter x: “);

double value;

value = scanner.nextDouble();

shape.arguments[i] = value;

System.out.print(“Enter y: “);

shape.arguments[i+no] = value;

value = scanner.nextDouble();

}

}

else {

System.out.println(“Unknown object”);

return;

}

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

newTwoD_Shape().setVisible(true);

}

});

}

else if(option.equals(“2”)) {

System.out.println(“Enter the object id: “);

int id = scanner.nextInt();

for(int i=0; i<drawings.size(); i++) {

if(drawings.get(i).ID == id) {

drawings.remove(drawings.get(i));

}

}

}

else if(option.equals(“3”)) {

System.out.println(“Choose an option: “);

System.out.println(“1.Change color”);

System.out.println(“2.Change fill status”);

String modify = scanner.nextLine();

System.out.print(“Give the ID:”);

intmodid = scanner.nextInt();

for(int i=0; i<drawings.size(); i++) {

if(drawings.get(i).ID == modid) {

if(modify.equals(“1”)) {

System.out.print(“Give the new color:” );

String text = scanner.nextLine();

Color color = null;

boolean fill;

Field field = null;

try {

field = Class.forName(“java.awt.Color”).getField(text.toLowerCase());

} catch (Exception e) {

e.printStackTrace();

}

try {

color = (Color)field.get(null);

} catch (Exception e) {

e.printStackTrace();

}

drawings.get(i).color = color;

}

else if(modify.equals(“2”)) {

drawings.get(i).Filled = false;

}

}

}

}

else if(option.equals(“4”)){

System.out.println(“Do you want to save the final drawing (yes/no)?”);

String save = scanner.nextLine();

if(save.equalsIgnoreCase(“yes”)) {

List<String> lines = Arrays.asList(“fill(” + shape.Filled + “)”, shape.type + “(” +

shape.arguments[0] + “,” + shape.arguments[1] + “,” + shape.arguments[2] + “)”);

Path file = Paths.get(“drawings.txt”);

try {

Files.write(file, lines, Charset.forName(“UTF-8”), StandardOpenOption.APPEND);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

}

else if(save.equalsIgnoreCase(“no”)) {

System.out.println(“Good bye”);

return;

}

}

else {

System.out.println(“Invalid input”);

}

}

}

}

} 

TwoD_Shape.java 

importjava.awt.Color;

importjava.awt.Graphics;

import java.awt.Graphics2D;

importjava.awt.geom.*;

importjava.util.Random;

importjavax.swing.JFrame;

importjavax.swing.SwingUtilities;

public class TwoD_Shape extends JFrame {

/**

*

*/

private static final long serialVersionUID = 1L;

int ID;

Color color;

boolean Filled;

String type;

double[] arguments;

publicTwoD_Shape() {

super(“Drawing panel”);

Random rand = new Random();

ID = rand.nextInt(50) + 1; //initializing id random number from 1 to 50

setSize(480, 200);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

}

//Drawing the a straight line

voiddrawLines(Graphics g, double x1, double y1, double x2, double y2) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(color);

Line2D.Double line = new Line2D.Double(x1, y1, x2, y2);

if(Filled == true) {

g2d.fill(line);

}

else {

g2d.draw(line);

}

}

voiddrawRect(Graphics g, double top, double left, double width, double height) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(color);

Rectangle2D.Double rect = new Rectangle2D.Double(top, left, width, height);

if(Filled == true) {

g2d.fill(rect);

}

else {

g2d.draw(rect);

}

}

voiddrawOval(Graphics g, double x, double y, double diameter1, double diameter2) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(color);

Ellipse2D.Double oval = new Ellipse2D.Double(x, y, diameter1, diameter2);

if(Filled == true) {

g2d.fill(oval);

}

else {

g2d.draw(oval);

}

}

voiddrawArc(Graphics g, double left, double top, double width, double height, double startAngle,

doubledeltaAngle, int closure) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(color);

Arc2D.Double arc = new Arc2D.Double(left, top, width, height, startAngle, deltaAngle, closure);

if(Filled == true) {

g2d.fill(arc);

}

else {

g2d.draw(arc);

}

}

voiddrawPolygon(Graphics g, double X[]) {

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(color);

GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, X.length);

path.moveTo(X[0], X[0]+X.length);

for (int i = 1; i <X.length; i++) {

path.lineTo(X[i], X[i]+X.length);

}

path.closePath();

if(Filled == true) {

g2d.fill(path);

}

else {

g2d.draw(path);

}

}

@Override

public void paint(Graphics g) {

super.paint(g);

if(type.equalsIgnoreCase(“line”)) {

drawLines(g, arguments[0], arguments[1], arguments[2], arguments[3]);

}

else if(type.equalsIgnoreCase(“rectangle”)) {

drawRect(g, arguments[0], arguments[1], arguments[2], arguments[3]);

}

else if(type.equalsIgnoreCase(“oval”)) {

drawOval(g, arguments[0], arguments[1], arguments[2], arguments[3]);

}

else if(type.equalsIgnoreCase(“arc”)) {

drawArc(g, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]

,  Arc2D.OPEN);

}

else if(type.equalsIgnoreCase(“polygon”)) {

drawPolygon(g, arguments);

}

else {

System.out.println(“Shape is not recignized”);

}

}

}