Convert Java Program to Java ME code
I'm just new to programming, and I want to try Mobile Applications. I have a code here that I want to convert to be a sample of mobile app. I don't know where and how to start. Hope you can help me. I'm using netbeans 6.7.1.
Here's my code to be converted to a sample mobile app.
Quote:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
public class Shapes extends JFrame
{
public double max=2;
public double min=1;
public boolean test;
public double size;
Thread th;
public Shapes()
{
super("Drawing 2d Shapes");
size=min;
mouseHandler mHand = new mouseHandler();
addMouseListener(mHand);
keyHandler kHand=new keyHandler();
addKeyListener(kHand);
getContentPane().setBackground(Color.green);
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
int xPoints[]={55,67,109,73,83,55,27,37,1,43};
int yPoints[]={0,36,36,54,96,72,96,54,36,36};
Graphics2D g2d=(Graphics2D)g;
GeneralPath star= new GeneralPath();
star.moveTo(xPoints[0]*size, yPoints[0]*size);
for(int count=1;count<xPoints.length;count++){
star.lineTo(xPoints[count]*size, yPoints[count]*size);
}
star.closePath();
g2d.translate(200,200);
for(int count=1; count<=20 ;count++){
g2d.rotate(Math.PI/10.0);
g2d.setColor(new Color(
(int)(Math.random()*256),
(int)(Math.random()*256),
(int)(Math.random()*256)));
g2d.fill(star);
}
}
public class mouseHandler implements MouseListener{
public void mouseReleased(MouseEvent e){
th=new Thread(new RunnableObject());
th.start();
}
public void mouseClicked(MouseEvent e){
th=new Thread(new RunnableObject());
th.start();
}
public void mousePressed(MouseEvent e){
if(test){
test=false;
}else{
test=true;
}
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
}
public class keyHandler implements KeyListener{
public void keyReleased(KeyEvent e){
if(e.getKeyText(e.getKeyCode()).equalsIgnoreCase(" Up")){
size+=.2;
if(size>max){
size=max;
}
}else if(e.getKeyText(e.getKeyCode()).equalsIgnoreCase(" Down")){
size-=.2;
if(size<min){
size=min;
}
}
repaint();
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e){
}
}
public class RunnableObject implements Runnable{
public void run(){
while(test){
try{
th.sleep(100);
}catch(InterruptedException e){
}
repaint();
}
}
}
public static void main(String args[]){
Shapes s=new Shapes();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Thanks in advance.
Re: Convert Java Program to Java ME code
Hi rinchan11
I think you should start it at begin.
In a J2ME program, you must make a class extends MIDlet class, and implement methods which describ for the MIDlet cycle.
In your program, you used mouse event that it don't have on mobile. If you want to write application for touch screen mobiles, that event will be seen as pointerPress, pointerRelease event.
Thanks for your attentions,
Tien