GUIs for Video GamesThe PlanLayout SuggestionsSlide 4Observer/Observable PatternWhy use Observer/Observable?More MotivationBenefitsSteps to writing Observer/ObservableExample: Score BoardScore.javaSlide 12ScorePanel.javaSlide 14Galaga.javaSlide 16PracticeCompSci 419.1GUIs for Video GamesGUIs for Video GamesCompSci 419.2GUIs for Video GamesThe PlanLayout suggestionsObserver/ObservableSteps to writing Observer/ObservablesSample CodePracticeCompSci 419.3GUIs for Video GamesLayout SuggestionsGame HereTitle HereControls HereControls HereScore HereCompSci 419.4GUIs for Video GamesLayout SuggestionsGame HereTitle HereControls HereControls HereScore HereUse a BorderLayout for the FrameMake the Title a JLabelMake the Controls and Score JPanelsin different classesCompSci 419.5GUIs for Video GamesObserver/Observable PatternObservableData that changesUsed for the modelAlerts Observers of changesObserverDepends on changing dataUsed for the viewResponds to changes in the ObservableCompSci 419.6GUIs for Video GamesWhy use Observer/Observable?Separates model from viewModel is typically where the complexity isChanges in complex models is undesirableAvoids pollingAn alternative is to repeated check if data has changedPolling wastes processor timeEnables flexibility of multiple viewsCan even have multiple views open at onceCompSci 419.7GUIs for Video GamesMore MotivationConsider tic-tac-toe:Model consisting of 9 variables telling board positionFull feature view for fast computerso3D AnimationoSoundoImagesScaled down version for slower computersoJButtonsoImageIconsControl class to change letters of modelCompSci 419.8GUIs for Video GamesBenefitsSame model, different viewController would enable networked game input.Model easy to send across network, view is difficult. Separation enables simplicity and flexibility.CompSci 419.9GUIs for Video GamesSteps to writing Observer/Observable1.Write separate classes for the model and the view.2.Make the model extend Observable.3.Make the view implement Observer.4.Connect the Observer and Observable using the addObserver method.CompSci 419.10GUIs for Video GamesExample: Score BoardModelEnemies leftHero's life leftTimerViewJPanel with JLabelsGridLayoutCompSci 419.11GUIs for Video GamesScore.javaimport java.util.*;public class Score extends Observable{double shipScore, enemyScore, time;public void setShipScore(double s){shipScore=s;setChanged();notifyObservers();}public void setEnemyScore(double s){enemyScore=s;setChanged();notifyObservers();}CompSci 419.12GUIs for Video GamesScore.javapublic double getShipScore(){return shipScore;}public double getEnemyScore(){return enemyScore;}public void setTime(double t){if((int)time!=(int)t){time=t;setChanged();notifyObservers();}time=t;}public double getTime(){return time;}}CompSci 419.13GUIs for Video GamesScorePanel.javaimport java.awt.*;import javax.swing.*;import java.util.*;public class ScorePanel extends JPanel implements Observer{JLabel title;JLabel shipScore;JLabel enemyScore;JLabel time;public ScorePanel(){super();makeComponents();makeLayout();}CompSci 419.14GUIs for Video GamesScorePanel.java private void makeComponents() { title=new JLabel("Score"); shipScore=new JLabel("Ship: "); enemyScore=new JLabel("Enemy: "); time=new JLabel("Time: 0"); } private void makeLayout() { setLayout(new GridLayout(4, 1)); add(title); add(shipScore); add(enemyScore); add(time); } public void update(Observable obs, Object arg) { Score score=(Score)obs; shipScore.setText("Ship: "+score.getShipScore()); enemyScore.setText("Enemy: "+score.getEnemyScore()); time.setText("Time: "+(int)score.getTime()); System.out.println("repainting"); repaint(); }}CompSci 419.15GUIs for Video GamesGalaga.javaimport java.awt.BorderLayout;import javax.swing.*;import java.awt.*;import java.awt.event.*;import tipgame.*;public class Galaga extends JApplet implements ActionListener{JFrame frame;JButton button;GalagaLoop game; JLabel title;ScorePanel scorePanel; public Galaga(){makeComponents();layoutComponents();}CompSci 419.16GUIs for Video GamesGalaga.javaprivate void makeComponents() {frame=new JFrame();button=new JButton("Start");button.addActionListener(this);game=new GalagaLoop(new Dimension(500, 500));title=new JLabel("Blast Those Professors!");Font font=title.getFont();title.setFont(font.deriveFont(32.0f));scorePanel=new ScorePanel();game.getScore().addObserver(scorePanel); } private void layoutComponents() {Container container=frame.getContentPane();container.setLayout(new BorderLayout());container.add(game.getCanvas(), BorderLayout.CENTER);container.add(button, BorderLayout.SOUTH);container.add(title, BorderLayout.NORTH);container.add(scorePanel, BorderLayout.EAST);frame.pack();frame.setResizable(false);}CompSci 419.17GUIs for Video GamesPracticeWrite a program to count the number of clicks on a yes, no and maybe button. To do this, write three classes:ClickCount – keeps three integer instance variables to count the number of clicksClickCountPanel – observes ClickCount for changes and updates its components when an update occursClickGUI – contains three buttons and the count panel. Clicking on the buttons registers the click via
View Full Document