CMSC 330 Project 1 The first programming project involves extending the Java skeleton program that it is provided in the attached . zip file. That skeleton program display

CMSC 330 Project 1 The first programming project involves extending the Java skeleton program that it is provided in the attached . zip file. That skeleton program displays a scene of graphic images contained in a scene definition file. The grammar for that scene definition file is shown below: scene – SCENE IDENTIFIER number_list images END ‘ . ‘ images – image images | image image – right_triangle | rectangle right_triangle – RIGHT_TRIANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ‘; ‘ rectangle – RECTANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ; number_list – ‘ (‘ numbers ‘ ) ‘ numbers – NUMBER | NUMBER ‘ , ‘ numbers In the above grammar, terminal symbols are upper case names or character literals shown in blue and nonterminal symbols are lower case names shown in red. EBNF metacharacters are shown in black. Tokens can be separated by any number of spaces. Identifiers and keywords are strings of alphabetic characters. Both are case sensitive. Numbers are unsigned integers. That program reads in the scene definition file that defines the image objects in a scene and creates those objects, inserts them into the scene and displays that scene. You are to modify the program so that it will parse and display the additional images defined by the expanded grammar shown below with the additions to the grammar highlighted in yellow: scene – SCENE IDENTIFIER number_list images END ‘ . ‘ images – image images | image image – right triangle | rectangle | parallelogram | regular polygon | isosceles text right_triangle – RIGHT_TRIANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ‘; ‘ rectangle – RECTANGLE_COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ‘; parallelogram – PARALLELOGRAM COLOR number list AT number list number list OFFSET NUMBER ‘;

Project1 Parser Lexer Syntax Error Scene Image 44 Text Polygon HollowPolygon SolidPolygon AA. RightTriangle Rectangle Isosceles Triangle Parallelogram Regular Polygon The classes shown in black are included in the skeleton project. You must complete the project by writing those classes shown in red and modifying the parser class so that it will parse the expanded grammar. Below is a description of each of the five classes that you must write: The Text class must contain a constructor that is supplied the color that defines the text color, a point that specifies the text location and a string containing the text to be displayed. It must also contain a draw function because it is extends the abstract class Image. The draw function must draw the text using the method drawstring in Graphics class. The solidPolygon class must contain a constructor that is passed the number of vertices in the polygon and its color. It must define the method drawPolygon because it is extends the abstract class polygon_. It should call the fillpolygon method of the Graphics class to draw a solid polygon.

olygons X Hello World The deliverables for this project include the following: 1. A . zip file containing all the source code correctly implementing all required functionality. a. All the . java files provided in the skeleton should included regardless of whether they required any changes b. All new files must include a header with the student name, date, project and a description of what the file contains c. All modified files must include a header with the same information 2. A Word or PDF file that contains the following: a. A discussion of how you approached the project b. A test plan that contains test cases that include all additional images and test any new functionality. For each test case, the output produced should be included. c. A discussion of lessons learned from the project and any improvements that could be made

Can you help with my code? It’s giving me error:

“C:Program FilesJavajdk-17.0.2binjava.exe” “-javaagent:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2021.3.2libidea_rt.jar=53396:C:Program FilesJetBrainsIntelliJ IDEA Community Edition 2021.3.2bin” -Dfile.encoding=UTF-8 -classpath C:UserstalitIdeaProjectsCMSC330proj1untitledoutproductionFrancoTalitaProj1 Main
Syntax Error on Line: 2 Expecting token SCENE not IDENTIFIER

Process finished with exit code 0
 

DrawingPanel.java

import java.awt.*;

import java.util.*;

import javax.swing.JPanel;

 

// Class that defines the panel for drawing the images
 

class DrawingPanel extends JPanel {

 

    public ArrayList<Image> images = new ArrayList<>();
 

    // Adds a graphic object to the drawing panel

 

    public void addImage(Image image) {

        images.add(image);

    }

 

    // Draws all the images on the drawing panel

 

    @Override

    public void paintComponent(Graphics graphics) {

        super.paintComponent(graphics);

        for (Image image : images)

            image.draw(graphics);

    }

}

HollowPolygon.java

import java.awt.*;

 

// Abstract class that defines all hollow polygons

 

abstract class HollowPolygon extends Polygon_ {

 

   // Constructor that calls super constructor

 

   public HollowPolygon(Color color, int vertexCount) {

       super(color, vertexCount);

   }

}

Image.java

import java.awt.*;

// Abstract base class that defines all image objects

 

abstract class Image {

 

   public Color color;

 

   // Constructor that can only be called by the subclasses to initialize the color

 

   public Image(Color color) {

       this.color = color;

   }

   // Sets the color of the image to be drawn. Must be called first by the draw function of the subclasses

 

   public void colorDrawing(Graphics graphics) {

       graphics.setColor(color);

   }

 

   abstract void draw(Graphics graphics);

}

 

IsoscelesTriangle.java

import java.awt.*;

 

public class IsoscelesTriangle extends SolidPolygon {

 

   public IsoscelesTriangle(Color color, Point topVertex, int height, int width) {

       super(color, 3);

       int[] x_points = {topVertex.x – width/2, topVertex.x, topVertex.x + width/2};

       int[] y_points = {topVertex.y + height, topVertex.y, topVertex.y + height};

       createPolygon(x_points, y_points);

   }

}

 

Lexer.java

import java.io.*;

 

// This class provides the lexical analyzer for project 1

 

class Lexer {

 

   public static final int KEYWORDS = 8;

   private final StreamTokenizer tokenizer;

   public String punctuation = “,;.()”;

   public Token[] punctuationTokens = {Token.COMMA, Token.SEMICOLON, Token.PERIOD, Token.LEFT_PAREN, Token.RIGHT_PAREN};

 

   // Constructor that creates a lexical analyzer object given the source file

 

   public Lexer(File file) throws FileNotFoundException {

       tokenizer = new StreamTokenizer(new FileReader(file));

       tokenizer.ordinaryChar(‘.’);

       tokenizer.quoteChar(‘”‘);

   }

   // Returns the next token in the input stream

 

   public Token getNextToken() throws LexicalError, IOException {

       Token result = Token.EOF;

       boolean finished = false;

       int token = tokenizer.nextToken();

       switch (token) {

           case StreamTokenizer.TT_NUMBER:

               result = Token.NUMBER;

               break;

           case StreamTokenizer.TT_WORD:

               for (Token aToken : Token.values()) {

                   if (aToken.ordinal() == KEYWORDS)

                       break;

                   if (aToken.name().replace(“_”, “”).equals(tokenizer.sval.toUpperCase())) {

                       result = aToken;

                       finished = true;

                       break;

                   }

               }

               if (finished) break;

               result = Token.IDENTIFIER;

               break;

           case StreamTokenizer.TT_EOF:

               break;

           default:

               for (int i = 0; i < punctuation.length(); i++)

                   if (token == punctuation.charAt(i)) {

                       result = punctuationTokens[i];

                       finished = true;

                       break;

                   }

               if (finished) break;

       }

       return result;

   }

 

   // Returns the lexeme associated with the current token

 

   public String getLexeme() {

       return tokenizer.sval;

   }

 

   // Returns the numeric value of the current token for numeric tokens

 

   public int getNumber() {

       return (int) tokenizer.nval;

   }

 

   // Returns the current line of the input file

 

   public int getLineNo() {

       return tokenizer.lineno();

   }

}

 

LexicalError.java

// Class that defines a lexical error

 

class LexicalError extends Exception

{

   // Constructor that creates a lexical error object given the line number and error

 

   public LexicalError(int line, String description)    {

       super(“Lexical Error on Line: ” + line + ” ” + description);

   }

}

Main.java

import java.io.*;

import java.util.*;

import javax.swing.*;

 

// Project 1 main class

class Main {

 

   // The main method of the whole program, allows the name of the scene definition file to be input on the

   // command line or selected using the file chooser dialog window. It calls the parser to parse the scene

   // definition file and add the graphic objects to the scene.

 

   public static void main(String[] args) {

       Scanner stdin = new Scanner(System.in);

       String sceneFileName;

       File sceneFile;

       Scene scene;

       JFileChooser choice = new JFileChooser(new File(“.”));

       int option = choice.showOpenDialog(null);

       if (option == JFileChooser.APPROVE_OPTION)

           sceneFile = choice.getSelectedFile();

       else {

           System.out.print(“Enter scene file name or a single space to choose file from window: “);

           sceneFileName = stdin.nextLine();

           sceneFile = new File(sceneFileName);

       }

       try {

           Parser parser = new Parser(sceneFile);

           scene = parser.parseScene();

           scene.draw();

       } catch (SyntaxError error) {

           System.out.println(error.getMessage());

       } catch (LexicalError error) {

           System.out.println(error.getMessage());

       } catch (IOException error) {

           System.out.println(“IO Error”);

       }

   }

}

 Parallelogram.java

import java.awt.*;

 

public class Parallelogram extends SolidPolygon {

   public int xOffset;

 

   public Parallelogram(Color color, Point upperLeft, Point lowerRight, int xOffset) {

       super(color, 4);

       this.xOffset = xOffset;

       int[] x_points = {upperLeft.x, lowerRight.x, lowerRight.x + xOffset, upperLeft.x + xOffset};

       int[] y_points = {upperLeft.y, lowerRight.y, lowerRight.y, upperLeft.y};

       createPolygon(x_points, y_points);

   }

}

Parser.java

import javax.swing.*;

import java.awt.*;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

 

// This class provides the skeleton parser for project 1

 

class Parser {

   public JFrame window;

   public Token token;

   public Lexer lexer;

 

   // Constructor to create new lexical analyzer from input file

 

   public Parser(File file) throws IOException {

       lexer = new Lexer(file);

   }

 

   // Parses the production

   // scene -> SCENE IDENTIFIER number_list images END ‘.’

 

   public Scene parseScene() throws LexicalError, SyntaxError, IOException {

       verifyNextToken(Token.SCENE);

       verifyNextToken(Token.IDENTIFIER);

       String window = lexer.getLexeme();

       int[] dimensions = getNumberList(2);

       Scene scene = new Scene(window, dimensions[0], dimensions[1]);

       parseImages(scene, lexer.getNextToken());

       verifyNextToken(Token.PERIOD);

       return scene;

   }

 

   // Parses the following productions

 

   // images -> image images | image

   // image -> right_triangle | rectangle

   // right_triangle -> RIGHT_TRIANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ‘;’

   // rectangle -> RECTANGLE_ COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ‘;’

 

   public void parseImages(Scene scene, Token imageToken) throws LexicalError, SyntaxError, IOException {

       int height, width, offset, radius = 0;

       verifyNextToken(Token.COLOR);

       int[] colors = getNumberList(3);

       Color color = new Color(colors[0], colors[1], colors[2]);

       verifyNextToken(Token.AT);

       int[] location = getNumberList(2);

       Point point = new Point(location[0], location[1]);

       if (imageToken == Token.RIGHT_TRIANGLE) {

           verifyNextToken(Token.HEIGHT);

           verifyNextToken(Token.NUMBER);

           height = lexer.getNumber();

           verifyNextToken(Token.WIDTH);

           verifyNextToken(Token.NUMBER);

           width = lexer.getNumber();

           RightTriangle triangle = new RightTriangle(color, point, height, width);

           scene.addImage(triangle);

       } else if (imageToken == Token.RECTANGLE) {

           verifyNextToken(Token.HEIGHT);

           verifyNextToken(Token.NUMBER);

           height = lexer.getNumber();

           verifyNextToken(Token.WIDTH);

           verifyNextToken(Token.NUMBER);

           width = lexer.getNumber();

           Rectangle rectangle = new Rectangle(color, point, height, width);

           scene.addImage(rectangle);

 

           /*This is the part being added to the program that Parses the following productions:

           *parallelogram → PARALLELOGRAM COLOR number_list AT number_list number_list OFFSET NUMBER ‘;’

           * regular_polygon → REGULAR_POLYGON COLOR number_list AT number_list SIDES NUMBER RADIUS NUMBER ‘;’

           * isosceles → ISOSCELES COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ‘;’

           * text → TEXT COLOR number_list AT number_list STRING ‘;’ */

 

       } else if (imageToken == Token.PARALLELOGRAM) {

           verifyNextToken(Token.COLOR);

           verifyNextToken(Token.AT);

           verifyNextToken(Token.OFFSET);

           verifyNextToken(Token.NUMBER);

           offset = lexer.getNumber();

           Parallelogram parallelogram = new Parallelogram(color, point, point, offset);

           scene.addImage(parallelogram);

       } else if (imageToken == Token.REGULAR_POLYGON) {

           verifyNextToken(Token.COLOR);

           verifyNextToken(Token.AT);

           verifyNextToken(Token.SIDES);

           verifyNextToken(Token.NUMBER);

           int numSides = lexer.getNumber();

           verifyNextToken(Token.RADIUS);

           verifyNextToken(Token.NUMBER);

           RegularPolygon polygon = new RegularPolygon(color, point, numSides, radius);

           scene.addImage(polygon);

       } else if (imageToken == Token.ISOSCELES) {

           verifyNextToken(Token.COLOR);

           verifyNextToken(Token.AT);

           verifyNextToken(Token.HEIGHT);

           verifyNextToken(Token.NUMBER);

           height = lexer.getNumber();

           verifyNextToken(Token.WIDTH);

           verifyNextToken(Token.NUMBER);

           width = lexer.getNumber();

           IsoscelesTriangle triangle = new IsoscelesTriangle(color, point, height, width);

           scene.addImage(triangle);

       } else if (imageToken == Token.TEXT) {

           verifyNextToken(Token.COLOR);

           verifyNextToken(Token.AT);

           verifyNextToken(Token.STRING_LITERAL);

           String text = lexer.getLexeme();

           Text textImage = new Text(color, point, text);

           scene.addImage(textImage);

       } else {

           throw new SyntaxError(lexer.getLineNo(), “Unexpected image name ” + imageToken);

       }

       verifyNextToken(Token.SEMICOLON);

       token = lexer.getNextToken();

       if (token != Token.END)

           parseImages(scene, token);

   }

 

   // Parses the following productions

 

   // number_list -> ‘(‘ numbers ‘)’

   // numbers -> NUMBER | NUMBER ‘,’ numbers

 

   // Returns an array of the numbers in the number list

 

   public int[]  getNumberList(int count) throws LexicalError, SyntaxError, IOException {

       int[] list = new int[count];

       verifyNextToken(Token.LEFT_PAREN);

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

           verifyNextToken(Token.NUMBER);

           list[i] = lexer.getNumber();

           token = lexer.getNextToken();

           if (i < count – 1)

               verifyCurrentToken(Token.COMMA);

           else

               verifyCurrentToken(Token.RIGHT_PAREN);

       }

       return list;

   }

 

   // Returns a list of numbers

 

   public int[] getNumberList() throws LexicalError, SyntaxError, IOException {

       ArrayList<Integer> list = new ArrayList<Integer>();

       verifyNextToken(Token.LEFT_PAREN);

       do {

           verifyNextToken(Token.NUMBER);

           list.add((int) lexer.getNumber());

           token = lexer.getNextToken();

       }

       while (token == Token.COMMA);

       verifyCurrentToken(Token.RIGHT_PAREN);

       int[] values = new int[list.size()];

       for (int i = 0; i < values.length; i++)

           values[i] = list.get(i);

       return values;

   }

 

   // Verifies that the next token is the expected token

 

   public void verifyNextToken(Token expectedToken) throws LexicalError, SyntaxError, IOException {

       token = lexer.getNextToken();

       verifyCurrentToken(expectedToken);

   }

 

   // Verifies that the current token is the expected token

 

   public void verifyCurrentToken(Token expectedToken) throws SyntaxError {

       if (token != expectedToken)

           throw new SyntaxError(lexer.getLineNo(), “Expecting token ” + expectedToken + ” not ” + token);

   }

}

Polygon.java

import java.awt.*;

 

// Base class for all polygon classes

 

class Polygon_ extends Image {

 

   public int vertexCount;

   public Polygon polygon;

 

   // Constructor that initializes color and vertexCount of a polygon

 

   public Polygon_(Color color, int vertexCount) {

       super(color);

       this.vertexCount = vertexCount;

   }

   // Creates a polygon object given its vertices

 

   public void createPolygon(int[] x_points, int[] y_points) {

       polygon = new Polygon(x_points, y_points, vertexCount);

   }

   // Draws polygon using polygon object

 

   @Override

   public void draw(Graphics graphics) {

       colorDrawing(graphics);

       graphics.drawPolygon(polygon);

   }

}

Rectangle.java

import java.awt.*;

 

// Class that defines a hollow rectangle object

 

class Rectangle extends HollowPolygon {

   // Constructor that initializes the vertices of the rectangle

 

   public Rectangle(Color color, Point upperLeft, int height, int width) {

       super(color, 4);

       int[] x_points = {upperLeft.x, upperLeft.x + width, upperLeft.x + width, upperLeft.x};

       int[] y_points = {upperLeft.y, upperLeft.y, upperLeft.y + height, upperLeft.y + height};

       createPolygon(x_points, y_points);

   }

}

RegularPolygon.java

import java.awt.*;

 

// Class that defines a regular polygon object

class RegularPolygon extends SolidPolygon {

 

   // Constructor that initializes the vertices of the polygon

   public RegularPolygon(Color color, Point center, int numSides, int radius) {

       super(color, numSides);

       int[] x_points = new int[numSides];

       int[] y_points = new int[numSides];

       double angle = 2 * Math.PI / numSides;

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

           x_points[i] = (int) (center.x + radius * Math.cos(angle * i));

           y_points[i] = (int) (center.y + radius * Math.sin(angle * i));

       }

       createPolygon(x_points, y_points);

   }

}

RightTriangle.java

import java.awt.*;

 

// Class that defines a hollow right triangle

 

class RightTriangle extends HollowPolygon {

 

   // Constructor that initializes the vertices of the right triangle

 

   public RightTriangle(Color color, Point upperLeft, int height, int width) {

       super(color, 3);

       int[] x_points = {upperLeft.x, upperLeft.x, upperLeft.x + width};

       int[] y_points = {upperLeft.y, upperLeft.y + height, upperLeft.y + height};

       createPolygon(x_points, y_points);

   }

}

 

Scene.java

import javax.swing.*;

 

// Class that defines a scene

 

class Scene {

 

   public JFrame window;

   public DrawingPanel drawing;

 

   // Constructor that must be supplied the height and width of the drawing window for the scene

 

   public Scene(String name, int height, int width) {

       window = new JFrame(name);

       window.setSize(width, height);

       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       drawing = new DrawingPanel();

       window.add(drawing);

   }

 

   // Adds a graphic object to the scene’s drawing panel

 

   public void addImage(Image image) {

       drawing.addImage(image);

   }

 

   // Causes the drawing panel to be repainted

 

   public void draw() {

       window.setVisible(true);

       drawing.repaint();

   }

}

scene.txt

Scene Polygons (500, 500)

 RightTriangle Color (255, 0, 0) at (50, 30) Height 100 Width 300;

 Rectangle Color (0, 128, 255) at (100, 100) Height 200 Width 100;

 Isosceles Color (255, 0, 0) at (120, 120) Height 100 Width 200;

 Parallelogram Color (0, 0, 255) at (340, 50) (440, 120) Offset 30;

 RegularPolygon Color(255, 0, 255) at (300, 300) Sides 6 Radius 80;

 Text Color(0, 0, 0) at (400, 200) “Hello World”;

End.

 

SolidPolygon.java

import java.awt.*;

 

// SolidPolygon class that extends the Polygon_ class

 

class SolidPolygon extends Polygon_ {

 

   // Constructor that calls super constructor

 

   public SolidPolygon(Color color, int vertexCount) {

       super(color, vertexCount);

   }

 

   // Method to draw a solid polygon

 

   @Override

   public void draw(Graphics graphics) {

       graphics.setColor(color);

       colorDrawing(graphics);

       graphics.fillPolygon(polygon);

   }

}

SyntaxError.java

// Class that defines a syntax error

 

class SyntaxError extends Exception

{

   // Constructor that creates a syntax error object given the line number and error

 

   public SyntaxError(int line, String description) {

       super(“Syntax Error on Line: ” + line + ” ” + description);

   }

}

 

Text.java

// Class for displaying text

import java.awt.*;

 

class Text extends Image {

   public Point location;

   public String text;

 

   // Constructor to initialize the color, location, and text to be displayed

   public Text(Color color, Point location, String text) {

       super(color);

       this.location = location;

       this.text = text;

   }

 

   // Overrides the draw method to display text on the screen

   @Override

   public void draw(Graphics graphics) {

       colorDrawing(graphics);

       graphics.drawString(text, location.x, location.y);

   }

}

 

Token.java

// Enumerated type that defines the list of tokens

 

enum Token {AT, COLOR, END,    HEIGHT, ISOSCELES, RECTANGLE, RIGHT_TRIANGLE, SCENE, WIDTH, COMMA, SEMICOLON, PERIOD, LEFT_PAREN,

   RIGHT_PAREN, IDENTIFIER, NUMBER, IMAGE, PARALLELOGRAM, OFFSET, REGULAR_POLYGON, SIDES, RADIUS, TEXT, STRING_LITERAL, EOF}