aspose file tools*
The moose likes Java Micro Edition and the fly likes what is keyPreesed (int parameter) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "what is keyPreesed (int parameter)" Watch "what is keyPreesed (int parameter)" New topic
Author

what is keyPreesed (int parameter)

waqas shahwar
Greenhorn

Joined: Sep 17, 2003
Posts: 19
hello world i couldnot understand following piece of code in following MIDlet
switch (action) {
case LEFT:
if (--curSlide < 0)
curSlide = slides.length - 1;
repaint();
break;
case RIGHT:
if (curSlide >= slides.length)
curSlide = 0;
repaint();
break;
}
WHY pre sign of minus & plus operator is used --curSlide ++curSlide
i could not get understanding from this point also keyPressed method i understand i just wrote for heading.
there is whole piece of code
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
public class SlideShow extends MIDlet implements CommandListener {
private Command exitCommand;
private Display display;
private SSCanvas screen;
public SlideShow() {
// Get the Display object for the MIDlet
display = Display.getDisplay(this);
// Create the Exit command
exitCommand = new Command("Exit", Command.EXIT, 2);
// Create the main screen form
screen = new SSCanvas();
// Set the Exit command
screen.addCommand(exitCommand);
screen.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException {
// Set the current display to the screen
display.setCurrent(screen);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}
class SSCanvas extends Canvas {
private Image[] slides;
private String[] captions = { "Automotive", "Beauty", "Construction",
"Pest Control", "Pet Store", "Restaurant" };
private int curSlide = 0;
public SSCanvas() {
// Load the slide show images
try {
slides = new Image[6];
slides[0] = Image.createImage("/BizAuto.png");
slides[1] = Image.createImage("/BizBeauty.png");
slides[2] = Image.createImage("/BizConstruction.png");
slides[3] = Image.createImage("/BizPest.png");
slides[4] = Image.createImage("/BizPet.png");
slides[5] = Image.createImage("/BizRestaurant.png");
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
}
public void keyPressed(int keyCode) {
// Get the game action from the key code
int action = getGameAction(keyCode);
// Process the left and right buttons
switch (action) {
case LEFT:
if (--curSlide < 0)
curSlide = slides.length - 1;
repaint();
break;
case RIGHT:
if (curSlide >= slides.length)
curSlide = 0;
repaint();
break;
}
}
public void paint(Graphics g) {
// Clear the display
g.setColor(255, 255, 255); // White
g.fillRect(0, 0, getWidth(), getHeight());
// Draw the current image
g.drawImage(slides[curSlide], getWidth() / 2, getHeight() / 2,
Graphics.HCENTER | Graphics.VCENTER);
// Set the font for the caption
Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
g.setFont(f);
// Draw the current caption
g.setColor(0, 0, 0); // Black
g.drawString(captions[curSlide], getWidth() / 2, 0,
Graphics.HCENTER | Graphics.TOP);
}
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Apparently the "--" operator is used to decrement the slide index when the user clicks the "left" button, effectively making the previous image displayed when the screen is repainted.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is keyPreesed (int parameter)