SlideShare a Scribd company logo
Crea%ng	
  Games	
  for	
  	
  
Asha	
  -­‐	
  Pla3orm	
  
Jussi	
  Pohjolainen	
  	
  
TAMK	
  University	
  of	
  Applied	
  Sciences	
  
GRAPHICS	
  IN	
  MIDP	
  
Class	
  Hierarchy	
  
javax.microedi9on.lcdui	
  

javax.microedi9on.lcdui.game	
  

Displayable	
  

Canvas	
  

Screen	
  

Alert	
  

List	
  

Form	
  

TextBox	
  

GameCanvas	
  
Using	
  Graphics	
  
•  Class:	
  javax.microedition.lcdui.Canvas
•  Create	
  a	
  subclass:	
  
–  class MyCanvas extends Canvas

•  Canvas-­‐class	
  has	
  only	
  one	
  abstract	
  method:	
  
–  paint(graphics g)

•  It	
  is	
  possible	
  to	
  override	
  methods	
  that	
  deal	
  
with	
  events	
  
Simple	
  Example	
  
class MyCanvas extends Canvas {
public void paint(Graphics g){
// draw
}
}
class MyMidlet extends MIDlet{
public void startApp(){
MyCanvas mycanvas = new MyCanvas();
Display.getDisplay(this).setCurrent(mycanvas);
}
}
Repain%ng	
  
•  You	
  never	
  call	
  the	
  paint()	
  method.	
  
•  Instead	
  of	
  you	
  use	
  repaint():
–  By	
  using	
  this	
  method,	
  you	
  ask	
  the	
  framework	
  to	
  
repaint	
  the	
  canvas	
  
–  Framework	
  decides	
  when	
  is	
  the	
  best	
  %me	
  to	
  repaint	
  
the	
  canvas	
  

•  There	
  is	
  a	
  also:	
  
–  repaint(int x, int y, int width, int
height)
Coordinates	
  
•  Upper-­‐LeS	
  (0,0)	
  
•  Translate	
  the	
  origon	
  
–  translate()	
  –	
  metodi	
  

•  Origo's	
  posi%on:	
  
–  getTranslateX()
–  getTranslateY()

x"

y"
Graphics-­‐classes	
  drawing	
  methods	
  
•  See	
  the	
  API!	
  
–  drawLine(..)
–  drawRect(...)
–  drawRoundRect(...)
–  drawArc(...)
–  fillTriangle(...)
–  fillRect(...)
–  fillRoundRect(...)
–  fillArc(...)
Key	
  Handling	
  
class MyCanvas extends Canvas{
public void paint(Graphics g) { }
protected void keyReleased(int keyCode) { }
protected void keyRepeated(int keyCode) { }
protected void keyPressed(int keyCode) { }

}
class MyMidlet extends MIDlet{
public void startApp(){
MyCanvas mycanvas = new MyCanvas();
Display.getDisplay(this).setCurrent(mycanvas);
}
}
GAME	
  API	
  
Game	
  API	
  
•  It	
  is	
  easy	
  to	
  handle	
  anima%on	
  and	
  graphics	
  
with	
  the	
  Game	
  API	
  
•  All	
  the	
  classes	
  can	
  be	
  found	
  from	
  
javax.microedition.lcdui.game.*;
GameCanvas	
  
•  Using	
  the	
  tradi%onal	
  Canvas-­‐class	
  
–  You	
  inherit	
  the	
  Canvas	
  and	
  override	
  paint-­‐method.	
  
–  repaint()
–  Event	
  handling	
  is	
  done	
  by	
  using	
  methods	
  like	
  keypressed,	
  
keyreleased	
  

•  Using	
  the	
  GameCanvas-­‐class	
  
–  You	
  inherit	
  the	
  GameCanvas-­‐class	
  
–  There	
  is	
  no	
  need	
  for	
  paint-­‐method,	
  you	
  can	
  draw	
  anywhere!	
  	
  
–  flushGraphics()
–  Two	
  ways	
  of	
  doing	
  event	
  handling!
Example	
  of	
  GameCanvas	
  Usage	
  
class MyCanvas extends GameCanvas {
public void anymethod(){
Graphics g = getGraphics();
// some drawing
flushGraphics()
}
}
Handling	
  Events	
  
•  Constructor	
  of	
  GameCanvas	
  
–  protected GameCanvas(boolean
suppressKeyEvents)

•  You	
  have	
  to	
  call	
  this	
  constructor	
  in	
  you	
  own	
  
GameCanvas	
  class..	
  
–  =>	
  You	
  have	
  to	
  give	
  a	
  boolean	
  value..	
  
–  true:	
  use	
  only	
  GameCanvases	
  own	
  event	
  handling	
  
–  false:	
  in	
  addi4on	
  to	
  GameCanvases	
  own	
  event	
  handling	
  
use	
  Canvases	
  event	
  handling	
  
GameCanvas	
  Usage	
  
class MyCanvas extends GameCanvas {
public MyCanvas() {
// Let's use Game Canvas event handling!
super(true);
}
public void anymethod() {
// drawing..
}
}
Event	
  Handling	
  
•  You	
  can	
  ask	
  which	
  bu]on	
  is	
  pressed	
  
(GameCanvas	
  Event	
  Handling)	
  
–  public int getKeyStates()

•  Bit-­‐finals	
  of	
  GameCanvas	
  
–  UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED,
RIGHT_PRESSED, FIRE_PRESSED,
GAME_A_PRESSED, GAME_B_PRESSED,
GAME_C_PRESSED, GAME_D_PRESSED
GameCanvas	
  Example	
  3	
  
class MyCanvas extends GameCanvas implements Runnable {
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
int ks = getKeyStates();
if ((ks & UP_PRESSED) != 0)
moveUp();
else if((ks & DOWN_PRESSED) != 0)
moveDown();
// Drawing...
}
}
}
Touch	
  
class MyCanvas extends GameCanvas implements Runnable {
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
doSomething();
}
}
public void pointerPressed(int x, int y) { .. }
public void pointerReleased(int x, int y) { .. }
public void pointerDragged(int x, int y) { .. }
}
GameLoop	
  and	
  FPS	
  
class MyCanvas extends GameCanvas implements Runnable {
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
// THIS WILL LOOP AS FAST AS IT CAN!
while(true) {
doSomething();
}
}
}
GameLoop	
  and	
  FPS	
  
class MyCanvas extends GameCanvas implements Runnable {
private int fps = 1;
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
// Now iteration is 1 frames per sec
while(true) {
doSomething();
try {
Thread.sleep(1000 / fps);
} catch (InterruptedException e) { .. }
}
}
}
GameLoop	
  and	
  FPS	
  
class MyCanvas extends GameCanvas implements Runnable {
private int fps = 30;
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {

// Problem, what if the mobile phone can keep up with the pace?
while(true) {

// This takes second…
doSomethingHeavyAndMagical3DStuff();
try {
Thread.sleep(1000 / fps); // and after the second, we will wait more!
} catch (InterruptedException e) { .. }
}
}
}
class MyCanvas extends GameCanvas implements Runnable {
private int fps = 30;
public
long
long
long
long

void run() {
time = 0;
elapsedTime = 0;
interval = 0;
sleepTime = 0;

while(true) {
// Get current time
time = System.currentTimeMillis();
doSomethingHeavyAndMagical3DStuff();

// Elapsed time
elapsedTime = System.currentTimeMillis() - time;
// Do we need to sleep?
sleepTime = 0;
// If elapsed time was 100 millisecs, then
//
1000 / 30 - 100 = -66,66. Sleep is not needed (= 0)!
// If elapsed time was 1 millisecs, then sleepTime:
//
1000 / 30 - 1 = 32,333.
// If elapsed time was 20 millisecs, then sleepTime:
//
1000 / 30 - 20 = 13,333..
interval = (int) ((1000.0 / fps) - elapsedTime);
if(interval > 0) {
sleepTime = interval;
}
try {

}

}

}

Thread.sleep(sleepTime);
} catch (InterruptedException e) { }
Layers	
  
•  You	
  can	
  use	
  layers	
  with	
  the	
  Game	
  canvas.	
  
•  For	
  example:	
  
–  background-­‐layer	
  (bo]om)	
  
–  car-­‐layer	
  (front	
  of	
  the	
  background)	
  
•  javax.microedition.lcdui.game.Layer
Layer-­‐class	
  
•  Layer	
  class	
  is	
  abstract	
  and	
  it	
  has	
  two	
  concrete	
  
subclasses:	
  1)	
  TiledLayer,	
  2)	
  Sprite
•  Layer's	
  methods	
  
–  int getX()
–  int getY()
–  int getWidth()
–  int getHeight()
–  void setPosition(..)
–  move(..)
Class	
  Diagram	
  
{abstract}	
  Layer	
  
int	
  getX()	
  
int	
  getY()	
  
int	
  getWidth()	
  
int	
  getHeight()	
  
void	
  setPosi%on(..)	
  
move(..)	
  
Sprite	
  

TiledLayer	
  
Mastering	
  the	
  layers	
  
•  Every	
  layer	
  (Sprite	
  or	
  TiledLayer)	
  is	
  put	
  into	
  a	
  
LayerManager.	
  The	
  LayerManager is	
  
eventually	
  drawn	
  to	
  the	
  screen.	
  
•  LayerManager's	
  methods	
  
–  append(Layer l)
–  insert(Layer l, int i)
–  Layer getLayer(int i)
–  paint(..)
Class	
  Diagram	
  
LayerManager	
  

{abstract}	
  Layer	
  

append(Layer	
  l)	
  
insert(Layer	
  l,	
  int	
  i)	
  
Layer	
  getLayer(int	
  i)	
  
paint(..)	
  

int	
  getX()	
  
*	
   int	
  getY()	
  
int	
  getWidth()	
  
int	
  getHeight()	
  
void	
  setPosi%on(..)	
  
move(..)	
  
Sprite	
  

TiledLayer	
  
LayerManager:	
  setViewWindow!
•  public void setViewWindow(int x, int y, int width,
int height)
•  What	
  part	
  of	
  a	
  big	
  picture	
  is	
  shown	
  on	
  the	
  screen:	
  
Sprite	
  -­‐	
  class	
  
•  Sprite	
  classes	
  constructors:	
  
–  public Sprite(Image i)
–  public Sprite(Image i, int framewidth,
int frameheight)
Example	
  of	
  Using	
  Sprite	
  and	
  
LayoutManager	
  
LayerManager l = new LayerManager();
Sprite s = new Sprite(myimage);
s.setPosition(50,50);
l.append(s);
Graphics g = getGraphics();
l.paint(g,0,0);
flushGraphics();
Sprite	
  anima%on	
  
•  Make	
  one	
  image-­‐file,	
  which	
  contains	
  all	
  the	
  
frames	
  
•  In	
  the	
  Sprite's	
  constructor	
  you	
  define	
  one	
  
frame's	
  height	
  and	
  width	
  
•  ASer	
  that	
  you	
  can	
  use	
  Sprite's	
  nextFrame()	
  
method	
  
Example	
  

Sprite x = new Sprite(image, 540/18, 30);
layermanager.append(x);
.
.
x.nextFrame();
With	
  Threads..	
  
public void run() {!
while(true){!
int ks = getKeyStates();!
if((ks & RIGHT_PRESSED) != 0){!
mysprite.move(3,0);!
mysprite.nextFrame();!
}!
}!
}!
Influencing	
  frames	
  
•  Changing	
  sequence	
  
–  int sequence [] = {0, 15, 17};
–  mysprite.setFrameSequence(sequence)

•  Jumping	
  to	
  another	
  frame	
  
–  mysprite.setFrame(10);
Transforma%on	
  
•  It	
  is	
  possible	
  to	
  transform	
  the	
  sprite	
  
–  public void setTransform(int transform)

•  Finals	
  
–  TRANS_NONE
–  TRANS_ROT90
–  TRANS_MIRROR
–  ..	
  see	
  the	
  api	
  

•  In	
  prac%ce	
  
–  mysprite.setTransform(Sprite.TRANS_MIRROR)
Reference	
  Pixel	
  

Reference pixel"
Reference	
  Pixel	
  
mysprite.defineReferencePixel(15,15);

Reference pixel"
Collisions	
  
•  Sprite	
  
–  collidesWith(Sprite s, boolean pixelLevel)
–  collidesWith(TiledLayer s, boolean pixelLevel)
–  collidesWith(Image s, int x, int y, boolean pixelLevel)

•  PixelLevel	
  
–  The	
  rect	
  of	
  the	
  sprite	
  or	
  the	
  "real	
  pixels"	
  
Example	
  
Sprite x = new Sprite(...);
Sprite y = new Sprite(...);
if(x.collidesWith(y, true)){
// CRASH!

}
TiledLayer	
  
•  A	
  TiledLayer	
  is	
  a	
  visual	
  element	
  composed	
  of	
  a	
  
grid	
  of	
  cells	
  that	
  can	
  be	
  filled	
  with	
  a	
  set	
  of	
  %le	
  
images.	
  
•  Rows	
  and	
  Columns	
  
–  TiledLayer(int columns, int rows, Image
i, int tileWidth, int tileHeight);
Example	
  
TiledLayer a = new TiledLayer(4,2, picture, 32, 32);
a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1);
a.setCell(3,1,1);
a.setCell(1,0,2);
a.setCell(2,0,3);

1"

2"

3"

0" 1" 2" 3"
0"
1"
Ad

More Related Content

What's hot (18)

14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
Adil Jafri
 
Real life XNA
Real life XNAReal life XNA
Real life XNA
Johan Lindfors
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
Jussi Pohjolainen
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲
吳錫修 (ShyiShiou Wu)
 
Developing games for Series 40 full-touch UI
Developing games for Series 40 full-touch UIDeveloping games for Series 40 full-touch UI
Developing games for Series 40 full-touch UI
Microsoft Mobile Developer
 
Unity programming 1
Unity programming 1Unity programming 1
Unity programming 1
Petri Lankoski
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Unity Technologies
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
Mahmoud Samir Fayed
 
Python book
Python bookPython book
Python book
Victor Rabinovich
 
The Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 MinutesThe Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 Minutes
Brandon McInnis
 
Breathing the life into the canvas
Breathing the life into the canvasBreathing the life into the canvas
Breathing the life into the canvas
Tomislav Homan
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88
Mahmoud Samir Fayed
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
吳錫修 (ShyiShiou Wu)
 
Chapter ii(coding)
Chapter ii(coding)Chapter ii(coding)
Chapter ii(coding)
Chhom Karath
 
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha PhonesGetting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Microsoft Mobile Developer
 
7java Events
7java Events7java Events
7java Events
Adil Jafri
 
OpenGL L05-Texturing
OpenGL L05-TexturingOpenGL L05-Texturing
OpenGL L05-Texturing
Mohammad Shaker
 
14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
Adil Jafri
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲
吳錫修 (ShyiShiou Wu)
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Unity Technologies
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
Mahmoud Samir Fayed
 
The Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 MinutesThe Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 Minutes
Brandon McInnis
 
Breathing the life into the canvas
Breathing the life into the canvasBreathing the life into the canvas
Breathing the life into the canvas
Tomislav Homan
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88
Mahmoud Samir Fayed
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
吳錫修 (ShyiShiou Wu)
 
Chapter ii(coding)
Chapter ii(coding)Chapter ii(coding)
Chapter ii(coding)
Chhom Karath
 
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha PhonesGetting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Microsoft Mobile Developer
 

Viewers also liked (7)

ADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming API
Michael Remijan
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
Jussi Pohjolainen
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
iOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationiOS Selectors Blocks and Delegation
iOS Selectors Blocks and Delegation
Jussi Pohjolainen
 
Compiling Qt Apps
Compiling Qt AppsCompiling Qt Apps
Compiling Qt Apps
Jussi Pohjolainen
 
ADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming API
Michael Remijan
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
iOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationiOS Selectors Blocks and Delegation
iOS Selectors Blocks and Delegation
Jussi Pohjolainen
 
Ad

Similar to Creating Games for Asha - platform (20)

MIDP: Game API
MIDP: Game APIMIDP: Game API
MIDP: Game API
Jussi Pohjolainen
 
Scmad Chapter07
Scmad Chapter07Scmad Chapter07
Scmad Chapter07
Marcel Caraciolo
 
java_for_future_15-Multithreaded-Graphics.pptx
java_for_future_15-Multithreaded-Graphics.pptxjava_for_future_15-Multithreaded-Graphics.pptx
java_for_future_15-Multithreaded-Graphics.pptx
khoand6055
 
Graphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsGraphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLets
Jussi Pohjolainen
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
feelinggifts
 
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Programming Homework Help
 
Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2
Marlon Luz
 
Java ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics EJava ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics E
Andreas Jakl
 
Mapping the world with Twitter
Mapping the world with TwitterMapping the world with Twitter
Mapping the world with Twitter
carlo zapponi
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
DouO
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
DouO
 
Stop running from animations droidcon London
Stop running from animations droidcon LondonStop running from animations droidcon London
Stop running from animations droidcon London
maric_iv
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
Tomi Aarnio
 
Criando meu 1º game com android
Criando meu 1º game com androidCriando meu 1º game com android
Criando meu 1º game com android
Daniel Baccin
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
Ankara JUG
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game API
Andreas Jakl
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
Korhan Bircan
 
Peint talk
Peint talkPeint talk
Peint talk
CyruzDraxs
 
A More Flash Like Web?
A More Flash Like Web?A More Flash Like Web?
A More Flash Like Web?
Murat Can ALPAY
 
Google Fit, Android Wear & Xamarin
Google Fit, Android Wear & XamarinGoogle Fit, Android Wear & Xamarin
Google Fit, Android Wear & Xamarin
Peter Friese
 
java_for_future_15-Multithreaded-Graphics.pptx
java_for_future_15-Multithreaded-Graphics.pptxjava_for_future_15-Multithreaded-Graphics.pptx
java_for_future_15-Multithreaded-Graphics.pptx
khoand6055
 
Graphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsGraphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLets
Jussi Pohjolainen
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
feelinggifts
 
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Programming Homework Help
 
Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2
Marlon Luz
 
Java ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics EJava ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics E
Andreas Jakl
 
Mapping the world with Twitter
Mapping the world with TwitterMapping the world with Twitter
Mapping the world with Twitter
carlo zapponi
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
DouO
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
DouO
 
Stop running from animations droidcon London
Stop running from animations droidcon LondonStop running from animations droidcon London
Stop running from animations droidcon London
maric_iv
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
Tomi Aarnio
 
Criando meu 1º game com android
Criando meu 1º game com androidCriando meu 1º game com android
Criando meu 1º game com android
Daniel Baccin
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
Ankara JUG
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game API
Andreas Jakl
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
Korhan Bircan
 
Google Fit, Android Wear & Xamarin
Google Fit, Android Wear & XamarinGoogle Fit, Android Wear & Xamarin
Google Fit, Android Wear & Xamarin
Peter Friese
 
Ad

More from Jussi Pohjolainen (19)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
Jussi Pohjolainen
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Jussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
Jussi Pohjolainen
 
Intro to Java ME and Asha Platform
Intro to Java ME and Asha PlatformIntro to Java ME and Asha Platform
Intro to Java ME and Asha Platform
Jussi Pohjolainen
 
Intro to PhoneGap
Intro to PhoneGapIntro to PhoneGap
Intro to PhoneGap
Jussi Pohjolainen
 
Quick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery MobileQuick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery Mobile
Jussi Pohjolainen
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
Jussi Pohjolainen
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
Jussi Pohjolainen
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
Jussi Pohjolainen
 
XAMPP
XAMPPXAMPP
XAMPP
Jussi Pohjolainen
 
Building Web Services
Building Web ServicesBuilding Web Services
Building Web Services
Jussi Pohjolainen
 
CSS
CSSCSS
CSS
Jussi Pohjolainen
 
Extensible Stylesheet Language
Extensible Stylesheet LanguageExtensible Stylesheet Language
Extensible Stylesheet Language
Jussi Pohjolainen
 
About Http Connection
About Http ConnectionAbout Http Connection
About Http Connection
Jussi Pohjolainen
 

Recently uploaded (20)

ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 

Creating Games for Asha - platform

  • 1. Crea%ng  Games  for     Asha  -­‐  Pla3orm   Jussi  Pohjolainen     TAMK  University  of  Applied  Sciences  
  • 3. Class  Hierarchy   javax.microedi9on.lcdui   javax.microedi9on.lcdui.game   Displayable   Canvas   Screen   Alert   List   Form   TextBox   GameCanvas  
  • 4. Using  Graphics   •  Class:  javax.microedition.lcdui.Canvas •  Create  a  subclass:   –  class MyCanvas extends Canvas •  Canvas-­‐class  has  only  one  abstract  method:   –  paint(graphics g) •  It  is  possible  to  override  methods  that  deal   with  events  
  • 5. Simple  Example   class MyCanvas extends Canvas { public void paint(Graphics g){ // draw } } class MyMidlet extends MIDlet{ public void startApp(){ MyCanvas mycanvas = new MyCanvas(); Display.getDisplay(this).setCurrent(mycanvas); } }
  • 6. Repain%ng   •  You  never  call  the  paint()  method.   •  Instead  of  you  use  repaint(): –  By  using  this  method,  you  ask  the  framework  to   repaint  the  canvas   –  Framework  decides  when  is  the  best  %me  to  repaint   the  canvas   •  There  is  a  also:   –  repaint(int x, int y, int width, int height)
  • 7. Coordinates   •  Upper-­‐LeS  (0,0)   •  Translate  the  origon   –  translate()  –  metodi   •  Origo's  posi%on:   –  getTranslateX() –  getTranslateY() x" y"
  • 8. Graphics-­‐classes  drawing  methods   •  See  the  API!   –  drawLine(..) –  drawRect(...) –  drawRoundRect(...) –  drawArc(...) –  fillTriangle(...) –  fillRect(...) –  fillRoundRect(...) –  fillArc(...)
  • 9. Key  Handling   class MyCanvas extends Canvas{ public void paint(Graphics g) { } protected void keyReleased(int keyCode) { } protected void keyRepeated(int keyCode) { } protected void keyPressed(int keyCode) { } } class MyMidlet extends MIDlet{ public void startApp(){ MyCanvas mycanvas = new MyCanvas(); Display.getDisplay(this).setCurrent(mycanvas); } }
  • 11. Game  API   •  It  is  easy  to  handle  anima%on  and  graphics   with  the  Game  API   •  All  the  classes  can  be  found  from   javax.microedition.lcdui.game.*;
  • 12. GameCanvas   •  Using  the  tradi%onal  Canvas-­‐class   –  You  inherit  the  Canvas  and  override  paint-­‐method.   –  repaint() –  Event  handling  is  done  by  using  methods  like  keypressed,   keyreleased   •  Using  the  GameCanvas-­‐class   –  You  inherit  the  GameCanvas-­‐class   –  There  is  no  need  for  paint-­‐method,  you  can  draw  anywhere!     –  flushGraphics() –  Two  ways  of  doing  event  handling!
  • 13. Example  of  GameCanvas  Usage   class MyCanvas extends GameCanvas { public void anymethod(){ Graphics g = getGraphics(); // some drawing flushGraphics() } }
  • 14. Handling  Events   •  Constructor  of  GameCanvas   –  protected GameCanvas(boolean suppressKeyEvents) •  You  have  to  call  this  constructor  in  you  own   GameCanvas  class..   –  =>  You  have  to  give  a  boolean  value..   –  true:  use  only  GameCanvases  own  event  handling   –  false:  in  addi4on  to  GameCanvases  own  event  handling   use  Canvases  event  handling  
  • 15. GameCanvas  Usage   class MyCanvas extends GameCanvas { public MyCanvas() { // Let's use Game Canvas event handling! super(true); } public void anymethod() { // drawing.. } }
  • 16. Event  Handling   •  You  can  ask  which  bu]on  is  pressed   (GameCanvas  Event  Handling)   –  public int getKeyStates() •  Bit-­‐finals  of  GameCanvas   –  UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED
  • 17. GameCanvas  Example  3   class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { while(true) { int ks = getKeyStates(); if ((ks & UP_PRESSED) != 0) moveUp(); else if((ks & DOWN_PRESSED) != 0) moveDown(); // Drawing... } } }
  • 18. Touch   class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { while(true) { doSomething(); } } public void pointerPressed(int x, int y) { .. } public void pointerReleased(int x, int y) { .. } public void pointerDragged(int x, int y) { .. } }
  • 19. GameLoop  and  FPS   class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { // THIS WILL LOOP AS FAST AS IT CAN! while(true) { doSomething(); } } }
  • 20. GameLoop  and  FPS   class MyCanvas extends GameCanvas implements Runnable { private int fps = 1; public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { // Now iteration is 1 frames per sec while(true) { doSomething(); try { Thread.sleep(1000 / fps); } catch (InterruptedException e) { .. } } } }
  • 21. GameLoop  and  FPS   class MyCanvas extends GameCanvas implements Runnable { private int fps = 30; public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { // Problem, what if the mobile phone can keep up with the pace? while(true) { // This takes second… doSomethingHeavyAndMagical3DStuff(); try { Thread.sleep(1000 / fps); // and after the second, we will wait more! } catch (InterruptedException e) { .. } } } }
  • 22. class MyCanvas extends GameCanvas implements Runnable { private int fps = 30; public long long long long void run() { time = 0; elapsedTime = 0; interval = 0; sleepTime = 0; while(true) { // Get current time time = System.currentTimeMillis(); doSomethingHeavyAndMagical3DStuff(); // Elapsed time elapsedTime = System.currentTimeMillis() - time; // Do we need to sleep? sleepTime = 0; // If elapsed time was 100 millisecs, then // 1000 / 30 - 100 = -66,66. Sleep is not needed (= 0)! // If elapsed time was 1 millisecs, then sleepTime: // 1000 / 30 - 1 = 32,333. // If elapsed time was 20 millisecs, then sleepTime: // 1000 / 30 - 20 = 13,333.. interval = (int) ((1000.0 / fps) - elapsedTime); if(interval > 0) { sleepTime = interval; } try { } } } Thread.sleep(sleepTime); } catch (InterruptedException e) { }
  • 23. Layers   •  You  can  use  layers  with  the  Game  canvas.   •  For  example:   –  background-­‐layer  (bo]om)   –  car-­‐layer  (front  of  the  background)   •  javax.microedition.lcdui.game.Layer
  • 24. Layer-­‐class   •  Layer  class  is  abstract  and  it  has  two  concrete   subclasses:  1)  TiledLayer,  2)  Sprite •  Layer's  methods   –  int getX() –  int getY() –  int getWidth() –  int getHeight() –  void setPosition(..) –  move(..)
  • 25. Class  Diagram   {abstract}  Layer   int  getX()   int  getY()   int  getWidth()   int  getHeight()   void  setPosi%on(..)   move(..)   Sprite   TiledLayer  
  • 26. Mastering  the  layers   •  Every  layer  (Sprite  or  TiledLayer)  is  put  into  a   LayerManager.  The  LayerManager is   eventually  drawn  to  the  screen.   •  LayerManager's  methods   –  append(Layer l) –  insert(Layer l, int i) –  Layer getLayer(int i) –  paint(..)
  • 27. Class  Diagram   LayerManager   {abstract}  Layer   append(Layer  l)   insert(Layer  l,  int  i)   Layer  getLayer(int  i)   paint(..)   int  getX()   *   int  getY()   int  getWidth()   int  getHeight()   void  setPosi%on(..)   move(..)   Sprite   TiledLayer  
  • 28. LayerManager:  setViewWindow! •  public void setViewWindow(int x, int y, int width, int height) •  What  part  of  a  big  picture  is  shown  on  the  screen:  
  • 29. Sprite  -­‐  class   •  Sprite  classes  constructors:   –  public Sprite(Image i) –  public Sprite(Image i, int framewidth, int frameheight)
  • 30. Example  of  Using  Sprite  and   LayoutManager   LayerManager l = new LayerManager(); Sprite s = new Sprite(myimage); s.setPosition(50,50); l.append(s); Graphics g = getGraphics(); l.paint(g,0,0); flushGraphics();
  • 31. Sprite  anima%on   •  Make  one  image-­‐file,  which  contains  all  the   frames   •  In  the  Sprite's  constructor  you  define  one   frame's  height  and  width   •  ASer  that  you  can  use  Sprite's  nextFrame()   method  
  • 32. Example   Sprite x = new Sprite(image, 540/18, 30); layermanager.append(x); . . x.nextFrame();
  • 33. With  Threads..   public void run() {! while(true){! int ks = getKeyStates();! if((ks & RIGHT_PRESSED) != 0){! mysprite.move(3,0);! mysprite.nextFrame();! }! }! }!
  • 34. Influencing  frames   •  Changing  sequence   –  int sequence [] = {0, 15, 17}; –  mysprite.setFrameSequence(sequence) •  Jumping  to  another  frame   –  mysprite.setFrame(10);
  • 35. Transforma%on   •  It  is  possible  to  transform  the  sprite   –  public void setTransform(int transform) •  Finals   –  TRANS_NONE –  TRANS_ROT90 –  TRANS_MIRROR –  ..  see  the  api   •  In  prac%ce   –  mysprite.setTransform(Sprite.TRANS_MIRROR)
  • 38. Collisions   •  Sprite   –  collidesWith(Sprite s, boolean pixelLevel) –  collidesWith(TiledLayer s, boolean pixelLevel) –  collidesWith(Image s, int x, int y, boolean pixelLevel) •  PixelLevel   –  The  rect  of  the  sprite  or  the  "real  pixels"  
  • 39. Example   Sprite x = new Sprite(...); Sprite y = new Sprite(...); if(x.collidesWith(y, true)){ // CRASH! }
  • 40. TiledLayer   •  A  TiledLayer  is  a  visual  element  composed  of  a   grid  of  cells  that  can  be  filled  with  a  set  of  %le   images.   •  Rows  and  Columns   –  TiledLayer(int columns, int rows, Image i, int tileWidth, int tileHeight);
  • 41. Example   TiledLayer a = new TiledLayer(4,2, picture, 32, 32); a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1); a.setCell(3,1,1); a.setCell(1,0,2); a.setCell(2,0,3); 1" 2" 3" 0" 1" 2" 3" 0" 1"