I am Using Netbeans for my coading with Qearty device to achive a touch screen support. i have write one prog but it wont work for Pointerpressed events in J2ME Plese help me My code is Below..
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/*
A quick sample of graphics, commands, and event handling.
*/
public class PointerExample extends MIDlet implements CommandListener
{
Display display;
Command exitCommand;
Command backCommand;
Command okCommand;
SampleCanvas sample; // Instance of sample canvas
List itemMenu;
Ticker ticker;
Alert alert;
Form form;
StringItem stringItem;
public PointerExample() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 1);
backCommand = new Command("Back", Command.BACK, 2);
okCommand = new Command("OK", Command.OK, 3);
ticker = new Ticker("Here is Pointer Demo.");
itemMenu = new List(null, Choice.IMPLICIT);
itemMenu.append("Canvas", null);
itemMenu.setCommandListener(this);
itemMenu.addCommand(exitCommand);
itemMenu.setTicker(ticker);
display.setCurrent(itemMenu);
}
public void startApp () {
}
public void destroyApp (boolean unconditional) {
}
public void pauseApp () {
}
public void commandAction(Command c, Displayable s)
{
if (c == backCommand)
{
display.setCurrent(itemMenu);
}
else if (s == itemMenu)
{
if (c == List.SELECT_COMMAND)
{
int i = itemMenu.getSelectedIndex();
switch (i)
{
case 0: // Show Sample canvas
display.setCurrent(getCanvas());
break;
}
}
}
else if (c == exitCommand)
{
notifyDestroyed();
}
}
SampleCanvas getCanvas()
{
if (sample == null)
{
sample = new SampleCanvas();
sample.addCommand(backCommand);
sample.setCommandListener(this);
}
return sample;
}
}
class SampleCanvas extends Canvas
{
int x, y; // Location of cross hairs
String event = ""; // Last key event type
int keyCode; // Last keyCode pressed
Font font; // Font used for drawing text
int fh; // height of the font
int w, h; // width and height of the canvas
int titleHeight; // Height of the title
int pieSize; // Size of the Pie chart used for width and height
int barSize; // Size of the Bar chart used for width and height
int eventHeight; // Size of the event region
int pad; // Padding used between items
boolean setmouseenable = true;
SampleCanvas()
{
w = getWidth();
h = getHeight();
System.out.println("Width : "+w);
System.out.println("height: "+h);
font = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_PLAIN, Font.SIZE_SMALL);
fh = font.getHeight();
pad = 2;
titleHeight = fh + pad * 2;
eventHeight = fh * 3;
}
protected void keyPressed(int key)
{
keyCode = key;
event = " Key Pressed";
handleActions(key);
repaint();
}
protected void keyRepeated(int key)
{
keyCode = key;
event = " Key Repeated";
handleActions(key);
repaint();
}
protected void keyReleased(int key)
{
keyCode = key;
event = " Key Released";
repaint();
}
protected void pointerPressed(int x, int y)
{
this.x = x;
this.y = y;
System.out.println(Integer.toString(this.x));
System.out.println(Integer.toString(this.y));
keyCode = 0;
event =" Pointer Pressed";
repaint();
}
protected void pointerReleased(int x, int y)
{
this.x = x;
this.y = y;
keyCode = 0;
event = "Pointer Released";
repaint();
}
protected void pointerDragged(int x, int y)
{
this.x = x;
this.y = y;
keyCode = 0;
event = "Pointer Dragged";
}
void handleActions(int keyCode)
{
int action = getGameAction(keyCode);
switch (action)
{
case LEFT:
x -= 1;
break;
case RIGHT:
x += 1;
break;
case UP:
y -= 1;
break;
case DOWN:
y += 1;
break;
}
}
protected void paint(Graphics g)
{
g.setFont(font);
g.setColor(152, 150, 150);
g.fillRect(0, 0, w, h);
/* x = (x < 0) ? w - 1 : x;
y = (y < 0) ? h - 1 : y;
x = x % w;
y = y % h;
*/
// Draw the key and pointer status
g.setColor(255, 255, 0);
int col1 = font.stringWidth("Action:");
g.drawString("Key: ", col1, 0,
Graphics.TOP|Graphics.RIGHT);
g.drawString(keyString(keyCode), col1, 0,
Graphics.TOP|Graphics.LEFT);
g.drawString("Action:", col1, fh,
Graphics.TOP|Graphics.RIGHT);
g.drawString(actionString(keyCode), col1, fh,
Graphics.TOP|Graphics.LEFT);
g.drawString("Event:", col1, fh*2,
Graphics.TOP|Graphics.RIGHT);
g.drawString(event, col1, fh*2,
Graphics.TOP|Graphics.LEFT);
int col2 = 80;
g.drawString("x:", col2, 0,
Graphics.TOP|Graphics.RIGHT);
g.drawString(Integer.toString(x), col2, 0,
Graphics.TOP|Graphics.LEFT);
g.drawString("y:", col2, fh,
Graphics.TOP|Graphics.RIGHT);
g.drawString(Integer.toString(y), col2, fh,
Graphics.TOP|Graphics.LEFT);
// Restore the origin and draw the crosshairs on top
g.translate(-g.getTranslateX(), -g.getTranslateY());
g.setColor(0, 0, 0);
g.drawLine(x, y - 5, x, y + 5);
g.drawLine(x - 5, y, x + 5, y);
}
String keyString(int keyCode)
{
if (keyCode == 0)
{
return "";
}
return Integer.toString(keyCode);
}
String actionString(int keyCode)
{
if (keyCode == 0)
{
return "";
}
int action = getGameAction(keyCode);
switch (action)
{
case FIRE:
return "Fire";
case LEFT:
return "Left";
case RIGHT:
return "Right";
case DOWN:
return "Down";
case UP:
return "Up";
case GAME_A:
return "Game A";
case GAME_B:
return "Game B";
case GAME_C:
return "Game C";
case GAME_D:
return "Game D";
case 0:
return "";
default:
return Integer.toString(action);
}
}
}

Reply With Quote

