Speed part 2 Barb Ericson Georgia Institute of Technology

advertisement
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,
AutoCAD, 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 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 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
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
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
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
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
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
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, 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
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
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 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
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
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
Download