What have we learnt last week? Wireless Online Game Development for Mobile Device. Introduction to Thread. What is Thread?

Similar documents
Mobile Systeme Grundlagen und Anwendungen standortbezogener Dienste. Location Based Services in the Context of Web 2.0

Lab Exercise 4. Please follow the instruction in Workshop Note 4 to complete this exercise.

GUIDELINES FOR GAME DEVELOPERS USING NOKIA JAVA MIDP DEVICES Version Nov 02

Mobile Information Device Profile (MIDP) Alessandro Cogliati. Helsinki University of Technology Telecommunications Software and Multimedia Laboratory

IT101. Graphical User Interface

CIS233J Java Programming II. Threads

Lab Exercise 7. Please follow the instruction in Workshop Note 7 to complete this exercise.

Accessing DB2 Everyplace using J2ME devices, part 1

Java Threads. Written by John Bell for CS 342, Spring 2018

15CS45 : OBJECT ORIENTED CONCEPTS

J2ME crash course. Harald Holone

Software Development & Education Center. Java Platform, Micro Edition. (Mobile Java)

Road Map. Introduction to Java Applets Review applets that ship with JDK Make our own simple applets

CT 229 Arrays in Java

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

PESIT Bangalore South Campus

G52CON: Concepts of Concurrency

Mobile Application Development. J2ME - Forms

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Definition: A thread is a single sequential flow of control within a program.

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

CST242 Concurrency Page 1

(2½ hours) Total Marks: 75

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

Animation Part 2: MoveableShape interface & Multithreading

ST.MARTIN'S ENGINEERING COLLEGE Dhulapally,Secunderabad-014

Building a Java First-Person Shooter

CISC 1600 Lecture 3.1 Introduction to Processing

Introduction to the Java T M Language

CISC 1600, Lab 2.2: Interactivity in Processing

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

TAMZ. JavaME. MIDlets. Department of Computer Science VŠB-Technical University of Ostrava

1 Getting started with Processing


Acknowledgments Introduction p. 1 The Wireless Internet Revolution p. 1 Why Java Technology for Wireless Devices? p. 2 A Bit of History p.

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

CISC 1600, Lab 3.2: Interactivity in Processing

Exercise NMCGJ: Animated Graphics

PESIT Bangalore South Campus

Unit 8: Threads. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

Who am I? Wireless Online Game Development for Mobile Device. What games can you make after this course? Are you take the right course?

BVRIT HYDERABAD College of Engineering for Women Department of Information Technology. Hand Out

CSE1720 Lecture 08; Week 05 Lecture 09 Second level Third level Fourth level Fifth level

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Click on the empty form and apply the following options to the properties Windows.

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

Chapter 13 Add Multimedia to Your MIDlets

Polymorphism and Interfaces. CGS 3416 Spring 2018

CS 351 Design of Large Programs Threads and Concurrency

Introduction to Programming Using Java (98-388)

Chapter 7 Applets. Answers

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

SCHEME OF COURSE WORK

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

GRAPHICS & INTERACTIVE PROGRAMMING. Lecture 1 Introduction to Processing

Nutiteq Maps SDK tutorial for Java ME (J2ME)

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

UCLA PIC 20A Java Programming

Govt. of Karnataka, Department of Technical Education Diploma in Information Science & Engineering. Fifth Semester

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Overview of Java Threads (Part 2)

INTRODUCTION TO ANDROID

Using Inheritance to Share Implementations

Graphics -- To be discussed

Chapter 3: Inheritance and Polymorphism

Jorge Cardoso and Nuno Rodrigues

CPS221 Lecture: Threads

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

VALLIAMMAI ENGINEERING COLLEGE

Graphics Applets. By Mr. Dave Clausen

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

1 Getting started with Processing

Java - Applets. public class Buttons extends Applet implements ActionListener

Performance Throughput Utilization of system resources

Advanced Java 2014

How to Add Text to an Animated Image

Concurrent Programming

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

What is a Thread? Individual and separate unit of execution that is part of a process. multiple threads can work together to accomplish a common goal

SwingStates programming graphical interactions in Java

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Exploring Processing

CSE1720. Objectives for this class meeting 2/10/2014. Cover 2D Graphics topic: displaying images. Timer class, a basic ActionListener

Object Orientated Analysis and Design. Benjamin Kenwright

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Multithreading Pearson Education, Inc. All rights reserved.

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Threads Chate Patanothai

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Models of concurrency & synchronization algorithms

The AWT Package, Graphics- Introduction to Images

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Lesson 6 Programming Arduino Example 9.7. Chapter-9 L06: "Internet of Things ", Raj Kamal, Publs.: McGraw-Hill Education

Transcription:

What have we learnt last week? Wireless Online Game Development for Mobile Device Lesson 5 Introduction to Low-level API Display Image in Canvas Configure color, line style, font style for Drawing Drawing Line, Arc, Sector, Rectangle, Triangle and String by using the coordinate system. Deployment of your MIDP application How to clear the screen I123-1-A@Peter Lo 2007 1 I123-1-A@Peter Lo 2007 2 Introduction to Thread What is Thread? One of the defining features of the Java environment is its built-in support for threads. Threads let an application perform multiple activities simultaneously. When used properly, threads let the application's user interface remain responsive while it performs lengthy operations like network communication or very complicated computations. While user interface responsiveness is important no matter what the platform, it's even more so on the handheld and consumer-oriented devices that the J2ME platform targets. For a sequential programs, It has a beginning, an execution sequence, and an end. At any given time during the runtime of the program, there is a single point of execution. A thread is similar to the sequential programs. A single thread also has a beginning, a sequence, and an end and at any given time during the runtime of the thread, there is a single point of execution. A thread itself is not a program; it cannot run on its own. Rather, it runs within a program. I123-1-A@Peter Lo 2007 3 I123-1-A@Peter Lo 2007 4

What is Thread? Context of Thread A thread is a single sequential flow of control within a program Threads are the fundamental units of program execution. Every running application has at least one thread. An application consisting of two or more threads is known as a multithreaded application. Each thread has a context associated with it. The context holds information about the thread, such as the address of the currently executing instruction and storage for local variables. The context is updated as the thread executes. The context also stores the thread's state. I123-1-A@Peter Lo 2007 5 I123-1-A@Peter Lo 2007 6 State of Thread What is Thread Scheduling? A thread can be in any of the following states: A Running Thread is executing code. A Ready Thread is ready to execute code. A Suspended Thread is waiting on an external event (For example, it may be waiting for data to arrive from another device. After the event occurs, the thread transitions back to the ready state). A Terminated Thread has finished executing code. A thread that is running, ready, or suspended is a live thread. A terminated thread is also known as a dead thread. Although an application may have many threads, the underlying device has a small, fixed number of processors available for code execution. Threads share these processors by taking turns being in the running state. This arrangement is called thread scheduling. I123-1-A@Peter Lo 2007 7 I123-1-A@Peter Lo 2007 8

Thread Scheduling Thread scheduling is a complicated affair over which the application has little control. The system gives each ready thread a chance to run for a short time (the suspended threads are ignored), switching rapidly from one thread to another. This context switching can occur at any time. The only real influence the application has over thread scheduling is via the thread's priority level: higher-priority threads execute more often than those with lower priority. Implementation of Thread There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. I123-1-A@Peter Lo 2007 9 I123-1-A@Peter Lo 2007 10 1) Subclass of Thread For example, a thread that computes primes larger than a stated value could be written as follows: 2) Implementation of Runnable For example, a thread that computes primes larger than a stated value could be written as follows: The following code create a thread and start it running The following code create a thread and start it running I123-1-A@Peter Lo 2007 11 I123-1-A@Peter Lo 2007 12

Animation? Moving Pictures? The idea of moving images developed in 65 B.C. with the theory of persistence of motion, but it took more than another 1900 years before someone photographed a moving scene. Animation is the simulation of movement through a series of pictures that have objects in slightly different positions. How to Display Animation? For animation to work, the pictures making up the animation must replace one another quickly enough to trick the human eye into believing there is movement. A replacement rate of at least 14 frames per second or faster accomplishes this sense of movement I123-1-A@Peter Lo 2007 13 I123-1-A@Peter Lo 2007 14 Source of Animation Implementation of Animation When you perform animations, you can first calculate the display content and then call repaint() to paint the new frame. In order to control the repaint time slot, we can use Sleep(). I123-1-A@Peter Lo 2007 15 I123-1-A@Peter Lo 2007 16

Event Handling When an event occurs on a mobile device, a Command object holds information about that event. This information includes the type of command executed, the label of the command, and its priority. In J2ME, commands are commonly represented with soft-buttons on the device. Command Events We process Canvas Commands the same way we would those on any other component: Create an instance of a Command object. Add the Command to Canvas Create a Command listener to capture events. Write a commandaction() method to process events. I123-1-A@Peter Lo 2007 17 I123-1-A@Peter Lo 2007 18 Implement CommandListener class ImageCanvas extends Canvas implements CommandListener { private Command cmdexit; display = Display.getDisplay(this); cmdexit = new Command("Exit", Command.EXIT, 0); addcommand(cmdexit); setcommandlistener(this); protected void paint(graphics g) { // Draw onto the canvas public void commandaction(command c, Displayable d) { if (c == cmdexit) I123-1-A@Peter Lo 2007 19 Capture Key Press In addition to soft-keys for processing commands, a Canvas object has access to 12 keycodes. It can handle ITU-T codes for 0..9, * and # only The codes that are guaranteed to be available on any MIDP device are shown below: KEY_NUM0 KEY_NUM1 KEY_NUM2 KEY_NUM3 KEY_NUM4 KEY_NUM5 KEY_NUM6 KEY_NUM7 KEY_NUM8 KEY_NUM9 KEY_STAR KEY_POUND I123-1-A@Peter Lo 2007 20

Key Codes Handling The five methods for working with keycodes are as follows: void keypressed (int keycode) void keyreleased (int keycode) void keyrepeated (int keycode) boolean hasrepeatevents( ) String getkeyname (int keycode) Game Key Capture LEFT, RIGHT, UP, DOWN and FIRE are game key codes, that need to use getgameaction to handle Movement should be done in a thread Experience from J2ME MIDP for Palm: Don t implement touchable key-emulation because it is already implemented. Holding the key does not have the same impact on a palm devices as in mobile phones I123-1-A@Peter Lo 2007 21 I123-1-A@Peter Lo 2007 22 Game Actions Handling Detecting Game Actions The values of the game action are mapped to the device's directional arrows, but not all mobile devices have these. If a device lacks directional arrows, game actions will be mapped to the keypad (e.g., LEFT mapped to 2, RIGHT mapped to 5, etc ). The following constants have been defined for handling game-related events in MIDP: UP GAME_A DOWN GAME_B LEFT GAME_C RIGHT GAME_D FIRE I123-1-A@Peter Lo 2007 23 The following code sample shows one way to detect various game actions and call the appropriate methods based on the action selected. protected void keypressed(int keycode) { switch (getgameaction(keycode)) { case Canvas.FIRE: shoot(); break; case Canvas.RIGHT: goright(); break; I123-1-A@Peter Lo 2007 24

Detecting Game Actions Game Flow Control Another option would be to create a reference for each game action during initialization, then look for the appropriate key at runtime, as shown below: // Initialization keyfire = getkeycode(fire); keyright = getkeycode(right); keyleft = getkeycode(left); // Runtime protected void keypressed(int keycode) { if (keycode == keyfire) shoot(); else if (keycode == keyright) goright() I123-1-A@Peter Lo 2007 25 We can declare a variable to control such as GameScreen the game flow. 0 Opening 1 Game Playing 2 Drop to the Hole 9 Game Over I123-1-A@Peter Lo 2007 26 Add an Opening Screen To add an opening screen for the shooting game, you must load the image and display it when the game start. The simple method is adding the following code in the Paint class. public void paint(graphics g) { try { imgopening = Image.createImage("/opening.png"); g.drawimage(imgopening, 0, 0, g.top g.left); Start the Game After adding the opening screen, then need to start the thread to handle the event of Start Game when user press the [Start] button. public void commandaction(command c, Displayable s) { if (c == mstartcommand) MyThread.start(); I123-1-A@Peter Lo 2007 27 I123-1-A@Peter Lo 2007 28

Game Over Screen As mentioned above, GameScreen variable can be used to control the game flow. Therefore, we can add our event in the logic condition GameScreen == 9 Movement of the Ground To simulate the movement, you can set the ground move from right to left. I123-1-A@Peter Lo 2007 29 I123-1-A@Peter Lo 2007 30