DOC PREVIEW
Duke CPS 004 - Video Games

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 PlanLayout suggestionsObserver/ObservableSteps to writing Observer/ObservablesSample CodePracticeCompSci 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 PatternObservableData that changesUsed for the modelAlerts Observers of changesObserverDepends on changing dataUsed for the viewResponds to changes in the ObservableCompSci 419.6GUIs for Video GamesWhy use Observer/Observable?Separates model from viewModel is typically where the complexity isChanges in complex models is undesirableAvoids pollingAn alternative is to repeated check if data has changedPolling wastes processor timeEnables flexibility of multiple viewsCan even have multiple views open at onceCompSci 419.7GUIs for Video GamesMore MotivationConsider tic-tac-toe:Model consisting of 9 variables telling board positionFull feature view for fast computerso3D AnimationoSoundoImagesScaled down version for slower computersoJButtonsoImageIconsControl class to change letters of modelCompSci 419.8GUIs for Video GamesBenefitsSame model, different viewController 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 BoardModelEnemies leftHero's life leftTimerViewJPanel with JLabelsGridLayoutCompSci 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

Duke CPS 004 - Video Games

Documents in this Course
Lecture

Lecture

18 pages

Chapter 7

Chapter 7

18 pages

Chapter 9

Chapter 9

15 pages

Java 1

Java 1

24 pages

Java 3

Java 3

11 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

28 pages

Chap 2

Chap 2

16 pages

Graphics

Graphics

20 pages

Lecture

Lecture

12 pages

HTML

HTML

16 pages

Java 1

Java 1

6 pages

Chapter 4

Chapter 4

16 pages

The Plan

The Plan

25 pages

Lecture

Lecture

16 pages

Chapter 6

Chapter 6

21 pages

Lecture

Lecture

18 pages

Lecture

Lecture

23 pages

Lecture

Lecture

16 pages

Lecture

Lecture

19 pages

Lecture

Lecture

12 pages

Lecture

Lecture

5 pages

Lecture

Lecture

26 pages

Lecture

Lecture

16 pages

Chapter 7

Chapter 7

23 pages

Lecture

Lecture

21 pages

Lecture

Lecture

4 pages

Lecture

Lecture

4 pages

Lecture

Lecture

8 pages

Lecture

Lecture

4 pages

Lecture

Lecture

10 pages

Chapter 4

Chapter 4

32 pages

Java

Java

4 pages

CompSci 4

CompSci 4

18 pages

Lecture

Lecture

26 pages

CompSci 4

CompSci 4

12 pages

HTML

HTML

17 pages

Lecture

Lecture

16 pages

Chapter 5

Chapter 5

22 pages

Lecture

Lecture

4 pages

Chapter 4

Chapter 4

10 pages

Chapter 2

Chapter 2

15 pages

Chapter 8

Chapter 8

14 pages

Lecture

Lecture

15 pages

Load more
Download Video Games
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Video Games and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Video Games 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?