Sign in to follow this  

Problems displaying img's...

This topic is 4164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, here i have some code i'm having some trouble with... its java for J2ME. ================================ class SSCanvasCuatic extends Canvas implements Runnable { Image logo=null; public SSCanvasCuatic() { } public void run()//ciclo del juego { repaint(); } public void paint(Graphics g) { //Tablero try { g.setColor(0,0,0); g.fillRect(0,0,getWidth(),getHeight()); logo = Image.createImage("/cuatic.png"); g.drawImage (logo, 0, 0, Graphics.HCENTER|Graphics.VCENTER); Thread.sleep(2500); } catch (Exception e) { System.out.println(e); } try { g.setColor(255,255,255); g.fillRect(0,0,getWidth(),getHeight()); Font fuenteIntro = Font.getFont (Font.FACE_PROPORTIONAL, Font.style_BOLD,Font.SIZE_MEDIUM); g.setColor(0,0,0); g.setFont(fuenteIntro); g.drawString ("Hola.", 100, 100, Graphics.LEFT | Graphics.TOP); Thread.sleep(2500); } catch (Exception e) { System.out.println(e); } } } =================================== It's suppose to display an image and then a white page with text. The ting is, the text is displayed but not the image. The image is in the res director, in fact, i have been able to display it before without the text. What's wrong? can anyone help me? THanks a lot

Share this post


Link to post
Share on other sites
You paint method is drawing the picture (and reloading it every time a repaint is needed which is not good, but it's not the problem here), and then drawing the text on top of the picture, and that's why you're not seeing anything. Most phones implement an internal double buffer so they only display the results on screen after paint() finishes.

The paint method should do either one thing or the other depending on what state the canvas is in(use a variable to control which state you're in), using some sort of condition (an if statement or more likely a switch statement when you have more states in your game).

The run() method should be the one with the Thread.sleep() call: It should set the state variable to the value for showing the image and do a repaint, call sleep(), then set the state variable to the value for showing the text and do another repaint.

You can see a better explanation with source examples in Chapter 7 of Jason Lam's book.

shmoove

Share this post


Link to post
Share on other sites
Thanks for the help man. I tried to apply the solution you said, i hope the one i' showing now is. Well, here's the code.

========================================
[source lang=java]
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Main extends MIDlet implements CommandListener
{
private Command comandoSalir;
private Display desplegar;
private SSCanvasB pantalla;
private SSCanvasCuatic pantallaA;
private SSCanvasMotor pantallaB;

private boolean estadoA=false;
private boolean estadoB=false;
private boolean estadoC=false;
public Main() //constructor
{
desplegar=Display.getDisplay(this);
comandoSalir = new Command("Salir",Command.EXIT,2);
pantalla=new SSCanvasB();
pantalla.addCommand(comandoSalir);
pantalla.setCommandListener(this);
new Thread(pantalla).start();
//escaparA = new Command("Salir",Command.EXIT,2);
pantallaA=new SSCanvasCuatic();
//pantallaA.addCommand(escaparA);
pantallaA.setCommandListener(this);
new Thread(pantallaA).start();
//escaparB = new Command("Salir",Command.EXIT,2);
pantallaB=new SSCanvasMotor();
//pantallaB.addCommand(escaparB);
pantallaB.setCommandListener(this);
new Thread(pantallaB).start();

}
public void startApp() throws MIDletStateChangeException
{
estadoA=true;
while(estadoA==true)
{
desplegar.setCurrent(pantallaA);
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println(e);
notifyDestroyed();
}
estadoA=false;
estadoB=true;
}

while(estadoB==true)
{
desplegar.setCurrent(pantallaB);
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
System.out.println(e);
notifyDestroyed();
}
estadoB=false;
estadoC=true;
}
if(estadoC==true)
{
desplegar.setCurrent(pantalla);
}
}

public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s)
{
if (c == comandoSalir)
{
destroyApp(false);
notifyDestroyed();
}
}
}

class SSCanvasB extends Canvas implements Runnable
{
//Declaramos
private GrafEngine tableroBlanco=new GrafEngine(1);
private GrafEngine tableroNegro=new GrafEngine(1);
private GrafEngine fichaRoja=new GrafEngine(1);
//private GrafEngine fichaAzul=new GrafEngine(1);
private int sleepTime;
private int valorX;
private int valorY;
public int posX=0;
public int posY=0;
public int cond=0;

//Tablero
int mapa[]=
{
1,2,1,2,1,2,1,2,
2,1,2,1,2,1,2,1,
1,2,1,2,1,2,1,2,
2,1,2,1,2,1,2,1,
1,2,1,2,1,2,1,2,
2,1,2,1,2,1,2,1,
1,2,1,2,1,2,1,2,
2,1,2,1,2,1,2,1
};
public SSCanvasB()
{
//Cargamos y activamos los sprites
fichaRoja.agregarFrame(1,"/FR.png");
//fichaAzul.agregarFrame(1,"/FA.png");


fichaRoja.spriteOn();
//fichaAzul.spriteOn();

}

void iniciar()
{
sleepTime=50;
valorX=0;
valorY=0;
fichaRoja.setPosX(0);
fichaRoja.setPosY(0);
tableroNegro.agregarFrame(1,"/BB.png");
tableroBlanco.agregarFrame(1,"/WB.png");
tableroNegro.spriteOn();
tableroBlanco.spriteOn();
//cargamos imágenes de intro

}

void computar()
{
posX+=valorX;
posY+=valorY;
if(posX>=128)
{
posX=112;
}
if(posX<=0)
{
posX=0;
}
if(posY>=128)
{
posY=112;
}
if(posY<=0)
{
posY=0;
}
fichaRoja.setPosX(posX);
fichaRoja.setPosY(posY);
}
public void run()//ciclo del juego
{
iniciar();
while (true)
{
computar();
//Actualizar pantalla
repaint();
try
{
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
System.out.println(e.toString());
}

}
}

//Otros métodos necesarios :P


public void keyPressed(int keyCode)
{
int action=getGameAction(keyCode);
switch (action)
{
case LEFT:
if(posX<=0)
{
}
else
{
valorX=-16;
}
break;
case RIGHT:
if(posX>=112)
{
}
else
{
valorX=16;
}
break;
case UP:
if(posY<=0)
{
}
else
{
valorY=-16;
}
break;
case DOWN:
if(posY>=112)
{
}
else
{
valorY=16;
}
break;
}
}
public void keyReleased(int keyCode) {
int action=getGameAction(keyCode);
switch (action) {
case LEFT:
valorX=0;
break;
case RIGHT:
valorX=0;
break;
case UP:
valorY=0;
break;
case DOWN:
valorY=0;
break;
}
}
public int vuelta=0;

public void paint(Graphics g)
{
//Tablero
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
int b=0;
for(int j=0;j<8;j++)
{
tableroNegro.setPosY(16*j);
tableroBlanco.setPosY(16*j);

for(int i=0;i<8;i++)
{
if(mapa[i+b]==1)
{
tableroNegro.setPosX(16*i);
tableroNegro.dibujar(g);
}
else
{
tableroBlanco.setPosX(16*i);
tableroBlanco.dibujar(g);
}
}
b+=8;
}
//dibujar ficha
fichaRoja.setPosX(fichaRoja.getPosX());
fichaRoja.setPosY(fichaRoja.getPosY());
fichaRoja.dibujar(g);

}


}

class SSCanvasCuatic extends Canvas implements Runnable
{
Image logo = null;
public SSCanvasCuatic()
{


}
public void iniciar()
{
try
{
logo = Image.createImage("/cuatic.png");
}
catch (Exception e)
{
System.out.println(e);
}
}
public void run()//ciclo del juego
{
iniciar();
while (true)
{
//Actualizar pantalla
repaint();
try
{
Thread.sleep(1);
} catch (InterruptedException e) {
System.out.println(e.toString());
}

}
}

public void paint(Graphics g)
{
try
{
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.drawImage (logo, 0, 0, Graphics.LEFT | Graphics.TOP);

}
catch (Exception e)
{
System.out.println(e);
}

}
}
class SSCanvasMotor extends Canvas implements Runnable
{
public SSCanvasMotor()
{


}
public void run()//ciclo del juego
{
while (true)
{
//Actualizar pantalla
repaint();
try
{
Thread.sleep(1);
} catch (InterruptedException e) {
System.out.println(e.toString());
}

}
}

public void paint(Graphics g)
{
try
{
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
Font fuenteIntro = Font.getFont (Font.FACE_PROPORTIONAL, Font.style_BOLD,Font.SIZE_MEDIUM);
g.setColor(0,0,0);
g.setFont(fuenteIntro);
g.drawString ("Hola.", getWidth()/2, getHeight()/2, Graphics.LEFT | Graphics.TOP);

}
catch (Exception e)
{
System.out.println(e);
}

}

}


==============================================

Ok, the thing is, they work just fine now, but they only do in my nokia 6670 and in the emulator. When i try it on a MG201D from LG, the only "screen" that is displayed is the SSCanvas. I eliminated the class, and, again, the only one that was displayed was the last screen (in that case was the SSCanvasMotor).

I dont know what to do with that cellphone :(

Please help

[EDITED]

There it is. Sorry, i didnt know about the tags. :)

[/EDITED]

[Edited by - Goguigo on July 23, 2006 4:18:37 PM]

Share this post


Link to post
Share on other sites

This topic is 4164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this