Java ME draw rectangle
How can I draw rectangles with Ja开发者_如何学编程va ME? Sorry, but I am new to Java and have to use Java ME for this project.
It's hard to know exactly how much detail to go into because you don't mention what you've done with ME so far, but the basic idea is:
- Override the Canvas class with your own class
- Override the paint() method
- Inside the paint() method, you can call drawRect() or fillRect() on the Graphics object that's passed in
- Elsewhere (e.g. in the startApp() method of your MIDlet class), set an instance of your canvas to be the current display
So example code looks roughy as follows. Create a Canvas class something like this:
public class MyCanvas extends Canvas {
public void paint(Graphics g) {
g.drawRect(20, 20, 50, 50);
}
}
Then something like this in your MIDlet class:
public class MyMIDlet extends MIDlet {
public void startApp() {
Canvas c = new MyCanvas();
Display.getDisplay(this).setCurrent(c);
}
...
}
Good guides to Java ME should give you an overview of other methods available on Graphics, other code you'll need in your MIDlet class, how to handle Commands (for handling button presses) etc.
精彩评论