Problem with repaint of display after a click event

Hi,

I have a problem with repaint of display. In particular in method keyPressed() i inserted a statement that, after i clicked bottom 2 of phone, must draw a string. But this string doesn't drawing.

Instead if i reduce to icon the window, which emulate my application, and then i enlarge it, i see display repainted with the string.

I don't know why.

Any suggestions?

Please help me.

[423 byte] By [fighter79@inwind.ita] at [2007-10-3 4:18:49]
# 1
HiPut the code from your paint and keyPressed methods in here.Mihai
Printisora at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Hi Mihai,

this is the code of my class that extends Canvas.

public class PlayerCanvas extends Canvas{

private Image play, pause, stop, next, previous = null;

private Graphics graph;

protected void paint(Graphics g)

{

graph = g;

g.setColor(0,0,150);

g.fillRect(0, 0, getWidth(), getHeight());

createController();

if (play != null){

g.drawImage(play, getWidth()/5, getHeight()-50, Graphics.BOTTOM | Graphics.HCENTER);

}

if (stop != null){

g.drawImage(stop, getWidth()/5, getHeight()-10, Graphics.BOTTOM | Graphics.HCENTER);

}

if (next != null){

g.drawImage(next, (getWidth()/5)+10, getHeight()-30, Graphics.BOTTOM | Graphics.LEFT);

}

if (previous != null){

g.drawImage(previous, (getWidth()/5)-30, getHeight()-30, Graphics.BOTTOM | Graphics.LEFT);

}

}

private void createController()

{

try {

play = Image.createImage("/icons/play.png");

pause = Image.createImage("/icons/pause.png");

stop = Image.createImage("/icons/stop.png");

next = Image.createImage("/icons/next.png");

previous = Image.createImage("/icons/previous.png");

} catch (IOException e) {

play = null;

pause = null;

stop = null;

next = null;

previous = null;

}

if (play == null){

System.out.println("cannot load play.png");

}

if (pause == null){

System.out.println("cannot load pause.png");

}

if (stop == null){

System.out.println("cannot load stop.png");

}

if (next == null){

System.out.println("cannot load next.png");

}

if (previous == null){

System.out.println("cannot load previous.png");

}

}

protected void keyPressed(int keyCode)

{

graph.setColor(0,0,0);

if ( (keyCode == 2) || (UP == getGameAction(keyCode)) ){

graph.drawString("PROVA", 0, 0, 0);

}

else if ( (keyCode == 8) || (DOWN == getGameAction(keyCode)) ){

}

else if ( (keyCode == 4) || (LEFT == getGameAction(keyCode)) ){

}

else if ( (keyCode == 6) || (RIGHT == getGameAction(keyCode)) ){

}

}

protected void keyReleased(int keyCode)

{

if ( (keyCode == 2) || (UP == getGameAction(keyCode)) ){

}

else if ( (keyCode == 8) || (DOWN == getGameAction(keyCode)) ){

}

else if ( (keyCode == 4) || (LEFT == getGameAction(keyCode)) ){

}

else if ( (keyCode == 6) || (RIGHT == getGameAction(keyCode)) ){

}

}

}

fighter79@inwind.ita at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
Try calling repaint() after you draw the string in your keyPressed method.Mihai
Printisora at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4
Hi Mihai.I already tried to insert method repaint() aftrer drawing the string, but don't happend nothing.
fighter79@inwind.ita at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

Try cleaning the canvas first in your paint method and also do a check to see if the key was pressed, like not drawing the string in the keyPressed method, but setting a variable there instead which tells you if it was pressed or not:

protected void paint(...) {

//clean Canvas

graphics.setColor(255,255,255);

graphics.fillRect(0, 0, getWidth(), getHeight());

...

//check to see if key no. 2 was pressed

if (wasPressed) {

drawString(...)

}

}

Mihai

Printisora at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6
I already tried to insert a println in method keyPressed and effectly it do that. In fact in WTK window i see result of println. This tell me that the key was pressed.
fighter79@inwind.ita at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

I was talking about drawing the String not in the keyPressed method, but in the paint method. In the keyPressed method you check to see if that key was pressed and accordantly to what you get, you set a variable ("wasPressed") and call repaint(). In your paint method you check for that boolean variable and you draw the String if it is true (I already wrote you this in my last post).

Mihai

Printisora at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

modified your code little

don't draw in keyPressed

///////////////////////////////////////////////////////////////////////////////////

import java.io.IOException;

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Display;

import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

public class PlayerCanvas extends Canvas implements CommandListener{

Display display;

Displayable dsp11;

private Image play, pause, stop, next, previous = null;

private int gamcode;

private Command quitCmd = new Command("Back", Command.ITEM, 1);

public PlayerCanvas(Display display,Displayable dsp11){

this.display =display;

this.dsp11 =dsp11;

addCommand(quitCmd);

createController();

setCommandListener(this);

display.setCurrent(this);

}

protected void paint(Graphics g)

{

g.setColor(255,200,150);

g.fillRect(0, 0, getWidth(), getHeight());

if (play != null){

g.drawImage(play, getWidth()/5, getHeight()-50, Graphics.BOTTOM | Graphics.HCENTER);

}

if (stop != null){

g.drawImage(stop, getWidth()/5, getHeight()-10, Graphics.BOTTOM | Graphics.HCENTER);

}

if (next != null){

g.drawImage(next, (getWidth()/5)+10, getHeight()-30, Graphics.BOTTOM | Graphics.LEFT);

}

if (previous != null){

g.drawImage(previous, (getWidth()/5)-30, getHeight()-30, Graphics.BOTTOM | Graphics.LEFT);

}

/////this will draw on key UP

g.setColor(0,0,0);

System.out.print(gamcode);

if(gamcode==Canvas.UP){

g.drawString("PROVA",10, 0, 0);

}else if(gamcode==Canvas.DOWN){

g.drawString("DIFFERENT",10, 30, 0);

}

}

private void createController()

{

try {

play = Image.createImage("/icon3.png");//replace your original images plz

pause = Image.createImage("/icon3.png");

stop = Image.createImage("/icon3.png");

next = Image.createImage("/icon3.png");

previous = Image.createImage("/icon3.png");

} catch (IOException e) {

play = null;

pause = null;

stop = null;

next = null;

previous = null;

}

if (play == null){

System.out.println("cannot load play.png");

}

if (pause == null){

System.out.println("cannot load pause.png");

}

if (stop == null){

System.out.println("cannot load stop.png");

}

if (next == null){

System.out.println("cannot load next.png");

}

if (previous == null){

System.out.println("cannot load previous.png");

}

}

protected void keyPressed(int keyCode)

{

repaint();

if ( (keyCode == 2) || (UP == getGameAction(keyCode)) ){

gamcode = UP;

repaint();

}

else if ( (keyCode == 8) || (DOWN == getGameAction(keyCode)) ){

gamcode =DOWN;

repaint();

}

else if ( (keyCode == 4) || (LEFT == getGameAction(keyCode)) ){

}

else if ( (keyCode == 6) || (RIGHT == getGameAction(keyCode)) ){

}

}

public void commandAction(Command arg0, Displayable arg1) {

// TODO Auto-generated method stub

if(arg0==quitCmd){

display.setCurrent(dsp11);

}

}

}

sailesh_dita at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9
> modified your code little>don't draw in keyPressed I said the same thing :). Regarding the code, I think you should have left him manage at least that part.Mihai
Printisora at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10
Hi Mihai.I tried your little modify and function all.Thank you very much for your suggestion!Thanks a lot.bye bye
fighter79@inwind.ita at 2007-7-14 22:20:33 > top of Java-index,Java Mobility Forums,Java ME Technologies...