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.
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)) ){
}
}
}
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);
}
}
}