demonstrate the functionality supported by javax.microedition.lcdui package. : Audio Media « J2ME « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » J2ME » Audio Media 




demonstrate the functionality supported by javax.microedition.lcdui package.


import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/*

MIDlet to demonstrate the functionality supported by javax.microedition.lcdui package.

Make changes inside the  constructor (SimpleMIDlet() method) and inside the startApp()
method to see all the different functionality in this package.

*** High-level APIs
    ---------------

1. testForm() method demonstrates a variety of formElements which are individually 
   instantiated from inside the addItemsToForm() method

2. testAlert() method throws up an Alert on top of an existing form when the SCREEN 
   button on the form is clicked

3. testList() and testBox() methods demonstrate the use of IMPLICIT List and TextBox 
   respectively

*** Low-level APIs
    --------------

testCanvas()method demonstrates the use of low-level APIs

*/

public class SimpleMIDlet extends MIDlet implements CommandListener, ItemStateListener 
{
    Form simpleForm;
    List simpleList;
    TextBox simpleTextBox;
    Alert simpleAlert;
    SimpleCanvas simpleCanvas;
    Ticker t;
    Command c1 = new Command("Back", Command.BACK, 1);
    Command c2 = new Command("Screen", Command.SCREEN, 1);
    Command c3 = new Command("OK", Command.OK, 1);

    public SimpleMIDlet (){
  // High-level API examples
  
  // 1. Form and form elements: 
  //        a. Uncomment corresponding line in startApp() also
  //        b. Uncomment following line and comment out all other lines   
  testForm();
  
  // 2. Alert example; click on the SCREEN button to see the Alert
  //        a. Uncomment corresponding line in startApp() also
  //        b. Uncomment following line and comment out all other lines

  // testAlert();

  // 3. IMPLICIT List example
  //        a. Uncomment corresponding line in startApp() also
  //        b. Uncomment following line and comment out all other lines   

  // testList();

  // 4. TextBox example
  //        a. Uncomment corresponding line in startApp() also
  //        b. Uncomment following line and comment out all other lines   

  // testTextBox();
  
  // Low-level API examples
  //        a. Uncomment corresponding line in startApp() also
  //        b. Uncomment following line and comment out all other lines
  // testCanvas();
    }

    protected void destroyApp(boolean unconditional) {
    }


    protected  void pauseApp() {
    }

    private void testForm() {
  simpleForm  = new Form("Simple Form");

  // Create and add a new ticker to the Form
  t = new Ticker("Tick tock tick tock");
  simpleForm.setTicker(t);

  // Add a few Commands to the Form
  simpleForm.addCommand(c1);
  simpleForm.addCommand(c2);
  simpleForm.addCommand(c3);

  addItemsToForm();
    }

    private void addItemsToForm() {
  addChoiceGroup();
  addDateField();
  addGauge();
  addImageItem();
  addTextField();
    }

    private void addChoiceGroup() {
  Image icon = null;

  // Load image
  try {
      icon = Image.createImage("/midp/simpleMIDlet/Icon.png");
  }
  catch (java.io.IOException e) {
      System.out.println("Could not load image. Exception: " + e);
  }

  // Image choices
  Image[] imageArray = new Image[]{ icon, icon, icon };
  String[] stringArray = "Choice 1"
         "Choice 2"
         "Choice 3" };

  // Set up an exclusive choice group
  ChoiceGroup cGroup1 = new ChoiceGroup("Exclusive",
                ChoiceGroup.EXCLUSIVE,
                stringArray,
                imageArray);
  // Set up a multiple choice choice group
  ChoiceGroup cGroup2 = new ChoiceGroup("Multiple",
                ChoiceGroup.MULTIPLE,
                stringArray,
                imageArray);
  simpleForm.appendcGroup1 );
  simpleForm.appendcGroup2 );

  simpleForm.setItemStateListener(this);
    }
    
    public void itemStateChanged (Item item) {

  ifitem.getLabel().equals("Exclusive")) { 
      int choice = ((ChoiceGroup)item).getSelectedIndex();

      switch (choice) {
      case 0
    simpleForm.append("\nSelection: 0\n");
    break;
      case 1:
    simpleForm.append("\nSelection: 1\n");
    break;
      case 2:
    simpleForm.append("\nSelection: 2\n");
    break;
      }
  }
    }

    private void addDateField() {
  simpleForm.append(new DateField("Date", DateField.DATE));
        simpleForm.append(new DateField("Date & Time", DateField.DATE_TIME));
    }

    private void addGauge() {

  // Add an interactive gauge with the maximum value of 100 
  // and an initial value of 50 
  simpleForm.append(new Gauge("Interactive", true, 10050));
  
  // Add a  non-interactive gauge
  simpleForm.append(new Gauge("Non-interactive", false, 10050));
    }

    private void addImageItem() {
  Image image = null;
        try {
      image = Image.createImage("/midp/SimpleMIDlet/JavaPowered-8.png");
  }
        catch(java.io.IOException e) {
      System.out.println("Could not load image. Exception: " + e);
        }

  simpleForm.append(
        new ImageItem("Default layout",
          image,
          ImageItem.LAYOUT_LEFT + ImageItem.LAYOUT_NEWLINE_BEFORE,
          "Image not visible"));
  
    }
    
    private void addTextField() {
  simpleForm.append(new TextField("Any character"""15, TextField.ANY));
        simpleForm.append(new TextField("Email address"""20, TextField.EMAILADDR));
        simpleForm.append(new TextField("Numeric"""10, TextField.NUMERIC));
        simpleForm.append(new TextField("Phone"""12, TextField.PHONENUMBER));
        simpleForm.append(new TextField("Password"""6, TextField.PASSWORD));
        simpleForm.append(new TextField("URL"""30, TextField.URL));
  
    }

    private void testList() {
  String[] listElems = {
      "Element 1",
      "Element 2",
      "Element 3",
      "Element 4",
      "Element 5",
      "Element 6",
      "Element 7",
      "Element 8",
      "Element 9"
  };
  
  simpleList = new List("List", List.IMPLICIT, listElems, null);
    }

    private void testTextBox() {
  simpleTextBox = new TextBox("Textbox""4154"10, TextField.PASSWORD);
    }

    private void testAlert() {
  testForm();

  simpleAlert = new Alert("Alert");
        simpleAlert.setTimeout(Alert.FOREVER);
        simpleAlert.setString("This is a test Alert");
        simpleAlert.setType(AlertType.INFO);
    }

    private void testCanvas() {
  simpleCanvas = new SimpleCanvas();
    }

    public void commandAction(Command c, Displayable d) {
  if c.getCommandType() == Command.BACK ) {
      // Go back
  }
  if c.getCommandType() == Command.SCREEN ) {
      Display.getDisplay(this).setCurrent(simpleAlert, simpleForm);
  }
    }

    protected void startApp() {
  SimpleMIDlet simpleMIDlet = new SimpleMIDlet();
  

  // High-level API examples

  // 1. Uncomment the following two lines for Form and Alert examples and comment all others
        Display.getDisplay(this).setCurrent(simpleMIDlet.simpleForm);
  simpleMIDlet.simpleForm.setCommandListener(this);

  // 2. Uncomment following line for IMPLICIT List example and comment all others
  // Display.getDisplay(this).setCurrent(simpleMIDlet.simpleList);

  // 3. Uncomment following line for TextBox examples and comment all others
  // Display.getDisplay(this).setCurrent(simpleMIDlet.simpleTextBox);

  // Low-level API examples 

  // Uncomment following line and comment all others
  // Display.getDisplay(this).setCurrent(simpleMIDlet.simpleCanvas);
    }
}

class SimpleCanvas extends Canvas {
    
    int width, height;
    int widthShift, heightShift;
    String displayString;

    public SimpleCanvas( ){
  super();
  width = getWidth() 1;
  height = getHeight() 1;
  widthShift = 0;
  heightShift = 0;
  displayString = "Hello World";
    
 
    protected void paintGraphics g ){
        g.setColor0xffffff );
        g.fillRect00, width, height );
        g.setColor);
        g.drawStringdisplayString, width/+ widthShift, height/+ heightShift,
                       g.TOP | g.HCENTER );
    }

    public void keyPressedint keyCode) {

  int action = getGameAction(keyCode);

  switch (action) {
  case LEFT:
      widthShift -= 2;
      break;
  case RIGHT:
      widthShift += 2;
      break;
  case UP:
      heightShift -= 2;
      break;
  case DOWN:
      heightShift += 2;
      break;
  case FIRE:
      displayString = "Boom!";
      break;
  }
  repaint();
    }
}



           
       














Related examples in the same category
1.Audio MIDletAudio MIDlet
2.Piano MIDletPiano MIDlet
3.Media Information MIDletMedia Information MIDlet
4.Tone MIDletTone MIDlet
5.Sound AlertSound Alert
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.