Archyvas

Įrašo tag'ai: ‘programming’

Howto. Show slash screen in JavaME

Gruodis 29th, 2009 Ernestas Kardzys 4 komentarai

I was trying to create a splash screen in JavaME. And here it is:

package info.ernestas.guicomponents;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 * @author Ernestas Kardzys
 */

public class SplashScreen extends Canvas implements Runnable {
    private Display display;
    private Displayable next;
    private long splashTime = 0;
    private Thread thread;

    public SplashScreen(Display display, Displayable next) {
        this.display = display;
        this.next = next;

        this.display.setCurrent(this);
    }

    public SplashScreen(Display display, Displayable next, long splashTime) {
        this.display = display;
        this.next = next;
        this.splashTime = splashTime;

        this.display.setCurrent(this);
    }

    protected void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        // Set background color
        g.setColor(0×006000);

        g.fillRect(0, 0, width, height);

        Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
        g.setColor(0×555555);
        g.setFont(font);

        g.drawString("Hello, World!", width/2, height/2, 0);
    }

    // Closes the screen
    private void dismiss(){
        if (isShown())
          display.setCurrent(next);
    }

    public void showSplash() {
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void notifyDone() {
        notify();
    }

    public void keyReleased(int keyCode) {
        // Do something if a key was pressed or dismiss(); …
    }

    public void pointerReleased(int x, int y) {
       // … or with pointer – dismiss();
    }

   

    public synchronized void run() {
        try {
            // Splash time set – splash until time ends
            if (splashTime > 0) {
                Thread.sleep(splashTime);
            }
            else {
                // No time set – wait until notified
                wait();
            }

            dismiss();
        }
        catch (InterruptedException ex) {
            ex.printStackTrace();
            dismiss();
        }
    }

}

You can call it like that:

SplashScreen splashScreen = new SplashScreen(Display.getDisplay(this), new Form("New form!"), 2000);
splashScreen.showSplash(); // Show splash screen for 2 seconds (1 second = 1000 milliseconds)

Or if you prefer to manually close the form:

SplashScreen splashScreen = new SplashScreen(Display.getDisplay(this), new Form("New form!"));
splashScreen.showSplash();

// Some code here…
splashScreen.notifyDone();

Kategorijos:JavaME, Programavimas Raktažodžiai:, ,

My first JavaME program aka Hello, World on Mobile phone

Gruodis 26th, 2009 Ernestas Kardzys 2 komentarai

I wanted to try the JavaME framework. And I tried it.
The first program I made was a simple program which shows an alert box with the text you entered.
Interesting point is that the menu buttons look different then in my “Sony Ericsson K550i”. In the emulator – the “Exit” is on the right, but in my mobile phone it’s on the left. At the moment I don’t have any idea why, but… Well, we’ll see :)

package helloworld;

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

/**
 * @author Ernestas Kardzys
 */

public class Midlet extends MIDlet implements CommandListener {
    private Display display;
    private Form mainForm;
    private TextBox textBox;
    private Command quitCommand;
    private Command backCommand;
    private Command alertCommand;
    private Command textboxCommand;

    /**
      * Called by the system to start our MIDlet.
      */

    public void startApp() {
        display = Display.getDisplay(this);

        quitCommand = new Command("Exit", Command.EXIT, 1);
        backCommand = new Command("Back", Command.BACK, 1);
        alertCommand = new Command("Alert", Command.SCREEN, 2);
        textboxCommand = new Command("Show textbox", Command.SCREEN, 3);

        mainForm = new Form("Main form!");
        mainForm.addCommand(quitCommand);
        mainForm.addCommand(alertCommand);
        mainForm.addCommand(textboxCommand);
        mainForm.setCommandListener(this);

        textBox = new TextBox("SimpleTextBox", "", 100, 0);
        textBox.setCommandListener(this);
        textBox.addCommand(alertCommand);
        textBox.addCommand(backCommand);
        textBox.setCommandListener(this);

        display.setCurrent(mainForm);
    }

    /**
      * Called by the system to pause our MIDlet.
      */

    public void pauseApp() {
       
    }

    /**
      * Called by the system to end our MIDlet.
      */

    public void destroyApp(boolean unconditional) {

    }

    public void commandAction(Command choice, Displayable displayable) {
        if (choice == quitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
        else if (choice == backCommand) {
            display.setCurrent(mainForm);
        }
        else if (choice == textboxCommand) {
            display.setCurrent(textBox);
        }
        else if (choice == alertCommand) {
            if (textBox.getString().trim().length() > 0) {
                Alert alert = new Alert(textBox.getString());
                alert.setTimeout(5000);
                textBox.setString("");
                display.setCurrent(alert);
            }
        }
    }
}

The next message in the JavaME section will be about how to send the SMS by using your mobile phone and JavaME :)

Kategorijos:JavaME Raktažodžiai:, , , ,

Required software for development

Liepa 8th, 2009 Ernestas Kardzys 2 komentarai

I thought it might be interesting to post something about development for the Symbian OS. So, at start we need some software.
We’ll need two things: a development environment (IDE) & SDK for Symbian OS. Lets start with IDE.
Symbian OS has a wonderful page about development: http://developer.symbian.com/main/index.jsp. So, let’s go here and look for IDE.
Go here: http://www.forum.nokia.com/info/sw.nokia.com/id/dbb8841d-832c-43a6-be13-f78119a2b4cb.html and press Download Now button. Then you need to register and up you go – the package of 203 MB starts downloading. Install it.
Then you’ll need ActivePerl-5.6.1.635 package (version is very important). Try downloading from here: http://downloads.activestate.com/ActivePerl/Windows/5.6/ (I’m not sure if I downloaded it from here).
You’ll also need Java Runtime Environment 1.5 (if you don’t have it so far). Get it from: http://java.com/en/download/manual.jsp.
Now the most interesting part. You need the SDK for Symbian OS. There are different versions of SDK. At first I downloaded the latest version (Symbian OS SDK S60 5th Edition), but I couldn’t make it work in my PC. Then I download 3rd Edition – with this everything was ok. The SDK is installed and is running smoothly. One note: be patient. It takes some time to install this SDK… Because I have 3rd Edition of SDK, I can develop applications only for Symbian OS 9.3. Get SDK from: http://www.forum.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html
And final note – check http://developer.symbian.com/main/documentation/books/books_files/pdf/Getting_Started_final.pdf
Good luck ;)

C#. Int array vs. ArrayList vs. Dictionary

Birželis 7th, 2009 Ernestas Kardzys 2 komentarai

private static void TestIntArray()
{
int [] array = new int[1000];
for (int i = 0; i < 1000; i++)
array[i] = i;
}

private static void TestArrayList()
{
ArrayList array = new ArrayList();
for (int i = 0; i < 1000; i++)
array.Add(i);
}

// Code….

long start = DateTime.Now.Ticks;
TestIntArray();
long end = DateTime.Now.Ticks;
Console.WriteLine(“Time with int array: {0}”, end – start);

long start2 = DateTime.Now.Ticks;
TestArrayList();
long end2 = DateTime.Now.Ticks;
Console.WriteLine(“Time with ArrayList array: {0}”, end2 – start2);

The result:

Time with int array: 30000
Time with ArrayList array: 10000

So, working with ArrayList is faster. Hm…

If we have multidimensional array and Dictionary<int, int> the difference is even bigger:

private static void TestIntArray()
{
int [ , ] array = new int[1000, 1000];
int i = 0;
for (int j = 0; j < 1000; j++)
array[i , j] = i++;
}

private static void TestDictionary()
{
Dictionary<int, int> array = new Dictionary<int, int>();
int i = 0;
for (int j = 0; j < 1000; j++)
array.Add(j, i++);
}

Time with int array: 60000
Time with Dictionary: 10000

Interesting :)

XNA. Change resolution & set full screen

Balandis 28th, 2009 Ernestas Kardzys 4 komentarai

I am creating a game with the XNA framework and I needed to change the resolution for XNA game. So, that’s how it’s done:

public class MyCoolGame : Microsoft.Xna.Framework.Game

// Code…

// GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);

graphics.PreferredBackBufferWidth = Width;
graphics.PreferredBackBufferHeight = Height;
graphics.ApplyChanges();

Another very useful property is IsFullScreen – boolean variable, which sets game to full screen or not (do not forget to call graphics.ApplyChanges().).

Įrašų įterpimas į surikiuotą masyvą

Spalis 21st, 2008 Ernestas Kardzys 4 komentarai

private static void insertEntryIntoArray(int insertationIndex, int entry, int [] array) {
for (int i = array.length – 1; i > insertationIndex; i–)
array[i] = array[i - 1];

array[insertationIndex] = entry;
}

// Kodas….

int [] array = new int[5];
int numberToInsert = 7;
int entriesFilled = 3;
array[0] = 1;
array[1] = 5;
array[2] = 10;

for (int i = 0; i < entriesFilled; i++)
if (array[i] > numberToInsert) {
insertEntryIntoArray(i, numberToInsert, array);
entriesFilled++;
break;
}

Gal kam pravers :)