Saturday, May 04, 2013

J2ME

-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
http://snk.to/f-ctm2u2y8
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------

FOR IMAGE MOVE

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.amplify.canvas;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.*;
import java.lang.*;
import javax.microedition.lcdui.Font;

/**
 * @author P.HINDUJA
 */
public class MyImageCanvas extends MIDlet {

    Display disp;
    Image im;
    MyImageView miv;
 

    public MyImageCanvas() {
        disp = Display.getDisplay(this);
        try {
            im = Image.createImage("/splashScreen.png");
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        miv = new MyImageView(im);
    }

    public void startApp() {
        disp.setCurrent(miv);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

class MyImageView extends Canvas {

    Image img;
    String str;
      int px, py;

    public MyImageView(Image i) {
        img = i;
        str = "PRESS ANY ACTION KEY";
//        getGameAction(UP);
         px = getWidth()/2;
        py = getHeight()/2;
    }

    protected void paint(Graphics g) {
//        g.setGrayScale(255);
        g.setColor(255, 0, 0);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(img, px,
                py, g.HCENTER | g.VCENTER);
        System.out.println("WIDTH >>> " + getWidth());
        System.out.println("HEIGHT >>> " + getHeight());

        g.setColor(255, 255, 255);
        g.setFont(Font.getFont(Font.FACE_SYSTEM,
                Font.STYLE_BOLD, Font.SIZE_LARGE));
        g.drawString(str, getWidth() / 2, 0,
                Graphics.TOP | Graphics.HCENTER);
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    protected void keyPressed(int keyCode) {
        int gameAction = getGameAction(keyCode);
        switch (gameAction) {
            case UP:
                str = "UP PRESSED";
                py-=10;
                repaint();
                break;
            case DOWN:
                str = "DOWN PRESSED";
                 py+=10;
                repaint();
                break;
            case LEFT:
                str = "LEFT PRESSED";
                 px-=10;
                repaint();
                break;
            case RIGHT:
                str = "RIGHT PRESSED";
                 px+=10;
                repaint();
                break;
            case FIRE:
                str = "FIRE PRESSED";
                px=getWidth()/2;
                py=getHeight()/2;
                repaint();
                break;
            case KEY_POUND:
                str = "HASH KEY PRESSED";
                repaint();
                break;
            case KEY_STAR:
                str = "STAR KEY PRESSED";
                repaint();
                break;
        }
        super.keyPressed(keyCode);
    }
}
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
FOR RANDOM IMAGES

package hello;

import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand; // The exit command
    private Display display;     // The display for this MIDlet
    private MyImageCanvas ic;

    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
       
    }

    public void startApp() {
        ic = new MyImageCanvas(/*20, 20*/);
        ic.addCommand(exitCommand);
        ic.setCommandListener(this);
        display.setCurrent(ic);

        for(int i=0; i<20; i++){
        try{
        Thread.sleep(2000);
        } catch(Exception e){}
//        ic = new MyImageCanvas(100, 100);
//        display.setCurrent(ic);
        ic.repaint();
        }
//        try{
//        Thread.sleep(3000);
//        } catch(Exception e){}
//        ic = new MyImageCanvas(200, 200);
//        display.setCurrent(ic);
//        ic.repaint();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }  
}

class MyImageCanvas extends Canvas {

    Image img;
//    int xp, yp;

    public MyImageCanvas(/*int x, int y*/) {
        try{
            img = Image.createImage("/Duke.png");
//            xp = x;
//            yp = y;
        } catch(Exception e){
         System.out.println(e.toString());
        }
    }

    protected void paint(Graphics g) {
        int Xstart = img.getWidth()/2;
        Random rx = new Random();
        int Xmax = getWidth()-(img.getWidth()/2);
        int xr = rx.nextInt(Xmax);
        if (xr < Xstart)
            xr=Xstart;


        int Ystart = img.getHeight()/2;
        Random ry = new Random();
        int Ymax = getHeight()-(img.getHeight()/2);
        int yr = ry.nextInt(Ymax);
        if (yr < Ystart)
            yr=Ystart;


        Random rcolor = new Random();
        int rc = rcolor.nextInt(255);
        int gc = rcolor.nextInt(255);
        int bc = rcolor.nextInt(255);

        g.setColor(rc, gc, bc);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(img, xr,
                    yr, g.HCENTER|g.VCENTER);

//        for(int i=0; i<20; i++){
//            g.drawImage(img, getWidth()/4,
//                    (getHeight()/4)+(i*50),
//                    g.HCENTER|g.VCENTER);
//            g.drawImage(img, (getWidth()/4)+(i*70),
//                    3*(getHeight()/4),
//                    g.HCENTER|g.VCENTER);
//        }

//        throw new UnsupportedOperationException("Not " +
//                "supported yet.");
    }  
}

-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
FOR  RECORD MANAGEMENT

package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand,save; // The exit command
    private Display display;     // The display for this MIDlet
    TextField txtname,txtage,txtemail;
    Form register;
    Form blank;
    RecordStore rs;



    public HelloMIDlet() {
        display = Display.getDisplay(this);
        register=new Form("Register Form");
        blank=new Form("Welcome");
        save=new Command("Save", Command.SCREEN, 1);
        exitCommand = new Command("Exit", Command.EXIT, 0);
        txtname=new TextField("Name", "", 20, TextField.ANY);
        txtage=new TextField("Age", "", 20, TextField.NUMERIC);
        txtemail=new TextField("Email", "", 20, TextField.ANY);
        try {
            rs=RecordStore.openRecordStore("reg", true);
        } catch (Exception e) {
        }
    }

    public void startApp() {
        display.setCurrent(register);
        register.append(txtname);
        register.append(txtage);
        register.append(txtemail);
        register.setCommandListener(this);
        register.addCommand(save);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        } else if(c == save)
        {
           display.setCurrent(blank);
       
            try {
                rs.addRecord(txtname.getString().getBytes(), 0, txtname.getString().length());
                rs.addRecord(txtage.getString().getBytes(), 0, txtage.getString().length());
                rs.addRecord(txtemail.getString().getBytes(), 0, txtemail.getString().length());
                for(int i=1;i<=rs.getNumRecords();i++)
                {
                   
                    byte[] text=rs.getRecord(i);
                    String name= new String(text);
                    blank.append("\n"+name);
                }
           
            } catch (Exception e) {
            }

        }
    }

}
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
FOR TEXT PROG.

package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand, mm; // The exit command
    private Display display;     // The display for this MIDlet
    Form page1;
    TextField txt;
    Alert a1;


    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
         mm = new Command("SHOW", Command.SCREEN, 0);
        txt = new TextField("write", null, 20, TextField.ANY);
        page1 = new Form("welcome");
        //a1 = new Alert("" + txt.getString().toString());
    }

    public void startApp() {

        display.setCurrent(page1);
        page1.append(txt);
        page1.setCommandListener(this);
        page1.addCommand(mm);
        
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == mm){
            
             a1 = new Alert("" + txt.getString().toString());
        display.setCurrent(a1);
        }
    }

}
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
FOR PREVIOUS NEXT


package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand, prev, next; // The exit command
    private Display display;     // The display for this MIDlet
    Form page1, page2;



    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
         prev = new Command("PREV", Command.SCREEN, 0);
          next = new Command("NEXT", Command.SCREEN, 0);
          page1=new Form("Welcome");
          page2=new Form("GO BACK...!");
    }

    public void startApp() {
        display.setCurrent(page1);
        
        page1.setCommandListener(this);
        page1.addCommand(prev);
       
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        } else if(c == next){
            page2.setCommandListener(this);
        page2.addCommand(prev);
            display.setCurrent(page2);
           
        } else if (c == prev){
            page1.setCommandListener(this);
        page1.addCommand(next);
            display.setCurrent(page1);
            
        }

    }

}
-----------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Monday, April 29, 2013


1.
-------------------------------------------------------------------------
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.MovieClip;

 var left = false;
 var right = false;
 var up = false;
 var down = false;
var friction:Number = 0.95
 var speed:Number = 0;
var acceleration:Number = 0.2;
// var maxSpeed:Number = 20;
 //var maxReverse:Number = -8;
 var rot;

Mouse.hide()

stage.addEventListener(Event.ENTER_FRAME, main);
stage.addEventListener(MouseEvent.MOUSE_MOVE,topMove)
     
function tank()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);

}


function KeyDown(event:KeyboardEvent)
{
switch(event.keyCode )
{
case 37:
left = true;
break;
case 38:
up = true;
break;
case 39:
right = true;
break;
case 40:
down = true;
break;
/*default :
trace("wrong key");*/
}
}

function KeyUp(event:KeyboardEvent)
{
switch(event.keyCode )
{
case 37:
left = false;
break;
case 38:
up = false;
break;
    case 39:
right = false;
break;
case 40:
down = false;
break;
/*default :
trace("wrong key");*/
}
}



function main(even:Event):void
{
if (left)
{
body.rotation -= 2;

}
if (right)
{
body.rotation += 2;
}
if (up)
{
speed += acceleration;
}
if (down)
{
speed -= acceleration;
}

/*if (speed > maxSpeed)
{
speed = maxSpeed;
}
if (speed < acceleration && speed > -acceleration)
{
speed = 0;
}
else if ( speed < maxReverse)
{
speed = maxReverse;
}*/



var direction:Number = body.rotation * Math.PI / 180
var xSpeed:Number = -Math.sin(direction) * speed;
var ySpeed:Number = Math.cos(direction) * speed;

body.x -= xSpeed;
body.y -= ySpeed;
speed = speed * friction;

top.x=body.x;
top.y=body.y;
top.rotation=rot;

mc.x=mouseX;
mc.y=mouseY;
}

 function topMove(event:MouseEvent)
 {
rot = (180*Math.atan2(mouseY-top.y,mouseX-top.x))/Math.PI + 90;

 }

tank();
---------------------------------------------------------------------------------
2.
----------------------------------------------------------------------------------------
//import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

//var cam: Rectangle = new Rectangle(0,0,stage.stagewidth, stage.Stageheight);
var cam:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
var right:Boolean = false;
var left:Boolean = false;
var up:Boolean = false;
var SpeedX:int = 0;
var SpeedY:int = 0;



var level: Array = new Array();
for(var i:int = 0; i<numChildren; i++)
{
if(getChildAt(i) is platform)
{
level.push(getChildAt(i).getRect(this));
//trace(level);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,key_press);
stage.addEventListener(KeyboardEvent.KEY_UP,key_unpress);

function key_press(e:KeyboardEvent):void
{
if(e.keyCode == 39)
{
right = true;

}
if(e.keyCode == 37)
{
left = true;
//trace("left")
}
if(e.keyCode == 38)
{
up = true;

}
/*if(e.keyCode == 40)
{
down = true;

}*/

}
function key_unpress(e:KeyboardEvent):void
{
if(e.keyCode == 39)
{
right = false;

}
if(e.keyCode == 37)
{
left =  false;

}
if(e.keyCode == 38)
{
up = false;

}
/*if(e.keyCode == 40)
{
down = false;

}*/
}
stage.addEventListener(Event.ENTER_FRAME, onenter);
function onenter(e:Event):void
{
if(right == true)
{

SpeedX = 5;
//trace("right");

}
else if(left == true)
{
SpeedX = -5;
//trace("left");

}
else
{
SpeedX *= 0.5;
}

player.x += SpeedX;
//trace(player.x);
for(var i: int=0; i<level.length; i++)
{
if(player.getRect(this).intersects(level[i]))
{
if(SpeedX >0)
{
player.x= level[i].left-player.width/2;

}
if(SpeedX <0)
{
player.x=level[i].right+player.width/2;
}
SpeedX=0;
}
}

SpeedY+=1;
var jump:Boolean = false;
player.y += SpeedY;
//trace(player.x);
for(i = 0; i<level.length; i++)
{
if(player.getRect(this).intersects(level[i]))
{
if(SpeedY >0)
{
player.y= level[i].top-player.height/2;
SpeedY = 0;
jump = true;

}
if(SpeedY <0)
{
player.y=level[i].bottom+player.height/2;
SpeedY *= -0.5;
}

}

}
//trace(up);
if(up && jump)
{
SpeedY = -15;
//trace("hi");

}

cam.x = player.x - cam.width +360;
cam.y = player.y - cam.height + 175;
if(cam.x <= 0)
{
cam.x = 0;
}
if(cam.x >= 2060)
{
cam.x = 2060;
}
if(cam.y <= -300)
{
cam.y = -300;
}
if(cam.y >= 0)
{
cam. y = 0
}
scrollRect = cam;

/*cam.x = player.x - stage.stagewidth/2 360;
cam.y = player.y - stage.stagewidth/2 175;
scrollRect = cam;*/
}

-----------------------------------------------------------------------------------------------------
3.
------------------------------------------------------------------------------------------------------

import flash.events.KeyboardEvent;
import flash.events.Event;
import flashx.textLayout.utils.CharacterUtil;

var speedX = 0;
var speedY = 0;
var scroller = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);

var level:Array = new Array();

for(var i =0; i < numChildren; i++)
{
if(getChildAt(i) is platform)
{
level.push(getChildAt(i).getRect(this));

}
}
var flg:Boolean = false;
var coin_arr:Array = new Array();
for(var j:int =0; j < 20; j++)
{
var coin:cn = new cn();
addChild(coin);
if(j < 5)
{
coin.x = (j * 3 + 20)*10 + 20;
}
if(j > 4 && j < 7)
{
coin.x = (j * 3 + 20)*10 + 200;
}
if(j > 6 && j < 11)
{
coin.x = (j * 3 + 20)*10 + 300;
}
if(j > 10 && j < 16)
{
coin.x = (j * 3 + 20)*10 + 500;
}
if(j > 15 && j < 19)
{
coin.x = (j * 3 + 19)*10 + 700;
}
coin.y = 150;
coin_arr.push(coin);
}

var kUp:Boolean = false;
var kDwn:Boolean = false;
var kLeft:Boolean = false;
var kRight:Boolean = false;
var score:Number = 0;
HUD.scr.text = score.toString();

stage.addEventListener(KeyboardEvent.KEY_DOWN, kd);
stage.addEventListener(KeyboardEvent.KEY_UP, ku);


function kd(event:KeyboardEvent):void{
switch(event.keyCode)
{
case 37:
kLeft = true;
//trace("left")
break;
case 38:
kUp = true;
//trace("up")
break;
case 39:
kRight = true;
//trace("right")
break;
case 40:
kDwn = true;
//trace("down")
break;
}
}

function ku(event:KeyboardEvent):void{
switch(event.keyCode)
{
case 37:
kLeft = false;
//trace("left")
break;
case 38:
kUp = false;
//trace("up")
break;
case 39:
kRight = false;
//trace("right")
break;
case 40:
kDwn = false;
//trace("down")
break;
}
}

addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void
{

if(kLeft)
{
speedX = -5
}else if(kRight)
{
speedX = 5
}else
{
speedX *= 0.5;
}
player.x += speedX;
for(i = 0; i < level.length; i++)
{
if(player.getRect(this).intersects(level[i]))
{
if(speedX > 0)
{
player.x = level[i].left - player.width / 2;
}
if(speedX < 0)
{
player.x = level[i].right + player.width / 2;
}
speedX = 0;
}
}

var jumpable = false;
speedY += 1;

player.y += speedY;
for(i = 0; i < level.length; i++)
{
if(player.getRect(this).intersects(level[i]))
{
if(speedY > 0)
{
player.y = level[i].top - player.height / 2;
speedY = 0;
jumpable = true;
}
if(speedY < 0)
{
player.y = level[i].bottom + player.height / 2;
speedY *= -0.5;
}
//speedX = 0;
}
}
if(kUp && jumpable)
{
speedY = -15;
}

scroller.x = (player.x - scroller.width+360);
scroller.y = (player.y - scroller.height+175);
if(scroller.x >= 1050){
scroller.x = 1050;
}
if(scroller.x <= 0){
scroller.x = 0;
}
if(scroller.y >= 0){
scroller.y = 0;
}
if(scroller.y <= -610)
{
scroller.y = -610;
}
scrollRect = scroller;
for(var k:int = 0; k < coin_arr.length; k++)
{
if(coin_arr[k].hitTestObject(player))
{
score++;
HUD.scr.text = score.toString();
removeChild(coin_arr[k]);

//trace("hit");
}
}
HUD.x = scroller.x + 70;
HUD.y = scroller.y + 50;
/* if(player.y > 400)
{ trace(player.y);
speedX *= 0;
speedY *= 0;

}*/

}

--------------------------------------------------------------------------------------