Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

J2ME Diplaying a picture

Status
Not open for further replies.

BPMan

Programmer
Jun 25, 2002
163
US
i am trying to simply display a picture with a midlet but can't seem to get it to work, does anyone know how to do this???
do i have to include the picture in the package???
 
it's not something i've done, but is there a method called paintComponent that you can extend? there is in most visual java stuff (applets, awt, swing etc.)

if there is, then try this:

public void paintComponent(Graphics g){
super.paintComponent(g);

Toolkit tk = Toolkit.getDefaultToolkit();
image i = tk.getImage("breasts.jpg"); //or whatever
g.drawimage(i, x, y, this);
}
//where x and y are the co-ordinates

 
below is the code i am using, i have an emulator for sprint phones and it does not display anything on them but it does on the defaultphones in my emulator, shouldn't this also work for sprint?



package TestFiles;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.* ;
import java.io.* ;
public class Test extends MIDlet implements CommandListener {

private Command exitCommand; // The exit command
private Command infoCommand;
private Command buyCommand;
private Display display;
private ImageItem imageItem;
private MyCanvas canvas;


public Test() {
display = Display.getDisplay(this);
canvas = new MyCanvas(this);
exitCommand = new Command("Exit",Command.SCREEN,2);

}

public void startApp() {
display.setCurrent(canvas);
}


public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}

}


class MyCanvas extends Canvas implements CommandListener
{
private Command cmdExit;
private Test midlet;
private Image image;
private int a;
private int b;
private int c;


public MyCanvas(Testmidlet)
{
a=b=c=0;
this.midlet = midlet;
cmdExit = new Command("Exit",Command.SCREEN,2);
addCommand(cmdExit);
setCommandListener(this);
try
{
image = Image.createImage("/TestFiles/gc.png");
}
catch (Exception e){}

}



public void paint(Graphics g)
{
g.drawImage(image,a,b,c);
//g.fillRect(50,60,10,20);

}
public void commandAction(Command c,Displayable d)
{
if (c == cmdExit)
{
midlet.destroyApp(false);
}

}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top