1. Displaying image in the screen (ImageMIDlet.java)
2. Printing a board of squares (SquareMIDlet.java)
3. Handling key Events (KeyEvent.java)
1. In the example, to define an image object, we have:
private Image ustLogo; |
2. In the example, to create the image object, we have:
ustLogo = Image.createImage("/logo.png"); |
3. In the example, to display the image which is simlar to draw a string, we have:
g.drawImage(ustLogo, getWidth()/2, getHeight()/2, g.VCENTER | g.HCENTER ); |
where "g" is an object of "Graphics".
4. To run the program, you have to download the image logo.png and store in the same directory with the .class files (such as C:\WTK22\apps\ImageMIDlet\classes).
1. Recall the function "fillRect" of class "Graphics":
fillRect(<x-coordinate>, <y-coordinate>, <width>, <height>); |
2. We now want to print a board of squares with length 12 pixels. The squares are seperated by space is size 2 pixels.
3. Recall the coorinate system used in Java is as follows:
-------->x |
4. The following codes can give you the number of horizontal blocks in a row and the number of vertical blocks a column:
int hTemp=bWidth-SPACE-MIN_MARGIN; numOfHBlock = hTemp / (BLK_SIZE+SPACE); |
5. So the total number of squares to be displace is: "numOfHBlock" times "numOfVBlock".
6. We have to use a nested for-loop to draw the squares:
posY = SPACE + vMargin; |
1. The GameConvas class is already designed to listen "key Events", to handle the event, we have to implement the funtion "keyPressed(keyCode)".
2. A key event is drawn when the user presses a key, the function "keyPressed" will be called.
3. The information of which key the user pressed is passed by the parameter "keyCode".
4. The example have shown how to distinguish the key pressed:
message=""; switch(keyCode) { switch(getGameAction(keyCode)) { |
5. In order to force the system to call the paint function, we have to call "repaint()".