Download presentation
Presentation is loading. Please wait.
Published byLouise Rich Modified over 5 years ago
1
Barb Ericson Georgia Institute of Technology May 2006
Speed part 2 Barb Ericson Georgia Institute of Technology May 2006 Georgia Institute of Technology
2
Georgia Institute of Technology
Learning Goals Gain some understanding of interpreters Write a simple interpreter that understands a simple graphical language Interpret the graphical language and produce the expected result Add more functionality to that interpreter Connect this to what is happening in Flash, AutoCAD, etc, Think about what is faster an interpreter or just the commands created by the interpreter Georgia Institute of Technology
3
Creating an Interpreter
Create a class GraphicsInterpreter that has a method interpretCommands that takes the name of a file Read graphics commands from the file and execute the commands on a blank 640 by 480 picture line Draw a line from (10,20) to (300,400) Circle Draw a circle with the upper left corner of the enclosing rectangle at (100,200) and a diameter of 10 Georgia Institute of Technology
4
interpretCommands Method
public Picture interpretCommands(String fileName) { String line = null; Picture frame = new Picture(640,480); String [] params = null; int x1, y1, x2, y2, diameter; Graphics g = frame.getGraphics(); g.setColor(Color.black); // try the following try { // read from the file BufferedReader reader = new BufferedReader(new FileReader(fileName)); Georgia Institute of Technology
5
interpretCommands Method – Cont1
// loop till end of file while ((line = reader.readLine()) != null) { // what command is this? if (line.startsWith("line")) // Get the parameters for drawing the line params = line.split(" "); // params[0] should be "line" x1 = Integer.parseInt(params[1]); y1 = Integer.parseInt(params[2]); x2 = Integer.parseInt(params[3]); y2 = Integer.parseInt(params[4]); // Now, draw the line in g.drawLine(x1,y1,x2,y2); } Georgia Institute of Technology
6
interpretCommands Method – Cont2
else if (line.startsWith("circle")) { // Get the parameters for drawing the circle params = line.split(" "); // params[0] should be "circle" x1 = Integer.parseInt(params[1]); y1 = Integer.parseInt(params[2]); diameter = Integer.parseInt(params[3]); // Now, draw the circle in g.drawOval(x1,y1,diameter,diameter); } else System.out.println("Uh-oh! Invalid command! "+line); return frame; Georgia Institute of Technology
7
interpretCommands Method – Cont3
} // end while } catch (FileNotFoundException ex) { System.out.println("Couldn't find file " + fileName); fileName = FileChooser.pickAFile(); interpretCommands(fileName); } catch (Exception ex) { System.out.println("Error during read or write"); ex.printStackTrace(); } return frame; Georgia Institute of Technology
8
Georgia Institute of Technology
Main for Testing public static void main(String[] args) { GraphicsInterpreter interpreter = new GraphicsInterpreter(); String fileName = FileChooser.getMediaPath("graphics-commands.txt"); Picture p = interpreter.interpretCommands(fileName); p.show(); } Georgia Institute of Technology
9
Georgia Institute of Technology
Resulting Picture Commands: circle circle line line line Georgia Institute of Technology
10
Georgia Institute of Technology
How This Works Create a blank 640 by 480 picture and get the graphics context for drawing Open the file to read from and loop reading a line at a time Check if the current line starts with "line" or "circle" If it starts with line get the next 4 numbers as the first point and second point x and y values and draw the line If it starts with circle get the next 3 numbers as the upper left corner and diameter and draw the circle Return the resulting picture object Georgia Institute of Technology
11
The Graphics Interpreter
We have just created a simple graphical language That is used to represent lines and circles And an interpreter that understands that language This is what Flash, AutoCAD, Postscript, and PDF do, too The file formats specify the language And they interpret the language and draw the image Georgia Institute of Technology
12
Which will execute faster?
Our GraphicsInterpreter Drawing the last picture using the interpretCommands method on the sample input Or, the GeneratedDrawing class main at right? import java.awt.*; public class GeneratedDrawing{ public static void main(String args[]){ Picture frame = new Picture(640,480); Graphics g = frame.getGraphics(); g.setColor(Color.black); g.drawOval(20,20,100,100); g.drawOval(300,20,100,100); g.drawLine(210,120,210,320); g.drawLine(210,320,310,320); g.drawLine(20,350,400,350); frame.show(); } // end main() } // end class Georgia Institute of Technology
13
Interpreters are Slower
The GeneratedDrawing class main will execute faster It doesn't need to interpret the commands Just execute them Think of talking to a native versus a non-native speaker through an interpreter A conversation will take longer with an interpreter in the middle Georgia Institute of Technology
14
Georgia Institute of Technology
Exercise Add the ability to draw triangles to the GraphicsInterpreter class triangle x1 y1 x2 y2 x3 y3 It will draw a triangle with points at (x1,y1), (x2,y2), and (x3, y3) Georgia Institute of Technology
15
Georgia Institute of Technology
Summary Interpreters read a language and execute the commands in that language You can write an interpreter for a simple graphics language Interpreters are slower than just executing the code that is being created You have to read the language and figure out what to do Georgia Institute of Technology
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.