<strong>/*</strong>
<strong> * @(#)Sample.java 1.9 01/06/08</strong>
<strong> * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved.</strong>
<strong> */</strong>
<strong>import javax.microedition.midlet.*;</strong>
<strong>import javax.microedition.lcdui.*;</strong>
<strong>/*</strong>
<strong> * A quick sample of graphics, commands, and event handling.</strong>
<strong> */</strong>
<strong>public class SampleCanvasMIDlet extends MIDlet implements CommandListener {</strong>
<strong> Display display;</strong>
<strong> Command exitCommand;</strong>
<strong> Command backCommand;</strong>
<strong> Command okCommand;</strong>
<strong> SampleCanvas sample; // Instance of sample canvas</strong>
<strong>List itemMenu;</strong>
<strong> List exclusiveList;</strong>
<strong> List multipleList;</strong>
<strong> TextBox textbox;</strong>
<strong> Ticker ticker;</strong>
<strong> Alert alert;</strong>
<strong> Form form;</strong>
<strong> StringItem stringItem;</strong>
<strong> ImageItem imageItem;</strong>
<strong> Image image;</strong>
<strong> TextField textItem;</strong>
<strong> ChoiceGroup choiceItem;</strong>
<strong> DateField dateItem;</strong>
<strong> Gauge gaugeItem;</strong>
<strong>public SampleCanvasMIDlet() {</strong>
<strong> display = Display.getDisplay(this);</strong>
<strong> exitCommand = new Command(”Exit”, Command.EXIT, 1);</strong>
<strong> backCommand = new Command(”Back”, Command.BACK, 2);</strong>
<strong> okCommand = new Command(”OK”, Command.OK, 3);</strong>
<strong>ticker = new Ticker(”Select an item to display”);</strong>
<strong> itemMenu = new List(null, Choice.IMPLICIT);</strong>
<strong> itemMenu.append(”Canvas”, null);</strong>
<strong> itemMenu.append(”Form”, null);</strong>
<strong> itemMenu.append(”Alert”, null);</strong>
<strong> itemMenu.append(”TextBox”, null);</strong>
<strong> itemMenu.append(”Exclusive List”, null);</strong>
<strong> itemMenu.append(”Multiple Choice”, null);</strong>
<strong>itemMenu.setCommandListener(this);</strong>
<strong> itemMenu.addCommand(exitCommand);</strong>
<strong> itemMenu.setTicker(ticker);</strong>
<strong> display.setCurrent(itemMenu);</strong>
<strong> }</strong>
<strong>public void startApp () {</strong>
<strong> }</strong>
<strong>public void destroyApp (boolean unconditional) {</strong>
<strong> }</strong>
<strong>public void pauseApp () {</strong>
<strong> }</strong>
<strong>public void commandAction(Command c, Displayable s) {</strong>
<strong> if (c == backCommand) {</strong>
<strong> display.setCurrent(itemMenu);</strong>
<strong> } else if (s == itemMenu) {</strong>
<strong> if (c == List.SELECT_COMMAND) {</strong>
<strong> // Handle the item sected to be displayed</strong>
<strong> int i = itemMenu.getSelectedIndex();</strong>
<strong> switch (i) {</strong>
<strong> case 0: // Show Sample canvas</strong>
<strong> display.setCurrent(getCanvas());</strong>
<strong> break;</strong>
<strong> case 1: // Show the form</strong>
<strong> display.setCurrent(getForm());</strong>
<strong> break;</strong>
<strong> case 2: // Show an alert</strong>
<strong> display.setCurrent(getAlert(”Warning”, </strong>
<strong> “This window will dismiss in two seconds.”));</strong>
<strong> break;</strong>
<strong> case 3: // Show TextBox</strong>
<strong> display.setCurrent(getTextBox());</strong>
<strong> break;</strong>
<strong> case 4: // Show Exclusive list</strong>
<strong> display.setCurrent(getExclusiveList());</strong>
<strong> break;</strong>
<strong> case 5: // Show Multiple List</strong>
<strong> display.setCurrent(getMultipleList());</strong>
<strong> break;</strong>
<strong> }</strong>
<strong> } else if (c == exitCommand) {</strong>
<strong> notifyDestroyed();</strong>
<strong> }</strong>
<strong> } else if (s == exclusiveList) {</strong>
<strong> int i = exclusiveList.getSelectedIndex();</strong>
<strong> String value = exclusiveList.getString(i);</strong>
<strong> alert = getAlert(”Border selected:”, value);</strong>
<strong> display.setCurrent(alert, itemMenu);</strong>
<strong> } else if (s == multipleList) {</strong>
<strong> StringBuffer b = new StringBuffer();</strong>
<strong> for (int i = 0; i <= 2; i++) {</strong>
<strong> if (multipleList.isSelected(i)) {</strong>
<strong> b.append(multipleList.getString(i));</strong>
<strong> b.append(”n”);</strong>
<strong> }</strong>
<strong> }</strong>
<strong> alert = getAlert(”Colors selected:”, b.toString());</strong>
<strong> display.setCurrent(alert, itemMenu);</strong>
<strong> } else if (s == textbox) {</strong>
<strong> String value = textbox.getString();</strong>
<strong> alert = getAlert(”Text Entered:”, value);</strong>
<strong> display.setCurrent(alert, itemMenu);</strong>
<strong> } else if (s == form) {</strong>
<strong> alert = getAlert(”Image options saved”, “”);</strong>
<strong> display.setCurrent(alert, itemMenu);</strong>
<strong> }</strong>
<strong> }</strong>
<strong>SampleCanvas getCanvas() {</strong>
<strong> if (sample == null) {</strong>
<strong> sample = new SampleCanvas();</strong>
<strong> sample.addCommand(backCommand);</strong>
<strong> sample.setCommandListener(this);</strong>
<strong> }</strong>
<strong> return sample;</strong>
<strong> }</strong>
<strong>List getExclusiveList() {</strong>
<strong> if (exclusiveList == null) {</strong>
<strong> exclusiveList = new List(”Border Style”, Choice.EXCLUSIVE);</strong>
<strong> exclusiveList.append(”None”, null);</strong>
<strong> exclusiveList.append(”Plain”, null);</strong>
<strong> exclusiveList.append(”Fancy”, null);</strong>
<strong> exclusiveList.addCommand(backCommand);</strong>
<strong> exclusiveList.addCommand(okCommand);</strong>
<strong> exclusiveList.setCommandListener(this);</strong>
<strong> }</strong>
<strong> return exclusiveList;</strong>
<strong> }</strong>
<strong>List getMultipleList() {</strong>
<strong> if (multipleList == null) {</strong>
<strong> multipleList = new List(”Colors to mix”, Choice.MULTIPLE);</strong>
<strong> multipleList.append(”Red”, null);</strong>
<strong> multipleList.append(”Green”, null);</strong>
<strong> multipleList.append(”Blue”, null);</strong>
<strong> multipleList.addCommand(backCommand);</strong>
<strong> multipleList.addCommand(okCommand);</strong>
<strong> multipleList.setCommandListener(this);</strong>
<strong> }</strong>
<strong> return multipleList;</strong>
<strong> }</strong>
<strong>TextBox getTextBox() {</strong>
<strong> if (textbox == null) {</strong>
<strong> textbox = new TextBox(”Enter a phone number”,””, 40,</strong>
<strong> TextField.PHONENUMBER);</strong>
<strong> textbox.addCommand(backCommand);</strong>
<strong> textbox.addCommand(okCommand);</strong>
<strong> textbox.setCommandListener(this);</strong>
<strong> }</strong>
<strong> return textbox;</strong>
<strong> }</strong>
<strong>Alert getAlert(String title, String contents) {</strong>
<strong> if (alert == null) {</strong>
<strong> alert = new Alert(title);</strong>
<strong> alert.setType(AlertType.WARNING);</strong>
<strong> alert.setTimeout(2000);</strong>
<strong> alert.setString(contents); </strong>
<strong> } else {</strong>
<strong> alert.setTitle(title);</strong>
<strong> alert.setString(contents);</strong>
<strong> }</strong>
<strong> return alert;</strong>
<strong> }</strong>
<strong>Form getForm() {</strong>
<strong> if (form == null) {</strong>
<strong> form = new Form(”Options”);</strong>
<strong>try {</strong>
<strong> image = Image.createImage(”/images/PhotoAlbum.png”);</strong>
<strong> imageItem = new ImageItem(”Preview:”, image, </strong>
<strong> ImageItem.LAYOUT_NEWLINE_BEFORE, “Mountain”);</strong>
<strong> form.append(imageItem);</strong>
<strong> } catch (java.io.IOException ex) {</strong>
<strong> }</strong>
<strong>textItem = new TextField(”Title:”, “Mountain”, 32,</strong>
<strong> TextField.ANY);</strong>
<strong> form.append(textItem);</strong>
<strong>dateItem = new DateField(”Date:”, DateField.DATE);</strong>
<strong> dateItem.setDate(new java.util.Date());</strong>
<strong> form.append(dateItem);</strong>
<strong>choiceItem = new ChoiceGroup(”Size:”, Choice.EXCLUSIVE);</strong>
<strong> choiceItem.append(”Small”, null);</strong>
<strong> choiceItem.append(”Large”, null);</strong>
<strong> form.append(choiceItem);</strong>
<strong>gaugeItem = new Gauge(”Speed:”, true, 10, 5);</strong>
<strong> form.append(gaugeItem);</strong>
<strong>form.addCommand(backCommand);</strong>
<strong> form.addCommand(okCommand);</strong>
<strong> form.setCommandListener(this);</strong>
<strong> }</strong>
<strong> return form;</strong>
<strong> }</strong>
<strong>}</strong>
<strong>class SampleCanvas extends Canvas {</strong>
<strong> int x, y; // Location of cross hairs</strong>
<strong> String event = “”; // Last key event type</strong>
<strong> int keyCode; // Last keyCode pressed</strong>
<strong> Font font; // Font used for drawing text</strong>
<strong> int fh; // height of the font</strong>
<strong> int w, h; // width and height of the canvas</strong>
<strong> int titleHeight; // Height of the title</strong>
<strong> int pieSize; // Size of the Pie chart used for width and height</strong>
<strong> int barSize; // Size of the Bar chart used for width and height</strong>
<strong> int eventHeight; // Size of the event region</strong>
<strong> int pad; // Padding used between items</strong>
<strong>SampleCanvas() {</strong>
<strong> w = getWidth();</strong>
<strong> h = getHeight();</strong>
<strong> font = Font.getFont(Font.FACE_SYSTEM,</strong>
<strong> Font.STYLE_PLAIN, Font.SIZE_SMALL);</strong>
<strong> fh = font.getHeight();</strong>
<strong>/* Compute the sizes of the bar and pie charts</strong>
<strong> * It should use all the space except for the title</strong>
<strong> * and event regions.</strong>
<strong> * Don’t let the charts get too small</strong>
<strong> */</strong>
<strong> pad = 2;</strong>
<strong> titleHeight = fh + pad * 2;</strong>
<strong> eventHeight = fh * 3;</strong>
<strong> barSize = h – (titleHeight + pad) – (eventHeight + pad);</strong>
<strong> if (barSize < 20) // Don’t let them get too small</strong>
<strong> barSize = 20;</strong>
<strong> if (barSize > (w – pad) / 2) // Shrink to 1/2 width</strong>
<strong> barSize = (w – pad) / 2;</strong>
<strong> pieSize = barSize;</strong>
<strong> }</strong>
<strong>protected void keyPressed(int key) {</strong>
<strong> keyCode = key;</strong>
<strong> event = “Pressed”;</strong>
<strong> handleActions(key);</strong>
<strong> repaint();</strong>
<strong> }</strong>
<strong>protected void keyRepeated(int key) {</strong>
<strong> keyCode = key;</strong>
<strong> event = “Repeated”;</strong>
<strong> handleActions(key);</strong>
<strong> repaint();</strong>
<strong> }</strong>
<strong>protected void keyReleased(int key) {</strong>
<strong> keyCode = key;</strong>
<strong> event = “Released”;</strong>
<strong> repaint();</strong>
<strong> }</strong>
<strong>protected void pointerPressed(int x, int y) {</strong>
<strong> this.x = x;</strong>
<strong> this.y = y;</strong>
<strong> keyCode = 0;</strong>
<strong> event = “Pressed”;</strong>
<strong> repaint();</strong>
<strong> }</strong>
<strong> protected void pointerReleased(int x, int y) {</strong>
<strong> this.x = x;</strong>
<strong> this.y = y;</strong>
<strong> keyCode = 0;</strong>
<strong> event = “Released”;</strong>
<strong> repaint();</strong>
<strong> }</strong>
<strong>protected void pointerDragged(int x, int y) {</strong>
<strong> this.x = x;</strong>
<strong> this.y = y;</strong>
<strong> keyCode = 0;</strong>
<strong> event = “Dragged”;</strong>
<strong> }</strong>
<strong>void handleActions(int keyCode) {</strong>
<strong> int action = getGameAction(keyCode);</strong>
<strong> switch (action) {</strong>
<strong> case LEFT:</strong>
<strong> x -= 1;</strong>
<strong> break;</strong>
<strong> case RIGHT:</strong>
<strong> x += 1;</strong>
<strong> break;</strong>
<strong> case UP:</strong>
<strong> y -= 1;</strong>
<strong> break;</strong>
<strong> case DOWN:</strong>
<strong> y += 1;</strong>
<strong> break;</strong>
<strong> }</strong>
<strong> }</strong>
<strong>protected void paint(Graphics g) {</strong>
<strong>g.setFont(font);</strong>
<strong> g.setGrayScale(255);</strong>
<strong> g.fillRect(0, 0, w, h);</strong>
<strong>x = (x < 0) ? w – 1 : x;</strong>
<strong> y = (y < 0) ? h – 1 : y;</strong>
<strong> x = x % w;</strong>
<strong> y = y % h;</strong>
<strong>// Draw Fill and outline for background of title Text</strong>
<strong> int swidth = pad * 2 + font.stringWidth(”Pie and Bar Samples”);</strong>
<strong> int title_x = (w – swidth)/2;</strong>
<strong>g.setGrayScale(128);</strong>
<strong> g.fillRoundRect(title_x, 0, swidth, fh, 5, 5);</strong>
<strong> g.setGrayScale(0);</strong>
<strong> g.drawRoundRect(title_x, 0, swidth, fh, 5, 5);</strong>
<strong>// Sample Text</strong>
<strong> g.setColor(0, 0, 0);</strong>
<strong> g.drawString(”Pie and Bar Samples”, </strong>
<strong> title_x + pad, pad, Graphics.TOP|Graphics.LEFT);</strong>
<strong>// Translate to below title text</strong>
<strong> g.translate(0, titleHeight + pad);</strong>
<strong>/*</strong>
<strong> * Draw pie chart on the left side</strong>
<strong> * using the barSize for width and height</strong>
<strong> */</strong>
<strong> g.setColor(255, 0, 0);</strong>
<strong> g.fillArc(0, 0, pieSize, pieSize, 45, 270);</strong>
<strong> g.setColor(0, 255, 0);</strong>
<strong> g.fillArc(0, 0, pieSize, pieSize, 0, 45);</strong>
<strong> g.setColor(0, 0, 255);</strong>
<strong> g.fillArc(0, 0, pieSize, pieSize, 0, -45);</strong>
<strong> g.setColor(0);</strong>
<strong> g.drawArc(0, 0, pieSize, pieSize, 0, 360);</strong>
<strong>// Draw Bar chart on right side of the display</strong>
<strong> // scale the values to the pieSize maximum value</strong>
<strong> int yorig = barSize;</strong>
<strong> int h1 = barSize / 3, h2 = barSize / 2, h3 = barSize;</strong>
<strong> int avg = (h1 + h2 + h3) / 3;</strong>
<strong>// Move over to draw Bar chart</strong>
<strong> g.translate((w + pad) / 2, 0);</strong>
<strong>int bw = pieSize / 7;</strong>
<strong> if (bw < 2)</strong>
<strong> bw = 2;</strong>
<strong> g.setColor(255, 0, 0);</strong>
<strong> g.fillRect(bw*1, yorig-h1, bw+1, h1);</strong>
<strong> g.setColor(0, 255, 0);</strong>
<strong> g.fillRect(bw*3, yorig-h2, bw+1, h2);</strong>
<strong> g.setColor(0, 0, 255);</strong>
<strong> g.fillRect(bw*5, yorig-h3, bw+1, h3);</strong>
<strong> g.setColor(0);</strong>
<strong> g.drawRect(bw*1, yorig-h1, bw, h1);</strong>
<strong> g.drawRect(bw*3, yorig-h2, bw, h2);</strong>
<strong> g.drawRect(bw*5, yorig-h3, bw, h3);</strong>
<strong>// Draw axis for bar chart.</strong>
<strong> g.setGrayScale(0);</strong>
<strong> g.drawLine(0, 0, 0, yorig);</strong>
<strong> g.drawLine(0, yorig, barSize, yorig);</strong>
<strong> g.setStrokeStyle(Graphics.DOTTED);</strong>
<strong> g.drawLine(0, yorig – avg, barSize, yorig-avg);</strong>
<strong> g.setStrokeStyle(Graphics.SOLID);</strong>
<strong>// Restore to left and move down</strong>
<strong> g.translate(-(w + pad) / 2, pieSize + pad);</strong>
<strong>// Draw the key and pointer status</strong>
<strong> g.setColor(128, 128, 128);</strong>
<strong> int col1 = font.stringWidth(”Action:”);</strong>
<strong> g.drawString(”Key: “, col1, 0,</strong>
<strong> Graphics.TOP|Graphics.RIGHT);</strong>
<strong> g.drawString(keyString(keyCode), col1, 0,</strong>
<strong> Graphics.TOP|Graphics.LEFT);</strong>
<strong> g.drawString(”Action:”, col1, fh,</strong>
<strong> Graphics.TOP|Graphics.RIGHT);</strong>
<strong> g.drawString(actionString(keyCode), col1, fh,</strong>
<strong> Graphics.TOP|Graphics.LEFT);</strong>
<strong> g.drawString(”Event:”, col1, fh*2,</strong>
<strong> Graphics.TOP|Graphics.RIGHT);</strong>
<strong> g.drawString(event, col1, fh*2,</strong>
<strong> Graphics.TOP|Graphics.LEFT);</strong>
<strong> int col2 = 80;</strong>
<strong> g.drawString(”x:”, col2, 0,</strong>
<strong> Graphics.TOP|Graphics.RIGHT);</strong>
<strong> g.drawString(Integer.toString(x), col2, 0,</strong>
<strong> Graphics.TOP|Graphics.LEFT);</strong>
<strong> g.drawString(”y:”, col2, fh,</strong>
<strong> Graphics.TOP|Graphics.RIGHT);</strong>
<strong> g.drawString(Integer.toString(y), col2, fh,</strong>
<strong> Graphics.TOP|Graphics.LEFT);</strong>
<strong>// Restore the origin and draw the crosshairs on top</strong>
<strong> g.translate(-g.getTranslateX(), -g.getTranslateY());</strong>
<strong>g.setColor(0, 0, 0);</strong>
<strong> g.drawLine(x, y – 5, x, y + 5);</strong>
<strong> g.drawLine(x – 5, y, x + 5, y);</strong>
<strong> }</strong>
<strong>String keyString(int keyCode) {</strong>
<strong> if (keyCode == 0) {</strong>
<strong> return “”;</strong>
<strong> }</strong>
<strong> return Integer.toString(keyCode);</strong>
<strong> }</strong>
<strong>String actionString(int keyCode) {</strong>
<strong> if (keyCode == 0) {</strong>
<strong> return “”;</strong>
<strong> }</strong>
<strong>int action = getGameAction(keyCode);</strong>
<strong> switch (action) {</strong>
<strong> case FIRE:</strong>
<strong> return “Fire”;</strong>
<strong> case LEFT:</strong>
<strong> return “Left”;</strong>
<strong> case RIGHT:</strong>
<strong> return “Right”;</strong>
<strong> case DOWN:</strong>
<strong> return “Down”;</strong>
<strong> case UP:</strong>
<strong> return “Up”;</strong>
<strong> case GAME_A:</strong>
<strong> return “Game A”;</strong>
<strong> case GAME_B:</strong>
<strong> return “Game B”;</strong>
<strong> case GAME_C:</strong>
<strong> return “Game C”;</strong>
<strong> case GAME_D:</strong>
<strong> return “Game D”;</strong>
<strong> case 0:</strong>
<strong> return “”;</strong>
<strong> default:</strong>
<strong> return Integer.toString(action);</strong>
<strong> }</strong>
<strong> }</strong>
<strong>}</strong>
Tinggalkan komentar