Speed part 2 Barb Ericson Georgia Institute of
Speed part 2 Barb Ericson Georgia Institute of Technology May 2006 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, Auto. CAD, etc, – Think about what is faster an interpreter or just the commands created by the interpreter Georgia Institute of Technology
Creating an Interpreter • Create a class Graphics. Interpreter – that has a method interpret. Commands 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 10 20 300 400 • Draw a line from (10, 20) to (300, 400) – Circle 100 200 10 • Draw a circle with the upper left corner of the enclosing rectangle at (100, 200) and a diameter of 10 Georgia Institute of Technology
interpret. Commands Method public Picture interpret. Commands(String file. Name) { String line = null; Picture frame = new Picture(640, 480); String [] params = null; int x 1, y 1, x 2, y 2, diameter; Graphics g = frame. get. Graphics(); g. set. Color(Color. black); // try the following try { // read from the file Buffered. Reader reader = new Buffered. Reader(new File. Reader(file. Name)); Georgia Institute of Technology
interpret. Commands Method – Cont 1 // loop till end of file while ((line = reader. read. Line()) != null) { // what command is this? if (line. starts. With("line")) { // Get the parameters for drawing the line params = line. split(" "); // params[0] should be "line" x 1 = Integer. parse. Int(params[1]); y 1 = Integer. parse. Int(params[2]); x 2 = Integer. parse. Int(params[3]); y 2 = Integer. parse. Int(params[4]); // Now, draw the line in g. draw. Line(x 1, y 1, x 2, y 2); } Georgia Institute of Technology
interpret. Commands Method – Cont 2 else if (line. starts. With("circle")) { // Get the parameters for drawing the circle params = line. split(" "); // params[0] should be "circle" x 1 = Integer. parse. Int(params[1]); y 1 = Integer. parse. Int(params[2]); diameter = Integer. parse. Int(params[3]); // Now, draw the circle in g. draw. Oval(x 1, y 1, diameter); } else { System. out. println("Uh-oh! Invalid command! "+line); return frame; } Georgia Institute of Technology
interpret. Commands Method – Cont 3 } // end while } catch (File. Not. Found. Exception ex) { System. out. println("Couldn't find file " + file. Name); file. Name = File. Chooser. pick. AFile(); interpret. Commands(file. Name); } catch (Exception ex) { System. out. println("Error during read or write"); ex. print. Stack. Trace(); } return frame; } Georgia Institute of Technology
Main for Testing public static void main(String[] args) { Graphics. Interpreter interpreter = new Graphics. Interpreter(); String file. Name = File. Chooser. get. Media. Path("graphics-commands. txt"); Picture p = interpreter. interpret. Commands(file. Name); p. show(); } Georgia Institute of Technology
Resulting Picture • Commands: circle 20 20 100 circle 300 20 100 line 210 120 210 320 line 210 320 310 320 line 20 350 400 350 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
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, Auto. CAD, Postscript, and PDF do, too – The file formats specify the language • And they interpret the language and draw the image Georgia Institute of Technology
Which will execute faster? • Our Graphics. Interpreter – Drawing the last picture using the interpret. Commands method on the sample input • Or, the Generated. Drawing class main at right? import java. awt. *; public class Generated. Drawing{ public static void main(String args[]){ Picture frame = new Picture(640, 480); Graphics g = frame. get. Graphics(); g. set. Color(Color. black); g. draw. Oval(20, 100, 100); g. draw. Oval(300, 20, 100); g. draw. Line(210, 120, 210, 320); g. draw. Line(210, 320, 310, 320); g. draw. Line(20, 350, 400, 350); frame. show(); } // end main() } // end class Georgia Institute of Technology
Interpreters are Slower • The Generated. Drawing class main will execute faster – It doesn't need to interpret the commands • Just execute them • Think of talking to a native versus a nonnative speaker through an interpreter – A conversation will take longer with an interpreter in the middle Georgia Institute of Technology
Exercise • Add the ability to draw triangles to the Graphics. Interpreter class – triangle x 1 y 1 x 2 y 2 x 3 y 3 • It will draw a triangle with points at (x 1, y 1), (x 2, y 2), and (x 3, y 3) 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
- Slides: 15