Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CLASS Main import java.io.*; import java.util.*; import javax.swing.*; // Main class class Main { // The main method of the whole program, allows the name

image text in transcribedimage text in transcribed

CLASS Main import java.io.*; import java.util.*; import javax.swing.*; // 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"); } } }

CLASS Parser import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; // This class provides the skeleton parser 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 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

CLASS Polygon_ 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); } }

CLASS Rectangle import java.awt.*; // Class that defines a hollow rectangle object class Rectangle extends HollowPolygon { // Constructor that initializes the vertices of the rectanglepublic 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); } }

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) Rightrriangle 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. Shown below is the scene that should be produced when the program is provided with the above scene definition

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

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions