Cosc 4755 Low-level GUI APIs. Low-Level API Composed of Canvas and Graphics classes – GameCanvas will be covered later on Canvas is an abstract class,

Slides:



Advertisements
Similar presentations
Applets and Graphics.
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
J2ME Form Nesneleri Melih Sakarya. Form Nesnesi Form form= new Form("Giris"); display.setCurrent(form);
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Cosc 5/4730 Blackberry Drawing. Screen size With Blackberry devices, they have a known screen size in pixels. If you are programming for specific device.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Simple Applet.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 12 Using the KToolbar Rob Pooley
J2ME: M3G/11 Intro to J2ME. Prog. v Objectives: –to introduce M3G; –to start talking about "M3G Chapter 2. An Animated Model" u leave the floor,
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 13 lcdui and OXO Rob Pooley
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Georgia Institute of Technology Movies part 2 Barb Ericson Georgia Institute of Technology April 2006.
CPSC1301 Computer Science 1 Chapter 14 Creating and Modifying Movies part 2.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
User Interface Summary Displayable ScreenCanvas FormAlertListTextbox Item DateFieldImageItemTextFieldStringItemGaugeChoiceGroup.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Features of Java CS 3331 Sections and 5.5.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
1 Introduction to J2ME Outline MIDP Building J2ME Apps- Tool J2ME Wireless Toolkit Demo MIDlet Programming -- MIDlet Transition States -- Midlet Skeleton.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Object Oriented Programming
Building Java Programs
Chapter 10 Graphics.
Kjava Programming.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Presentation transcript:

Cosc 4755 Low-level GUI APIs

Low-Level API Composed of Canvas and Graphics classes – GameCanvas will be covered later on Canvas is an abstract class, where you must write your paint(Graphics g) method. – There are numerous draw methods – But we are “rolling our own” for most everything else.

Canvas Canvas is a displayable object. So we extend a canvas class and implement the required abstract class paint(Graphics g). The simplest example is to draw something on the “screen”, known as the canvas

Simple Canvas example import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class LowLevel extends MIDlet{ private Display display; // The display for this MIDlet private myCanvas myCan; public LowLevel() { display = Display.getDisplay(this); myCan = new myCanvas(); } public void startApp() { display.setCurrent(myCan); //Canvas is a displayable object, like other screen objects myCan.repaint(); //force a redraw. } public void pauseApp() {} public void destroyApp(boolean unconditional) { } } class myCanvas extends Canvas { public void paint(Graphics g) { g.setColor(0,0,255); //set color to blue g.drawString("Hello World!",0,0,0); g.drawRect(getWidth()/2 -10, getHeight()/2 – 10, 20, 20); //draw square centered, with length of 20. }

Drawing Methods Graphics has the following methods – DrawArc Draws an elliptical arc – DrawChar Draws a character – DrawImage Draws a specified image – DrawLine Draws a line – DrawRect Draws an outline of rectangle – DrawRoundRec Draws a outline of rectangle with rounded corners – DrawString Draws a specified string of characters.

Drawing Methods (2) And set of “fill” draw methods. – fillArc, fillRect, fillRoundRect – Also a fillTraingle, which doesn’t have a draw method. Color – getColor() Returns an int RGB 0x00RRGGBB Or getRedComponent, Green, and Blue – SetColor(int red, int green, int blue) Or SetColor(int RGB) Fonts (Note there may only be one system font) – SetFont(Font font) – getFont() which returns a Font

Drawing Methods (3) From the canvas method, two very important methods: – getHeight() – getWidth() – You will need to know the pixel height and width of the “screen” you are working with. Every phone screen is likely to be a little different and your app will have to deal with it. Default emulators: ClamShell 170x220px, Msa 240x320px (one the examples pics are from).

Examples of drawing First for the rest of the example, assume the following class exists, declared as myColor Color = new myColor(); class myColor { //color in RGB format, so I don't have to remember them. int WHITE = 0x00FFFFFF; int BLACK = 0x ; int BLUE = 0x000000FF; int RED = 0x00FF0000; int GREEN = 0x0000FF00; int GRAY = 0x007F7F7F; }

Drawing Example 1 Extending the basic example before, class myCanvas extends Canvas { Image myImage; public myCanvas() { try { myImage = Image.createImage("/pic.jpg"); } catch (Exception e) { myImage = null;} } public void paint(Graphics g) { //clear the screen to white g.setColor(Color.WHITE); g.fillRect(0,0, getWidth(), getHeight()); //now draw some stuff g.setColor(Color.BLUE); g.drawString("Hello World!",0,0,0); g.setColor(Color.BLACK); g.drawRect(getWidth()/2 -10, getHeight()/2 – 10, 20, 20); g.setColor(Color.GREEN); g.fillRect(20, 20, 20, 20); g.setColor(Color.RED); g.fillRoundRect(15, 55, myImage.getWidth() +10, myImage.getHeight()+10, 10, 10); g.drawImage(myImage, 20, 60, 0); //put image over rounded rectangle g.setColor(Color.GRAY); g.drawString("Triangle", 100, 75, 0); g.fillTriangle(100, 100, 120, 100, 110, 110); //attempt a circle. g.setColor(Color.RED); g.drawString("Circle!", 100, 35, 0); g.fillArc(100, 50, 20, 20, 0, 360); }

Event key actions. There are three empty methods that can be subclassed to handle key events. – keyPressed(int KeyCode) – keyRepeated(int KeyCode) – keyReleased(int KeyCode) – And the following KeyCodes are defined as constants KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_STAR, and KEY_POUND.

Event Key actions (2) For “full keyboards”, with keycode to find out what the key pressed was – If (keycode >0) {char ch = (char)keyCode;} Using the getGameAction(int KeyCode) with can also use the following constants – UP, DOWN, LEFT, RIGHT, FIRE, GAME_A, GAME_B, GAME_C, and GAME_D. But you will have to figure out where the keys were map too.

Example 2 class myCanvas extends Canvas { myColor Color = new myColor(); int x, y; public myCanvas() { x = getWidth()/2; y = getHeight()/2; } public void paint(Graphics g) { //clear the screen to white g.setColor(Color.WHITE); g.fillRect(0,0, getWidth(), getHeight()); g.setColor(Color.RED); g.drawString("Press a key to move the ball",0,0,0); g.fillArc(x-5, y-5, 10, 10, 0, 360); } public void keyPressed(int keyCode){ // what game action does this key map to? int gameAction = getGameAction(keyCode); if(gameAction == RIGHT) { x += 5; } else if(gameAction == LEFT) {x -= 5; } else if(gameAction == UP) {y -= 5; } else if(gameAction == DOWN) {y += 5; } repaint(); // make sure to repaint }

Example 3 class myCanvas extends Canvas { myColor Color = new myColor(); char c; public myCanvas() { c = '?'; } public void paint(Graphics g) { //clear the screen to white g.setColor(Color.WHITE); g.fillRect(0,0, getWidth(), getHeight()); g.setColor(Color.RED); if (c == '?') { g.drawString("Press any key",0,0,0); } else { g.drawString("You Pressed:"+c,0,0,0); } public void keyPressed(int keyCode){ if (keyCode >0) { c = (char)keyCode; } else { c = '?'; } // make sure to repaint repaint(); } Only the number keys worked with this emulator

Adding Commands Commands can be added just like in MIDlets. This example, adds menus to change between the last 3 examples. – See website for complete code: LowLevel.zip private Command done, ex1, ex2, ex3; public LowLevel() { display = Display.getDisplay(this); myCan = new myCanvas(); done = new Command("Exit", Command.EXIT,0); ex1 = new Command("Example1", Command.ITEM, 1); ex2 = new Command("Example2", Command.ITEM, 1); ex3 = new Command("Example3", Command.ITEM, 1); myCan.addCommand(done); myCan.addCommand(ex1); myCan.addCommand(ex2); myCan.addCommand(ex3); myCan.setCommandListener(this); } … public void commandAction(Command c, Displayable s) { if (c == done) { destroyApp(false); notifyDestroyed(); } else if (c == ex1) { myCan.switchEx(1); } else if (c == ex2){ myCan.switchEx(2); } else if (c == ex3){ myCan.switchEx(3); }

Adding Commands (2) Note the different placement of the Menu items, between the 2 emulators.

Fonts and Anchor Points DrawString(“Text”, int x, int y, int anchor) – Drawing the text is based on “anchor points”, which are used to minimize the amount of computation required to place text. – Anchor points are defined by 1. horizontal (LEFT, HCENTER, RIGHT) 2. vertical (TOP, BASELINE, BOTTOM) – Using a bitwise OR TOP|LEFT which also results in zero.

Font placement Example Font f = g.getFont(); String str = "Hello World"; x = getWidth()/2; y =0; g.drawString(str,x, y, g.TOP |g.LEFT); y += f.getHeight(); g.drawString(str, x, y, g.TOP|g.HCENTER); y += f.getHeight(); g.drawString(str, x, y, g.TOP|g.RIGHT); y += f.getHeight(); //The next three should look the same g.drawString(str,x, y, g.TOP |g.LEFT); y += f.getHeight(); g.drawString(str, x + f.stringWidth(str)/2, y, g.TOP|g.HCENTER); y += f.getHeight(); g.drawString(str, x + f.stringWidth(str), y, g.TOP|g.RIGHT);

Changing the font In the graphics, there is a setfont method. – SetFont(Font f); – You can use Font.getFont to get the font you want to set. – There are a couple of constants for getFont Font.FACE_SYSTEM (system font) Font.FACE_MONOSPACE Font.FACE_PROPORTIONAL Size is either SMALL, MEDIUM, or LARGE

Changing the font (2) And last is the style – STYLE_PLAIN, STYLE_BOLD, STYLE_ITALIC, and STYLE_UNDERLINED – Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM) Returns the font, that is medium size, system, bold. NOTE: if you request a font that is not supported, it returns the standard system font.

Font Example g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE)); g.drawString("System Large Font", 0, 0, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM)); g.drawString("System Medium Font", 0, 15, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL)); g.drawString("System Small Font", 0, 30, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM)); g.drawString("Bold Style", 0, 45, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_MEDIUM)); g.drawString("Italic Style", 0, 60, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM)); g.drawString("Underlined Style", 0, 75, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM)); g.drawString("System Monospace", 0, 90, g.LEFT | g.TOP); g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM)); g.drawString("System Pproportional", 0, 105, g.LEFT | g.TOP); Note: System font Large, medium and small are the same, because small and large are not supported in this emulator.

Touch events Supported in the Canvas class First you can check to see if the phone has touchevents with – bool hasPointerEvents() – bool hasPointerMotionEvents() A Note, a big in emulators for SDK 3.0, These always come back true.

Touch events (2) three method are provided, there are empty by default. you will need to override the methods with your own code protected void pointerPressed(int x, int y) – Called when the touch/pointer is pressed. protected void pointerReleased(int x, int y) – Called when the touch/pointer is released. protected void pointerDragged(int x, int y) – called when each time the pointer is moved. so as you slide your finger across the screen, it called every time your finger moves.

Q A &