Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is keyPreesed (int parameter)

 
waqas shahwar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic