SlideShare uma empresa Scribd logo
1 de 39
Java™Platform, Micro Edition Part 3 – Low Level Graphics v3.0b – 02 April 2009 1 Andreas Jakl, 2009
Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Java ME courses at the University of Applied Sciences in Hagenberg, Austria at the Mobile Computing department ( http://www.fh-ooe.at/mc ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. This document contains copyright materials which are proprietary to Sun or various mobile device manufacturers, including Nokia, SonyEricsson and Motorola. Sun, Sun Microsystems, the Sun Logo and the Java™ Platform, Micro Edition are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.  Andreas Jakl, 2009 2
Contents Low Level Graphics Canvas Key Events Geometry & Text Images Andreas Jakl, 2009 3
Hierarchy of Displayables Andreas Jakl, 2009 Display One Display instance / MIDlet Available in all sub-classes of Displayable Command Displayable Methods for drawing to a canvas Canvas Screen Ticker Graphics TextBox Form List Alert Item Spacer CustomItem Choice (Interface) ChoiceGroup StringItem DateField ImageItem TextField Gauge 4
Canvas Canvas = base class Derive your own class from Canvas Canvas assigned to the screen like high level UI  can be mixed with high level UI! Functionality: Key events (key codes) Pointer events Paint (must be implemented) Visible / invisible Query properties Andreas Jakl, 2009 5
Example: Canvas – Quick Overview Andreas Jakl, 2009 6 public class RealReplayMIDletextendsMIDlet { private booleaniFirstStart = true; private MenuCanvasiMainCanvas; publicRealReplayMIDlet() { } protected void startApp() { // Startapp also gets called when the midlet gains foreground again         // -> Only initialize everything in the very first start-up. if (iFirstStart)         { iMainCanvas = newMenuCanvas(this);     // Create the main canvas Display.getDisplay(this).setCurrent(iMainCanvas);   // Activate the canvas iFirstStart = false;         }     } publicvoidpauseApp() { } publicvoiddestroyApp(boolean unconditional) { } public void exit() { destroyApp(true); notifyDestroyed();     } }
Example: Canvas – Quick Overview Andreas Jakl, 2009 7 public class MenuCanvasextends Canvas { privateRealReplayMIDletiMidlet;     private intiWidth;     private intiHeight; publicMenuCanvas(RealReplayMIDletaMidlet) { iMidlet = aMidlet;                   // Needed to quit the game setFullScreenMode(true);     // Activate full screen mode iWidth = getWidth();	  // Query screen size iHeight = getHeight();        } public void paint(Graphics aGraphics)     {         // Draw background aGraphics.setColor(255, 255, 255); aGraphics.fillRect(0, 0, iWidth, iHeight);         // [...]     } protected void keyPressed(intaKeyCode)     {         // Process key events [...]          repaint();     } }
The Life of a Canvas Andreas Jakl, 2009 8 Canvasconstructor display.setCurrent(myCanvas) showNotify() paint() … events … keyPressed() pointerPressed() display.setCurrent(myForm) hideNotify()
Canvas – Overview Andreas Jakl, 2009 9 getWidth() (0, 0) setFullScreenMode(true) drawImage() drawString() getHeight() fillRoundRect() drawRoundRect() fillRect()
Canvas – Full Screen setFullScreenMode(false); setFullScreenMode(true); Andreas Jakl, 2009 10
Canvas – Show and Hide showNotify() Is called directly before Canvas becomes visible For initialization, e.g. (re)starting a timer hideNotify() Called after becoming invisible Free resources, cancel timers and threads, ... Also called by the AMS when system screens (e.g., incoming call) are displayed on top Query visibility: Displayable.isShown() Andreas Jakl, 2009 11
Canvas – Events Events sent to (visible) Canvas Calls methods defined in Canvas base class Key- & pointer-events, commands Serial event delivery No second event is sent before first event is handled (= you left the event handling method) Exception: repaint enforced by you Andreas Jakl, 2009 12
Key Events keyPressed(intkeyCode) When a key is pressed down keyRepeated(intkeyCode) Called again and again when the key is held down Frequency depends on mobile phone   better: use an own timer until … keyReleased(intkeyCode) When the key is released Andreas Jakl, 2009 13 keyPressed() keyRepeated() keyReleased()
Key Codes protectedvoidkeyPressed(intkeycode) Constantsaredefinedforstandardkeys Andreas Jakl, 2009 14 Defined in the Canvasclass
Key Codes – Game Actions Phones: no standardized key layout Therefore: game actions – appropriate keys for each phone intgetGameAction (intkeyCode) Andreas Jakl, 2009 15 Defined in the Canvasclass
Key Codes – Example Andreas Jakl, 2009 16 protected void keyPressed(intkeycode) { SimplePlayerGUIgui = getGUI(); switch (keycode) {		// This variant does not allow handling multiple simultaneous keypresses case Canvas.KEY_NUM1:	// (which are not supported by many phones) // Jump backward gui.skip(true); break; caseCanvas.KEY_NUM2: gui.togglePlayer(); break; // [...] caseCanvas.KEY_POUND: gui.changeVolume(false); break; default:	// Handle both keys and game actions – keys first, as they might be game actions as well intgameAction = getGameAction(keycode); if (gameAction == Canvas.RIGHT) { // Jump forward gui.skip(false);         // [...]         } elseif (gameAction == Canvas.FIRE) { gui.togglePlayer();         }     } Adapted from the Sun WTK 2.5.2. MobileMediaAPI-examle
Canvas – Soft Keys, … Problem: MIDP 2.0 does not define keycode constants for softkeys, delete, ...   Different keycodesfor every manufacturer Solutions: Use Commands (problematic: high level menu doesn’t fit to your design!) Compile an own version for every manufacturer Use generic ‘translation’-method – within certain limits Example – left softkeykeycode: -6 (Nokia), -1 (Siemens), -21 (Motorola), 21 (Motorola) Andreas Jakl, 2009 17
Canvas – Paint If repaint is necessary (e.g. info dialog was on top of MIDlet) Framework calls Canvas.paint() Request repaint yourself Call: repaint() Optional: specify region paint()will be called automatically andasynchronously, at a “fitting” moment Double Buffering Usually supported and activated Query: booleandblBuf = canvas.isDoubleBuffered(); If not: create off-screen bitmap yourself Andreas Jakl, 2009 18 void keyPressed(int aKeyCode) {     // Process key events [...]      repaint(); } paint() not called instantly; usually through an extra thread void paint(Graphics g) {     // Draw... }
Graphics Drawing tothe Screen Andreas Jakl, 2009 19
Graphics Graphics-object sent to paint() as a parameter Provides methods for drawing of: Line (SOLID or DOTTED) Rectangle (also filled and/or with rounded corners) Arc (part of an ellipse, also filled) Triangle (also filled) Text Images Andreas Jakl, 2009 20 void paint(Graphics g) {     // Draw... }
Colours Colour is used for all drawing operations that follow (including text) g.setColor(int red, int green, int blue); Usually no 32 bit display: Automatic assignment of nearest displayable colour no colour fidelity Andreas Jakl, 2009 21 void paint(Graphics g) { g.setColor(255, 255, 255); 	// White g.fillRect(0, 0, getWidth(), getHeight());		// Fill the whole background g.setColor(255, 0, 0);  	// Red g.drawLine(5, 5, 250, 250);         // Centered text g.drawString("Hello", getWidth() / 2, getHeight() / 2, Graphics.HCENTER|Graphics.BASELINE); }
Text Definepositionusinganchorpoints Images: similar, but useVCenterinsteadofBaseline Andreas Jakl, 2009 22 Left HCenter Right Top Mopius Baseline Bottom
Example: g.drawString(“Mopius”, 0, 0, Graphics.HCENTER|Graphics.BASELINE); Canvas Text – Positioning Andreas Jakl, 2009 23 Mopius g.drawString(“Mopius”, 0, 0, Graphics.LEFT|Graphics.TOP); Mopius
Recap: OR Andreas Jakl, 2009 24 OR executed for corresponding bits of two values Overview: OR Both values that were combined withor can be retrieved later on Baseline is contained in the combined value!
Text – Font Choose through Font-object, then assign to Graphics-obj. Request font based on criteria: Face: monospace, proportional, system Style: plain, bold, italic, underlined Size:large, medium, small Example: Font titleFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD|Font.STYLE_ITALIC, Font.SIZE_LARGE);g.setFont(titleFont);g.drawString(“Test”, 0, 0, Graphics.TOP|Graphics.LEFT); Andreas Jakl, 2009 25
Text – Font Sun WTK Nokia Series 40 (Emulator) Andreas Jakl, 2009 26 … mostgamesusetheirownbitmapfonts!
Geometry Definesettings g.setStrokeStyle(Graphics.DOTTED); // orGraphics.SOLID Example: protectedvoidpaint (Graphics g) {// Draw backgroundg.setColor(255, 255, 255);g.fillRect(0, 0, getWidth(), getHeight());// Draw strokeg.setStrokeStyle(Graphics.DOTTED);g.setColor(255, 0, 0);g.drawLine(0, 0, getWidth(), getHeight());} Andreas Jakl, 2009 27
Coordinate System Default: Top Left = 0 / 0 Visible areacanbemovedthroughtranslate() Andreas Jakl, 2009 28 protectedvoidpaint (Graphics g) {   //g.translate(50, 50); g.setColor(128, 128, 128); g.fillRect(0, 0, getWidth(), getHeight()); g.setStrokeStyle(Graphics.SOLID); g.setColor(255, 0, 0); g.drawLine(0, 0, getWidth(), getHeight()); g.drawLine(0, getHeight(), getWidth(), 0); g.setColor (0,0, 0); g.drawString ("Hello World", 0, 0, Graphics.TOP|Graphics.HCENTER); } w/o translate() withtranslate()
Images Pictures, Photos, Drawings, … Andreas Jakl, 2009 29
Images Main image format for JavaME (must be supported): .png (Portable Network Graphics) Similar to .gif (which was patented until 2003 because of its LZW compression) Features: Compression: works well for graphics, not so well for photos Transparency: support depends on device – 1 bit transparency or full alpha-channel. Query:int levels = [Display].numAlphaLevels() Usually used for phones:8 bit colour depth (file size), 1 bit transparency (compatibility) Andreas Jakl, 2009 30
Save PNGs – File Size Reduction Optimized export – Photoshop: Save for Web Further optimization – Pngcrush:http://pmt.sourceforge.net/pngcrush/ Andreas Jakl, 2009 31 Use as few colors as possible (fine gradients compress worse) No dithering (compression gets more difficult) Transparenter Kanal kann gesetzt werden You can set a transparency channel
PNG Optimization – Example: 472 x 472 px  Andreas Jakl, 2009 32 256 colours, no dither 30,0 kB 64 colours, no dither 16,3 kB 8 colours, no dither 6,3 kB Results: - Not much difference between 256 and 64 colours (especially on a device display), but only half of the file size - Limits of optimization: 8 colours not enough - Dithering at 8 colours: same file size as visually better 64 colours image. Often, dithering is problematic! 8 colours, Diffusion dither 15,9 kB
Images Image-class saves picture, independent of the display Variants: Immutable: not modifiable, e.g. when loaded from files, resource bundles or a network.Examples: logos, sprites, … Mutable: modifiable, created by the applicationExamples: off-screen bitmap for double buffering, dynamically modifiable images, images created by the app at runtime for MIDlet file size reasons Andreas Jakl, 2009 33
Immutable Images and MIDlets Image file has to be in .jar archive Often, IDEs copy all files of project folders to .jar  don’t leave backups or test data in there! Andreas Jakl, 2009 34
Image – Example Add an image to the NetBeans-project Create resource folder in the project folder (e.g. /res/) Add folder to the NetBeans-project “Resources” Resource-folder should be included in the packaging process ( project properties) Andreas Jakl, 2009 35 3 1 2
Image – Example Load and display the image Andreas Jakl, 2009 36 // Load image as an instance variable // Warning: can take a long time for multiple / large images // -> in that case: asynchronous loading in an own thread! Image titleImg = Image.createImage(“/title.png”);…protected void paint (Graphics g) {g.drawImage(titleImg, 0, 0, Graphics.TOP|Graphics.LEFT);}
Mutable Images Created dynamically during runtime Andreas Jakl, 2009 37 classImageDemoCanvasextendsCanvas { privateImagemutableImg; publicImageDemoCanvas ()      { mutableImg= Image.createImage (10,10); mutableImg.getGraphics().fillArc (0,0,10,10,0,360);     } }
Conversion Mutable   Immutable Mutable  Immutable: Immutable  Mutable: Andreas Jakl, 2009 38 immutableImg = Image.createImage(mutableImg); Image immutableImg; // theimagetobecopiedimmutableImg = Image.createImage(...); 	// Create theimmutableimagefrom e.g. a file // Create a mutableimagewiththe same dimensionsImage mutableImg = Image.createImage(immutableImg.getWidth(), immutableImg.getHeight()); // GetthegraphicscontextofthenewmutableimageGraphics g = copy.getGraphics(); // Copytheimmutableimagetothemutableimage (bydrawingit)g.drawImage(immutableImg, 0, 0, TOP|LEFT);
Thanks for your attention That’s it! Andreas Jakl, 2009 39

Mais conteúdo relacionado

Semelhante a Java ME - 03 - Low Level Graphics E

Java ME - 02 - High Level UI
Java ME - 02 - High Level UIJava ME - 02 - High Level UI
Java ME - 02 - High Level UIAndreas Jakl
 
Java ME - 08 - Mobile 3D Graphics
Java ME - 08 - Mobile 3D GraphicsJava ME - 08 - Mobile 3D Graphics
Java ME - 08 - Mobile 3D GraphicsAndreas Jakl
 
Symbian OS - Quick Start
Symbian OS - Quick StartSymbian OS - Quick Start
Symbian OS - Quick StartAndreas Jakl
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Symbian OS - Memory Management
Symbian OS - Memory ManagementSymbian OS - Memory Management
Symbian OS - Memory ManagementAndreas Jakl
 
Om Pawar MP AJP.docx
Om Pawar MP AJP.docxOm Pawar MP AJP.docx
Om Pawar MP AJP.docxOmpawar61
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 
Beginning direct3d gameprogramming01_20161102_jintaeks
Beginning direct3d gameprogramming01_20161102_jintaeksBeginning direct3d gameprogramming01_20161102_jintaeks
Beginning direct3d gameprogramming01_20161102_jintaeksJinTaek Seo
 
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
 
Java ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and ThreadsJava ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and ThreadsAndreas Jakl
 
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.pdffeelinggifts
 
2010 bb dev con
2010 bb dev con 2010 bb dev con
2010 bb dev con Eing Ong
 
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 APITomi Aarnio
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTPayal Dungarwal
 
Android Wear Essentials
Android Wear EssentialsAndroid Wear Essentials
Android Wear EssentialsNilhcem
 
The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189Mahmoud Samir Fayed
 

Semelhante a Java ME - 03 - Low Level Graphics E (20)

Java ME - 02 - High Level UI
Java ME - 02 - High Level UIJava ME - 02 - High Level UI
Java ME - 02 - High Level UI
 
Java ME - 08 - Mobile 3D Graphics
Java ME - 08 - Mobile 3D GraphicsJava ME - 08 - Mobile 3D Graphics
Java ME - 08 - Mobile 3D Graphics
 
Symbian OS - Quick Start
Symbian OS - Quick StartSymbian OS - Quick Start
Symbian OS - Quick Start
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Symbian OS - Memory Management
Symbian OS - Memory ManagementSymbian OS - Memory Management
Symbian OS - Memory Management
 
Om Pawar MP AJP.docx
Om Pawar MP AJP.docxOm Pawar MP AJP.docx
Om Pawar MP AJP.docx
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Beginning direct3d gameprogramming01_20161102_jintaeks
Beginning direct3d gameprogramming01_20161102_jintaeksBeginning direct3d gameprogramming01_20161102_jintaeks
Beginning direct3d gameprogramming01_20161102_jintaeks
 
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)
 
GNURAdioDoc-8
GNURAdioDoc-8GNURAdioDoc-8
GNURAdioDoc-8
 
GNURAdioDoc-8
GNURAdioDoc-8GNURAdioDoc-8
GNURAdioDoc-8
 
Java ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and ThreadsJava ME - 04 - Timer, Tasks and Threads
Java ME - 04 - Timer, Tasks and Threads
 
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
 
Image j advanced
Image j   advancedImage j   advanced
Image j advanced
 
2010 bb dev con
2010 bb dev con 2010 bb dev con
2010 bb dev con
 
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
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
 
Android Wear Essentials
Android Wear EssentialsAndroid Wear Essentials
Android Wear Essentials
 
21 -windows
21  -windows21  -windows
21 -windows
 
The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189
 

Mais de Andreas Jakl

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityAndreas Jakl
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAndreas Jakl
 
Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndreas Jakl
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndreas Jakl
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndreas Jakl
 
Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Andreas Jakl
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web TechnologiesAndreas Jakl
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreAndreas Jakl
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Andreas Jakl
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test AutomationAndreas Jakl
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Andreas Jakl
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneAndreas Jakl
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingAndreas Jakl
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartAndreas Jakl
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosAndreas Jakl
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentAndreas Jakl
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)Andreas Jakl
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt CommunicationAndreas Jakl
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and GraphicsAndreas Jakl
 

Mais de Andreas Jakl (20)

Create Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented RealityCreate Engaging Healthcare Experiences with Augmented Reality
Create Engaging Healthcare Experiences with Augmented Reality
 
AR / VR Interaction Development with Unity
AR / VR Interaction Development with UnityAR / VR Interaction Development with Unity
AR / VR Interaction Development with Unity
 
Android Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App ManagementAndroid Development with Kotlin, Part 3 - Code and App Management
Android Development with Kotlin, Part 3 - Code and App Management
 
Android Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSONAndroid Development with Kotlin, Part 2 - Internet Services and JSON
Android Development with Kotlin, Part 2 - Internet Services and JSON
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
 
Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)Android and NFC / NDEF (with Kotlin)
Android and NFC / NDEF (with Kotlin)
 
Basics of Web Technologies
Basics of Web TechnologiesBasics of Web Technologies
Basics of Web Technologies
 
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & MoreBluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
Bluetooth Beacons - Bluetooth 5, iBeacon, Eddystone, Arduino, Windows 10 & More
 
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
Which new scenarios are enabled by Windows 10 for NFC, Bluetooth LE & Beacons?
 
Mobile Test Automation
Mobile Test AutomationMobile Test Automation
Mobile Test Automation
 
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
Qt App Development - Cross-Platform Development for Android, iOS, Windows Pho...
 
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows PhoneWinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
WinJS, Apache Cordova & NFC - HTML5 apps for Android and Windows Phone
 
Nokia New Asha Platform Developer Training
Nokia New Asha Platform Developer TrainingNokia New Asha Platform Developer Training
Nokia New Asha Platform Developer Training
 
Windows Phone 8 NFC Quickstart
Windows Phone 8 NFC QuickstartWindows Phone 8 NFC Quickstart
Windows Phone 8 NFC Quickstart
 
Windows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App ScenariosWindows (Phone) 8 NFC App Scenarios
Windows (Phone) 8 NFC App Scenarios
 
Windows 8 Platform NFC Development
Windows 8 Platform NFC DevelopmentWindows 8 Platform NFC Development
Windows 8 Platform NFC Development
 
NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)NFC Development with Qt - v2.2.0 (5. November 2012)
NFC Development with Qt - v2.2.0 (5. November 2012)
 
06 - Qt Communication
06 - Qt Communication06 - Qt Communication
06 - Qt Communication
 
05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics05 - Qt External Interaction and Graphics
05 - Qt External Interaction and Graphics
 
04 - Qt Data
04 - Qt Data04 - Qt Data
04 - Qt Data
 

Último

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 

Último (20)

DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 

Java ME - 03 - Low Level Graphics E

  • 1. Java™Platform, Micro Edition Part 3 – Low Level Graphics v3.0b – 02 April 2009 1 Andreas Jakl, 2009
  • 2. Disclaimer These slides are provided free of charge at http://www.symbianresources.com and are used during Java ME courses at the University of Applied Sciences in Hagenberg, Austria at the Mobile Computing department ( http://www.fh-ooe.at/mc ) Respecting the copyright laws, you are allowed to use them: for your own, personal, non-commercial use in the academic environment In all other cases (e.g. for commercial training), please contact andreas.jakl@fh-hagenberg.at The correctness of the contents of these materials cannot be guaranteed. Andreas Jakl is not liable for incorrect information or damage that may arise from using the materials. This document contains copyright materials which are proprietary to Sun or various mobile device manufacturers, including Nokia, SonyEricsson and Motorola. Sun, Sun Microsystems, the Sun Logo and the Java™ Platform, Micro Edition are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Andreas Jakl, 2009 2
  • 3. Contents Low Level Graphics Canvas Key Events Geometry & Text Images Andreas Jakl, 2009 3
  • 4. Hierarchy of Displayables Andreas Jakl, 2009 Display One Display instance / MIDlet Available in all sub-classes of Displayable Command Displayable Methods for drawing to a canvas Canvas Screen Ticker Graphics TextBox Form List Alert Item Spacer CustomItem Choice (Interface) ChoiceGroup StringItem DateField ImageItem TextField Gauge 4
  • 5. Canvas Canvas = base class Derive your own class from Canvas Canvas assigned to the screen like high level UI  can be mixed with high level UI! Functionality: Key events (key codes) Pointer events Paint (must be implemented) Visible / invisible Query properties Andreas Jakl, 2009 5
  • 6. Example: Canvas – Quick Overview Andreas Jakl, 2009 6 public class RealReplayMIDletextendsMIDlet { private booleaniFirstStart = true; private MenuCanvasiMainCanvas; publicRealReplayMIDlet() { } protected void startApp() { // Startapp also gets called when the midlet gains foreground again // -> Only initialize everything in the very first start-up. if (iFirstStart) { iMainCanvas = newMenuCanvas(this); // Create the main canvas Display.getDisplay(this).setCurrent(iMainCanvas); // Activate the canvas iFirstStart = false; } } publicvoidpauseApp() { } publicvoiddestroyApp(boolean unconditional) { } public void exit() { destroyApp(true); notifyDestroyed(); } }
  • 7. Example: Canvas – Quick Overview Andreas Jakl, 2009 7 public class MenuCanvasextends Canvas { privateRealReplayMIDletiMidlet; private intiWidth; private intiHeight; publicMenuCanvas(RealReplayMIDletaMidlet) { iMidlet = aMidlet; // Needed to quit the game setFullScreenMode(true); // Activate full screen mode iWidth = getWidth(); // Query screen size iHeight = getHeight(); } public void paint(Graphics aGraphics) { // Draw background aGraphics.setColor(255, 255, 255); aGraphics.fillRect(0, 0, iWidth, iHeight); // [...] } protected void keyPressed(intaKeyCode) { // Process key events [...] repaint(); } }
  • 8. The Life of a Canvas Andreas Jakl, 2009 8 Canvasconstructor display.setCurrent(myCanvas) showNotify() paint() … events … keyPressed() pointerPressed() display.setCurrent(myForm) hideNotify()
  • 9. Canvas – Overview Andreas Jakl, 2009 9 getWidth() (0, 0) setFullScreenMode(true) drawImage() drawString() getHeight() fillRoundRect() drawRoundRect() fillRect()
  • 10. Canvas – Full Screen setFullScreenMode(false); setFullScreenMode(true); Andreas Jakl, 2009 10
  • 11. Canvas – Show and Hide showNotify() Is called directly before Canvas becomes visible For initialization, e.g. (re)starting a timer hideNotify() Called after becoming invisible Free resources, cancel timers and threads, ... Also called by the AMS when system screens (e.g., incoming call) are displayed on top Query visibility: Displayable.isShown() Andreas Jakl, 2009 11
  • 12. Canvas – Events Events sent to (visible) Canvas Calls methods defined in Canvas base class Key- & pointer-events, commands Serial event delivery No second event is sent before first event is handled (= you left the event handling method) Exception: repaint enforced by you Andreas Jakl, 2009 12
  • 13. Key Events keyPressed(intkeyCode) When a key is pressed down keyRepeated(intkeyCode) Called again and again when the key is held down Frequency depends on mobile phone  better: use an own timer until … keyReleased(intkeyCode) When the key is released Andreas Jakl, 2009 13 keyPressed() keyRepeated() keyReleased()
  • 14. Key Codes protectedvoidkeyPressed(intkeycode) Constantsaredefinedforstandardkeys Andreas Jakl, 2009 14 Defined in the Canvasclass
  • 15. Key Codes – Game Actions Phones: no standardized key layout Therefore: game actions – appropriate keys for each phone intgetGameAction (intkeyCode) Andreas Jakl, 2009 15 Defined in the Canvasclass
  • 16. Key Codes – Example Andreas Jakl, 2009 16 protected void keyPressed(intkeycode) { SimplePlayerGUIgui = getGUI(); switch (keycode) { // This variant does not allow handling multiple simultaneous keypresses case Canvas.KEY_NUM1: // (which are not supported by many phones) // Jump backward gui.skip(true); break; caseCanvas.KEY_NUM2: gui.togglePlayer(); break; // [...] caseCanvas.KEY_POUND: gui.changeVolume(false); break; default: // Handle both keys and game actions – keys first, as they might be game actions as well intgameAction = getGameAction(keycode); if (gameAction == Canvas.RIGHT) { // Jump forward gui.skip(false); // [...] } elseif (gameAction == Canvas.FIRE) { gui.togglePlayer(); } } Adapted from the Sun WTK 2.5.2. MobileMediaAPI-examle
  • 17. Canvas – Soft Keys, … Problem: MIDP 2.0 does not define keycode constants for softkeys, delete, ...  Different keycodesfor every manufacturer Solutions: Use Commands (problematic: high level menu doesn’t fit to your design!) Compile an own version for every manufacturer Use generic ‘translation’-method – within certain limits Example – left softkeykeycode: -6 (Nokia), -1 (Siemens), -21 (Motorola), 21 (Motorola) Andreas Jakl, 2009 17
  • 18. Canvas – Paint If repaint is necessary (e.g. info dialog was on top of MIDlet) Framework calls Canvas.paint() Request repaint yourself Call: repaint() Optional: specify region paint()will be called automatically andasynchronously, at a “fitting” moment Double Buffering Usually supported and activated Query: booleandblBuf = canvas.isDoubleBuffered(); If not: create off-screen bitmap yourself Andreas Jakl, 2009 18 void keyPressed(int aKeyCode) { // Process key events [...] repaint(); } paint() not called instantly; usually through an extra thread void paint(Graphics g) { // Draw... }
  • 19. Graphics Drawing tothe Screen Andreas Jakl, 2009 19
  • 20. Graphics Graphics-object sent to paint() as a parameter Provides methods for drawing of: Line (SOLID or DOTTED) Rectangle (also filled and/or with rounded corners) Arc (part of an ellipse, also filled) Triangle (also filled) Text Images Andreas Jakl, 2009 20 void paint(Graphics g) { // Draw... }
  • 21. Colours Colour is used for all drawing operations that follow (including text) g.setColor(int red, int green, int blue); Usually no 32 bit display: Automatic assignment of nearest displayable colour no colour fidelity Andreas Jakl, 2009 21 void paint(Graphics g) { g.setColor(255, 255, 255); // White g.fillRect(0, 0, getWidth(), getHeight()); // Fill the whole background g.setColor(255, 0, 0); // Red g.drawLine(5, 5, 250, 250); // Centered text g.drawString("Hello", getWidth() / 2, getHeight() / 2, Graphics.HCENTER|Graphics.BASELINE); }
  • 22. Text Definepositionusinganchorpoints Images: similar, but useVCenterinsteadofBaseline Andreas Jakl, 2009 22 Left HCenter Right Top Mopius Baseline Bottom
  • 23. Example: g.drawString(“Mopius”, 0, 0, Graphics.HCENTER|Graphics.BASELINE); Canvas Text – Positioning Andreas Jakl, 2009 23 Mopius g.drawString(“Mopius”, 0, 0, Graphics.LEFT|Graphics.TOP); Mopius
  • 24. Recap: OR Andreas Jakl, 2009 24 OR executed for corresponding bits of two values Overview: OR Both values that were combined withor can be retrieved later on Baseline is contained in the combined value!
  • 25. Text – Font Choose through Font-object, then assign to Graphics-obj. Request font based on criteria: Face: monospace, proportional, system Style: plain, bold, italic, underlined Size:large, medium, small Example: Font titleFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD|Font.STYLE_ITALIC, Font.SIZE_LARGE);g.setFont(titleFont);g.drawString(“Test”, 0, 0, Graphics.TOP|Graphics.LEFT); Andreas Jakl, 2009 25
  • 26. Text – Font Sun WTK Nokia Series 40 (Emulator) Andreas Jakl, 2009 26 … mostgamesusetheirownbitmapfonts!
  • 27. Geometry Definesettings g.setStrokeStyle(Graphics.DOTTED); // orGraphics.SOLID Example: protectedvoidpaint (Graphics g) {// Draw backgroundg.setColor(255, 255, 255);g.fillRect(0, 0, getWidth(), getHeight());// Draw strokeg.setStrokeStyle(Graphics.DOTTED);g.setColor(255, 0, 0);g.drawLine(0, 0, getWidth(), getHeight());} Andreas Jakl, 2009 27
  • 28. Coordinate System Default: Top Left = 0 / 0 Visible areacanbemovedthroughtranslate() Andreas Jakl, 2009 28 protectedvoidpaint (Graphics g) { //g.translate(50, 50); g.setColor(128, 128, 128); g.fillRect(0, 0, getWidth(), getHeight()); g.setStrokeStyle(Graphics.SOLID); g.setColor(255, 0, 0); g.drawLine(0, 0, getWidth(), getHeight()); g.drawLine(0, getHeight(), getWidth(), 0); g.setColor (0,0, 0); g.drawString ("Hello World", 0, 0, Graphics.TOP|Graphics.HCENTER); } w/o translate() withtranslate()
  • 29. Images Pictures, Photos, Drawings, … Andreas Jakl, 2009 29
  • 30. Images Main image format for JavaME (must be supported): .png (Portable Network Graphics) Similar to .gif (which was patented until 2003 because of its LZW compression) Features: Compression: works well for graphics, not so well for photos Transparency: support depends on device – 1 bit transparency or full alpha-channel. Query:int levels = [Display].numAlphaLevels() Usually used for phones:8 bit colour depth (file size), 1 bit transparency (compatibility) Andreas Jakl, 2009 30
  • 31. Save PNGs – File Size Reduction Optimized export – Photoshop: Save for Web Further optimization – Pngcrush:http://pmt.sourceforge.net/pngcrush/ Andreas Jakl, 2009 31 Use as few colors as possible (fine gradients compress worse) No dithering (compression gets more difficult) Transparenter Kanal kann gesetzt werden You can set a transparency channel
  • 32. PNG Optimization – Example: 472 x 472 px Andreas Jakl, 2009 32 256 colours, no dither 30,0 kB 64 colours, no dither 16,3 kB 8 colours, no dither 6,3 kB Results: - Not much difference between 256 and 64 colours (especially on a device display), but only half of the file size - Limits of optimization: 8 colours not enough - Dithering at 8 colours: same file size as visually better 64 colours image. Often, dithering is problematic! 8 colours, Diffusion dither 15,9 kB
  • 33. Images Image-class saves picture, independent of the display Variants: Immutable: not modifiable, e.g. when loaded from files, resource bundles or a network.Examples: logos, sprites, … Mutable: modifiable, created by the applicationExamples: off-screen bitmap for double buffering, dynamically modifiable images, images created by the app at runtime for MIDlet file size reasons Andreas Jakl, 2009 33
  • 34. Immutable Images and MIDlets Image file has to be in .jar archive Often, IDEs copy all files of project folders to .jar  don’t leave backups or test data in there! Andreas Jakl, 2009 34
  • 35. Image – Example Add an image to the NetBeans-project Create resource folder in the project folder (e.g. /res/) Add folder to the NetBeans-project “Resources” Resource-folder should be included in the packaging process ( project properties) Andreas Jakl, 2009 35 3 1 2
  • 36. Image – Example Load and display the image Andreas Jakl, 2009 36 // Load image as an instance variable // Warning: can take a long time for multiple / large images // -> in that case: asynchronous loading in an own thread! Image titleImg = Image.createImage(“/title.png”);…protected void paint (Graphics g) {g.drawImage(titleImg, 0, 0, Graphics.TOP|Graphics.LEFT);}
  • 37. Mutable Images Created dynamically during runtime Andreas Jakl, 2009 37 classImageDemoCanvasextendsCanvas { privateImagemutableImg; publicImageDemoCanvas () { mutableImg= Image.createImage (10,10); mutableImg.getGraphics().fillArc (0,0,10,10,0,360); } }
  • 38. Conversion Mutable  Immutable Mutable  Immutable: Immutable  Mutable: Andreas Jakl, 2009 38 immutableImg = Image.createImage(mutableImg); Image immutableImg; // theimagetobecopiedimmutableImg = Image.createImage(...); // Create theimmutableimagefrom e.g. a file // Create a mutableimagewiththe same dimensionsImage mutableImg = Image.createImage(immutableImg.getWidth(), immutableImg.getHeight()); // GetthegraphicscontextofthenewmutableimageGraphics g = copy.getGraphics(); // Copytheimmutableimagetothemutableimage (bydrawingit)g.drawImage(immutableImg, 0, 0, TOP|LEFT);
  • 39. Thanks for your attention That’s it! Andreas Jakl, 2009 39