Barb Ericson Georgia Institute of Technology May 2006

Slides:



Advertisements
Similar presentations
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
Advertisements

Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Recursion1 Barb Ericson Georgia Institute of Technology March 2010.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Georgia Institute of Technology Drawing in Java part 1 Barb Ericson Georgia Institute of Technology September 2006.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
Georgia Institute of Technology Creating and Modifying Text part 4 Barb Ericson Georgia Institute of Technology Oct 2005.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology Movies part 2 Barb Ericson Georgia Institute of Technology April 2006.
CPSC1301 Computer Science 1 Chapter 14 Creating and Modifying Movies part 2.
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
CPSC1301 Computer Science 1 Chapter 12 Creating and Modifying Text part 4.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Georgia Institute of Technology Movies Barb Ericson Georgia Institute of Technology April 2006.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
Georgia Institute of Technology Movies part 4 Barb Ericson Georgia Institute of Technology April 2006.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Georgia Institute of Technology Making Text for the Web part 2 Barb Ericson Georgia Institute of Technology March 2006.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Drawing in Java – part 1 Barb Ericson Georgia Institute of Technology September 2005.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Eleventh Graphics in Java.
OO Design and Programming II I/O: Reading and Writing
Manipulating Pictures, Arrays, and Loops part 2
Building Java Programs
Barb Ericson Georgia Institute of Technology September 2005
Building Java Programs
Building Java Programs
Barb Ericson Georgia Institute of Technology May 2006
Creating and Modifying Text part 2
Manipulating Pictures, Arrays, and Loops part 2
Barb Ericson Georgia Institute of Technology August 2005
Barb Ericson Georgia Institute of Technology August 2005
An Introduction to Java – Part I, language basics
Arrays versus ArrayList
Barb Ericson Georgia Institute of Technology April 2006
Workshop for Programming And Systems Management Teachers
Introduction to Processing Digital Sounds part 3
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Workshop for Programming And Systems Management Teachers
Sadalage & Fowler (Amazon)
Building Java Programs
Barb Ericson Georgia Institute of Technology May 2006
Making Text for the Web part 6
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
Creating and Modifying Text part 3
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs
Barb Ericson Georgia Institute of Technology Oct 2005
More on Creating Classes part 3
Building Java Programs
Graphics Reading: Supplement 3G
Presentation transcript:

Barb Ericson Georgia Institute of Technology May 2006 Speed part 2 Barb Ericson Georgia Institute of Technology May 2006 Georgia Institute of Technology

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

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

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

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 non-native speaker through an interpreter A conversation will take longer with an interpreter in the middle Georgia Institute of Technology

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

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