10 4 15
play

10/4/15 Graphical Programming (1) Maze Program TOPICS Graphical - PDF document

10/4/15 Graphical Programming (1) Maze Program TOPICS Graphical Programming Using Classes (Objects) Multiple Files (Eclipse) Maze Logistics CS 160, Fall Semester 2015 2 Graphical Programming (2) Graphical Programming (3)


  1. 10/4/15 Graphical Programming (1) Maze Program TOPICS • Graphical Programming • Using Classes (Objects) • Multiple Files (Eclipse) • Maze Logistics CS 160, Fall Semester 2015 2 Graphical Programming (2) Graphical Programming (3) § No, we’re not going to show the code § But, why not have you write code that for the Puzzle program! (yet) controls a graphical program? § 150 lines of Java Swing code § Maze program: § Move student around a maze § What kinds of things does it do? § ~250 lines of graphical programming § Set window size, title, and location § You write the main method § Setup a frame and panel, add buttons § You instantiate the Maze § Read a photo and extract parts of it § You control the character § Listen for mouse and keyboard events CS 160, Fall Semester 2015 3 CS 160, Fall Semester 2015 4 1

  2. 10/4/15 Maze Program: Objectives Maze Program: Initial Code n Use objects (classes) developed by // Create maze someone else String fileName = args[0]; Maze maze = new Maze(fileName); n Create an instance of a class System. out.println("Maze name: " + fileName); n Call methods on the object n For example: graphical programming! // Get dimensions mazeWidth = maze.getWidth(); n Multiple source files mazeHeight = maze.getHeight(); n Resource and data files System. out.println("Maze width: " + mazeWidth); System. out.println("Maze height: " + mazeHeight); CS 160, Fall Semester 2015 5 CS 160, Fall Semester 2015 6 Maze Program: Interface Maze Program: Output // Moving character Maze name: maze1.txt maze.moveRight(); // move right, no return value Maze width: 6 maze.moveLeft(); // move left, no return value Maze height: 6 maze.moveUp(); // move up, no return value Moved to row 0, column 1 maze.moveDown(); // move down, no return value Moved to row 1, column 1 … // Getting position Chihiro found Haku, congratulations! maze.getColumn(); // get column of character Error Messages maze.getRow(); // get row of character Chihiro went outside the maze! Chihiro ran into the witch Yubaba! CS 160, Fall Semester 2015 7 CS 160, Fall Semester 2015 8 2

  3. 10/4/15 Maze Program: User Interface Maze Program: Algorithm § Rules for moving Chihiro around maze: § If not on edge, move straight to top edge § If on top edge, move to top-right § If on right edge, move to bottom-right § If on bottom edge, move to bottom-left § If on left edge, move to top-left Guaranteed to find Haku, will never meet Yubaba! CS 160, Fall Semester 2015 9 CS 160, Fall Semester 2015 10 Maze Program: Methods Maze Program: Setup § ~/workspace/P6 // Returns true if on any edge, false otherwise § Chihiro.png § public static boolean onEdge(); § Haku.png // Loops from current position to edge § Yubaba.png § public static void loopLeft(); § Success.png § public static void loopRight(); § Maze*.txt § public static void loopUp(); § ~/workspace/P6/src § public static void loopDown(); § Maze.java Methods not required, but very useful! § P6.java (write this!) CS 160, Fall Semester 2015 11 CS 160, Fall Semester 2015 12 3

Recommend


More recommend