ece 462 object-oriented progggramming using …ece 462 object-oriented progggramming using c++ and...
TRANSCRIPT
ECE 462Object-Oriented Programmingj g g
using C++ and Java
Flickering and Double Buffering
Yung-Hsiang Lul @ d [email protected]
YHL Double Buffering 1
FlickeringFlickering
YHL Double Buffering 2
No FlickeringNo Flickering
YHL Double Buffering 3
FlickeringFlickering
• Goal: continuously iterate through the three imagesy g g• Approach: draw background first then draw an image• Problem: occasionally, only the background is shown on
the screen without any of the three images• Result: the character sometimes disappears
YHL Double Buffering 4
Interleaving of Drawing and DisplayInterleaving of Drawing and Display
public void draw(Graphics g) {// draw backgroundg.drawImage(bgImage, 0, 0, null);
//sent to screen
// draw imageg.drawImage(anim.getImage(), 0, 0, null);
}}
YHL Double Buffering 5
Possible SolutionsPossible Solutions
• erase and draw difference
⇒ find the differences between each pair of images is not trivial Interleaving is still a problemis not trivial. Interleaving is still a problem.
• make draw atomic (i.e. cannot interleave)⇒ When the screen refreshes and drawing is
YHL Double Buffering 6
gincomplete, the screen will appear black.
Double Buffering + Page Flipping
• double buffering
Double Buffering Page Flipping
g– buffer 1: working buffer for drawing, incomplete,
invisible to user– buffer 2: completed, shown on screen– switching between the two buffers for frame updates
page flipping• page flipping– the pixels are not copied– instead a pointer switching betweeninstead, a pointer switching between
the two buffers– flipping between monitor refresh
buffer 1screen
YHL Double Buffering 7
pp gbuffer 2
screen
YHL Double Buffering 8
YHL Double Buffering 9
window.createBufferStrategy(2);gy( );BufferStrategy strategy = window.getBufferStrategy();Graphics g = strategy.getDrawGraphics();draw(g);g.dispose();
()strategy.show();
YHL Double Buffering 10
Screen Manager with BufferStrategy
YHL Double Buffering 11
Automatic Document Generation using Javadoc
YHL Double Buffering 12
YHL Double Buffering 13
YHL Double Buffering 14
YHL Double Buffering 15
YHL Double Buffering 16
YHL Double Buffering 17
YHL Double Buffering 18
YHL Double Buffering 19
YHL Double Buffering 20
YHL Double Buffering 21
YHL Double Buffering 22
YHL Double Buffering 23
YHL Double Buffering 24
YHL Double Buffering 25
YHL Double Buffering 26
Images may be stored involatile memory forperformance reasons.p
YHL Double Buffering 27
AnimationTest2Using ScreenManager
YHL Double Buffering 28
Using ScreenManagerUsing ScreenManager
POSSIBLE MODES: an array of different DisplayMode_ y p y
create ScreenManager
findFirstCompatibleMode(POSSIBLE_MODES)
setFullScreen(displayMode)getGraphicsdrawanimationLoop drawdisposeupdate
YHL Double Buffering 29
YHL Double Buffering 30
YHL Double Buffering 31
YHL Double Buffering 32
YHL Double Buffering 33
ECE 462Object-Oriented Programmingj g g
using C++ and Java
Moving Image: Sprite
Yung-Hsiang Lul @ d [email protected]
YHL Sprite 1
YHL Sprite 2
YHL Sprite 3
SpriteSprite
YHL Sprite 4
YHL Sprite 5
YHL Sprite 6
YHL Sprite 7
YHL Sprite 8
SpriteTest1SpriteTest1
YHL Sprite 9
YHL Sprite 10
YHL Sprite 11
YHL Sprite 12
YHL Sprite 13
YHL Sprite 14
ECE 462Object-Oriented Programmingj g g
using C++ and Java
Unified Modeling Language
Yung-Hsiang Lul @ d [email protected]
YHL UML 1
Unified Modeling Language UMLUnified Modeling Language UML
• why to model?y– abstraction, ignore details, or multiple levels of details– identify participating objects
communicate with people– communicate with people– use tools to generate code, check correctness ...
• why UML?– language independent– platform independent– international standard– international standard– expressive (state, sequence, time, interface ...)– tool rich (UML → code, code → UML ... )
YHL UML 2
YHL UML 3
YHL UML 4
UML Example in NetbeansUML Example in Netbeans
YHL UML 5
YHL UML 6
YHL UML 7
YHL UML 8
YHL UML 9
What Does a Model Say?What Does a Model Say?
• relationships among objectsp g j– which objects participate in an activity– actions and interfaces– transitions of attributes
• sequence of actions• specialization, composition, ownership • quantities
• In addition, one critical value of a model is to help a developer think before doing.
YHL UML 10
p g
Modeling a BankModeling a Bank
• objects:j– people: customers, tellers, bank managers, loan evaluators,
ATM maintainers ...– data: accountsdata: accounts– properties: branch offices, ATM machines, furniture ...
• actions: deposit, withdraw, apply for loans, approve or reject applications collect money from ATMapplications, collect money from ATM ...
• relationships among objects– customer can deposit, withdraw cash, or talk to teller– customer cannot talk to ATM maintainer– ATM maintainer cannot approve or reject loan applications– ATM can accept deposit; desk cannot
YHL UML 11
ATM can accept deposit; desk cannot ...
YHL UML 12
YHL UML 13
Model States and TransitionsModel States and Transitions
• Many objects' behaviors depend on the values (i.e. y j p (state) of the objects' attributes.– age ⇒ vote
account balance ⇒ withdraw– account balance ⇒ withdraw– available credit ⇒ purchase
• Objects' behaviors often follow strict orders based on the transitions of the attributes.– vending machine must accept payment before returning changes– customer must open an account before withdrawing money– customer must open an account before withdrawing money– a user must enter the password before checking email– a bank customer must insert the ATM card and enter PIN before
deposit
YHL UML 14
deposit
YHL UML 15
Sequence of ActionsSequence of Actions
• State diagrams do not express the objects involved. g p j• For example, the previous diagram does not specify the
necessity of a customer and an ATM machine.• In fact, three objects are involved
– customerATM– ATM
– account (and the balance)
YHL UML 16
YHL UML 17
Class DiagramClass Diagram
YHL UML 18
YHL UML 19
QuantityQuantity
YHL UML 20
YHL UML 21
Start a UML Project in NetbeansStart a UML Project in Netbeans
YHL UML 22
YHL UML 23
YHL UML 24
YHL UML 25
YHL UML 26