90 likes | 108 Views
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)
E N D
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
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));
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
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 }
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); }
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
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);
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