1 / 9

Picture It Game with GUI Control Class

Picture It Game with GUI Control Class. Pepper. Class Diagram. Game (controller & maybe model) holds the state of the game has a static main method has 2 players and one MyCanvas GameGUI (view) Manages the Gui Given the two player locations, it draws the board Player (model)

khilliard
Download Presentation

Picture It Game with GUI Control Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Picture It Game with GUI Control Class Pepper

  2. Class Diagram • Game (controller & maybe model) • holds the state of the game • has a static main method • has 2 players and one MyCanvas • GameGUI (view) • Manages the Gui • Given the two player locations, it draws the board • Player (model) • Holds everything one player needs to know about his status in the game

  3. JavaLib has a Canvas • One window: Create one canvas, and keep drawing on it. • Add the knowledge to your program; import javalib.worldcanvas.*; • Create a WorldCanvas object WorldCanvasgameSpace = new WorldCanvas(500,500,"game place") ; • Show the canvas once in the beginning gameSpace.show(); • Redraw the image from the model gameSpace.drawImage(MyWorld.drawGame(loc));

  4. GameGui • State: • WorldCanvas • Any pictures or shapes your game needs • Methods • Constructor • Show the canvas once • Create the first picture of the board • Redraw • Take in all the info it needs to know what to draw • Recreate the board picture from scratch • Redraw using the new board picture

  5. GameGUI Class - Constructor import javalib.worldcanvas.*; // canvas import javalib.worldimages.*; import java.awt.Color; public class GameGUI { // instance variables for all the shapes needed - WorldCanvas critical WorldCanvas gameSpace = new WorldCanvas(500,500,"game place") ; WorldImage board = AImage.makeRectangle(500,500, Color.yellow, Mode.solid); WorldImage onePiece = AImage.makeCircle (10, Color.blue, Mode.solid); WorldImage twoPiece = AImage.makeCircle (10, Color.red, Mode.solid); public GameGUI(){ gameSpace.show(); drawGame (0,0) // Draw the empty canvas and then draw the game picture in start position }

  6. GAMEGui Draw Method • Draw the board and then place the players • Take in player placement info, or take in full players public void drawGame (int one, int two) { WorldImagehorizLine = AImage.makeLine(new Posn(0,0),new Posn(500,0)); WorldImageverticalLine = AImage.makeLine(new Posn(0,0),new Posn(0,500)); for (int count = 50; count <= 500; count= count + 50){ board = board.place(horizLine,250,count); board = board.place(verticalLine,count,250); } board = board.place(onePiece, (one%10)*50-25, one/10*50+25) ; board = board.place(twoPiece, (two%10)*50-25, two/10*50+25) ; gameSpace.drawImage(board); }

  7. Game Has a Game GUI • Main static method • Creates a GameGui (view) • GameGui's constructor will open the canvas • Calls the redraw method every time a move is made • Pass in whatever the GameGui needs in order to draw • For this game, only the 2 player positions are needed

  8. Game Control - Main • Create a GameGUI once GameGUIgameBoard = new GameGUI(); • Call the drawGame method after every turn gameBoard.drawGame(p[0].place,p[1].place);

  9. Summary • Divide Interface responsibilities among classes • The GUI has an instance variable of the canvas - which means one window • Creating an instance of the Gui lets the canvas persist • a static method could not remember its canvas (window) so the game would have had to control the canvas

More Related