Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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. The IsoscelesTriangle class must have a constructor that is supplied the color of the triangle, a point that specifies the location of the top vertex, and the height and width of the triangle. It must allocate the arrays of x and y coordinates that defines the triangle and it must compute their values. The Parallelogram class must have a constructor that is supplied the color of the parallelogram, two points that specifies the location of the upper left and lower right vertices in addition to an x offset value that specifies the x distance between the upper and lower left vertices. It must allocate the arrays of x and y coordinates that defines the parallelogram and it must compute their values. The RegularPolygon class must contain a constructor that is supplied the color of the polygon, the number of sides, a point that specifies the location of its center, and the radius, which defines the distance between the center and each of the vertices. It must allocate the arrays of x and y coordinates that defines the regular polygon and it must compute their values. Below is a sample of a scene definition file that would provide input to the program: 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. Skeleton Code: DrawingPanel Class: import java.awt.*; import java.util.*; import javax.swing.*; // Class that defines the panel for drawing the images class DrawingPanel extends JPanel { private ArrayList 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 protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); for (Image image : images) image.draw(graphics); } } HollowPolygon Class: 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 Class: import java.awt.*; // Abstract base class that defines all image objects abstract class Image { private 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); } Lexer Class: import java.io.*; // This class provides the lexical analyzer for project 1 class Lexer { private static final int KEYWORDS = 8; private StreamTokenizer tokenizer; private String punctuation = ",;.()"; private 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 { int token = tokenizer.nextToken(); switch (token) { case StreamTokenizer.TT_NUMBER: return Token.NUMBER; case StreamTokenizer.TT_WORD: for (Token aToken : Token.values()) { if (aToken.ordinal() == KEYWORDS) break; if (aToken.name().replace("_","").equals(tokenizer.sval.toUpperCase())) return aToken; } return Token.IDENTIFIER; case StreamTokenizer.TT_EOF: return Token.EOF; default: for (int i = 0; i < punctuation.length(); i++) if (token == punctuation.charAt(i)) return punctuationTokens[i]; } return Token.EOF; } // 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 Class: // 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 Class: 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"); } } } Parser Class: import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; // This class provides the skeleton parser for project 1 class Parser { private JFrame window; private Token token; private 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 ';' private void parseImages(Scene scene, Token imageToken) throws LexicalError, SyntaxError, IOException { int height, width, offset, radius; 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); } 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 private 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 private int[] getNumberList() throws LexicalError, SyntaxError, IOException { ArrayList list = new ArrayList(); 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 private void verifyNextToken(Token expectedToken) throws LexicalError, SyntaxError, IOException { token = lexer.getNextToken(); verifyCurrentToken(expectedToken); } // Verifies that the current token is the expected token private void verifyCurrentToken(Token expectedToken) throws SyntaxError { if (token != expectedToken) throw new SyntaxError(lexer.getLineNo(), "Expecting token " + expectedToken + " not " + token); } } Polygon Class: import java.awt.*; // Base class for all polygon classes class Polygon_ extends Image { private int vertexCount; private 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 Class: 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); } } RightTriangle Class: 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 Class: import javax.swing.*; // Class that defines a scene class Scene { private JFrame window; private 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(); } } Syntax Error Class: // 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); } } Token Class: // Enumerated type that defines the list of tokens enum Token {AT, COLOR, END, HEIGHT, RECTANGLE, RIGHT_TRIANGLE, SCENE, WIDTH, COMMA, SEMICOLON, PERIOD, LEFT_PAREN, RIGHT_PAREN, IDENTIFIER, NUMBER, EOF}

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions

Question

What elements of multimedia-based instruction facilitate learning?

Answered: 1 week ago

Question

Explain the importance of HRM to all employees.

Answered: 1 week ago

Question

Discuss the relationship between a manager and an HR professional.

Answered: 1 week ago

Question

Outline demographic considerations.

Answered: 1 week ago