Home of www.RoboHobby.com Java robotics project www.CubesAndCrosses.com
Train your logic skills in geometry
Create and delete one-color crosses
Play for free right in your browser!
Home-made CNC machine
Our new project -
How to make home-made CNC router
PICkit2 - Tutorials, Experiments
PICkit 2 - Small size USB programmer
from Microchip.
How to use it as a demo device for
USB programming in C language




www.RoboHobby.com
Home of www.RoboHobby.com
Java robotics project


Portable Notebook Table
How to make Portable Notebook Table.
Do it yourself!

Java SE SWING JSR-296 Example
How to run background task in
JSR-296 SWING application
with 'busy animation' in the status bar.
Useful addition to Netbeans template.

Java ME Code Examples
Java ME Source code examples
for robotics projects


Run Java ME Code Examples
How to run our Java ME code examples
for robotics projects



www.RoboHobby.com
/*
 * J2ME code example. Java ME Disc Utils Midlet
 * Part of www.RoboHobby.com Java robotics project.
 * www.RoboHobby.com
 */

package com.robohobby.me;

import java.io.UnsupportedEncodingException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;


/**
 * @Ads.Taglet
 * RobotMIDlet
 * @author Oleg Lyubchenko

 */
public class RobotMIDlet extends MIDlet implements CommandListener {

    public boolean midletPaused = false;

    CallBacks callBacks = CallBacks.getInstance();

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">                      

    private Command exitCommand;
    private Command writeToDiscCommand;
    private Command infoItemCommand;
    private Command itemCommand;
    private Command rootsCommand;
    private Command readRemoteCmdItemCommand;
    private Form form;
    private StringItem timeInfoStringItem;
    private StringItem stateStringItem;
    private StringItem rootsStringItem;
    //</editor-fold>                    

    /**
     * The RobotMIDlet constructor.
     */
    public RobotMIDlet() {
        callBacks.robotMIDlet = this;
        callBacks.discUtils = new DiscUtils();
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">                       

    //</editor-fold>                     

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">                                           
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started.
     * The method is called before the <code>startMIDlet</code> method.

     */
    private void initialize() {
        // write pre-initialize user code here

        // write post-initialize user code here
    }
    //</editor-fold>                          

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">                                        
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.

     */
    public void startMIDlet() {
        // write pre-action user code here
        switchDisplayable(null, getForm());
        // write post-action user code here

    }
    //</editor-fold>                           

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">                                         

    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.

     */
    public void resumeMIDlet() {
        // write pre-action user code here

        // write post-action user code here
    }
    //</editor-fold>                            

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">                                              
    /**
     * Switches a current displayable in a display.
     * The <code>display</code> instance is taken from <code>getDisplay</code> method.
     * This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; 
     * if <code>null</code>, then <code>nextDisplayable</code> is set immediately

     * @param nextDisplayable the Displayable to be set
     */

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        // write pre-switch user code here
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
        // write post-switch user code here

    }
    //</editor-fold>                                 

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">                                                 

    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.

     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked

     */
    public void commandAction(Command command, Displayable displayable) {
        // write pre-action user code here
        if (displayable == form) {
            if (command == exitCommand) {
                // write pre-action user code here

                exitMIDlet();
                // write post-action user code here
            } else if (command == readRemoteCmdItemCommand) {
                // write pre-action user code here

                // write post-action user code here
                readCommandFromDisc();
            } else if (command == rootsCommand) {
                // write pre-action user code here


                // write post-action user code here
                this.getRoots();
            } else if (command == writeToDiscCommand) {
                // write pre-action user code here

                // write post-action user code here
                writeToDisc();
            }
        }
        // write post-action user code here

    }
    //</editor-fold>                              



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">                                   

    /**
     * Returns an initiliazed instance of exitCommand component.

     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {
            // write pre-init user code here

            exitCommand = new Command("Exit", Command.EXIT, 0);
            // write post-init user code here
        }
        return exitCommand;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">                                   
    /**
     * Returns an initiliazed instance of form component.

     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {
            // write pre-init user code here

            form = new Form("www.RoboHobby.com",
            new Item[] { getRootsStringItem(), getTimeInfoStringItem(), getStateStringItem() });

            form.addCommand(getExitCommand());
            form.addCommand(getWriteToDiscCommand());
            form.addCommand(getRootsCommand());
            form.addCommand(getReadRemoteCmdItemCommand());
            form.setCommandListener(this);
            // write post-init user code here
        }
        return form;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: timeInfoStringItem ">                                   
    /**
     * Returns an initiliazed instance of timeInfoStringItem component.

     * @return the initialized component instance
     */
    public StringItem getTimeInfoStringItem() {
        if (timeInfoStringItem == null) {
            // write pre-init user code here

            timeInfoStringItem = new StringItem("Time: ", "", Item.PLAIN);
            // write post-init user code here
        }
        return timeInfoStringItem;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: writeToDiscCommand ">                                   
    /**
     * Returns an initiliazed instance of writeToDiscCommand component.

     * @return the initialized component instance
     */
    public Command getWriteToDiscCommand() {
        if (writeToDiscCommand == null) {
            // write pre-init user code here

            writeToDiscCommand = new Command("Write to Disc", Command.ITEM, 0);
            // write post-init user code here
        }
        return writeToDiscCommand;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">                                   
    /**
     * Returns an initiliazed instance of itemCommand component.

     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {
            // write pre-init user code here

            itemCommand = new Command("Item", Command.ITEM, 0);
            // write post-init user code here
        }
        return itemCommand;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: infoItemCommand ">                                   
    /**
     * Returns an initiliazed instance of infoItemCommand component.

     * @return the initialized component instance
     */
    public Command getInfoItemCommand() {
        if (infoItemCommand == null) {
            // write pre-init user code here

            infoItemCommand = new Command("State: ", Command.ITEM, 0);
            // write post-init user code here
        }
        return infoItemCommand;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stateStringItem ">                                   
    /**
     * Returns an initiliazed instance of stateStringItem component.

     * @return the initialized component instance
     */
    public StringItem getStateStringItem() {
        if (stateStringItem == null) {
            // write pre-init user code here

            stateStringItem = new StringItem("State: ", "");
            // write post-init user code here
        }
        return stateStringItem;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: rootsStringItem ">                                   
    /**
     * Returns an initiliazed instance of rootsStringItem component.

     * @return the initialized component instance
     */
    public StringItem getRootsStringItem() {
        if (rootsStringItem == null) {
            // write pre-init user code here

            rootsStringItem = new StringItem("Roots: ", "");
            // write post-init user code here
        }
        return rootsStringItem;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: rootsCommand ">                                   
    /**
     * Returns an initiliazed instance of rootsCommand component.

     * @return the initialized component instance
     */
    public Command getRootsCommand() {
        if (rootsCommand == null) {
            // write pre-init user code here

            rootsCommand = new Command("Get Roots", Command.ITEM, 0);
            // write post-init user code here
        }
        return rootsCommand;
    }
    //</editor-fold>                       

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: readRemoteCmdItemCommand ">                                   
    /**
     * Returns an initiliazed instance of readRemoteCmdItemCommand component.

     * @return the initialized component instance
     */
    public Command getReadRemoteCmdItemCommand() {
        if (readRemoteCmdItemCommand == null) {
            // write pre-init user code here

            readRemoteCmdItemCommand = new Command("Item", Command.ITEM, 0);
            // write post-init user code here
        }
        return readRemoteCmdItemCommand;
    }
    //</editor-fold>                       

    private void readCommandFromDisc()
    {
        if ( callBacks.discUtils.isDiskCommand() )
        {
            String cmd = callBacks.discUtils.readDiskCommand();
            this.stateStringItem.setText("cmd=" + cmd);
            callBacks.discUtils.deleteDiskCommand();
        }
    }

    private void getRoots()
    {
        callBacks.discUtils.getRoots();
    }

    /**

     * Returns a display instance.
     * @return the display instance.

     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.

     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    public void writeToDisc()
    {
        Calendar c = Calendar.getInstance();

        String nowStr =  StrCalendar.calendarToStrFs( c );

        System.out.println( "writeToDisc() nowStr="+nowStr );

        this.timeInfoStringItem.setText(nowStr);

        String testText = "testText";
        byte[] outBuf;

        try {

            outBuf = testText.getBytes("UTF-8");
            callBacks.discUtils.writeToDisk( nowStr+".txt", outBuf);

        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
            this.stateStringItem.setText( "Error e="+ex );
        }


    }

    /**

     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.

     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**

     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**

     * Called to signal the MIDlet to terminate.

     * @param unconditional if true, then the
     * MIDlet has to be unconditionally

     * terminated and all resources has to be released.

     */
    public void destroyApp(boolean unconditional) {
    }

}








Google
 



Contact us:


My Skype name is oleg_skp
Start Skype text chatStart Skype text chat
View Skype profileView Skype profile
Start Skype voice callStart Skype voice call
Add me to your Skype contact listAdd to Skype contact list
Leave voicemail using SkypeLeave voicemail using Skype
Send a file using SkypeSend a file using Skype
Note: To use these links
the Skype program should
be installed on your computer.
You can load it for free from
their official site:
Contact via Skypewww.Skype.com
------------------