📄 photocanvas.java
字号:
/*
* Filename: PhotoCanvas.java
* - contains definition of the class PhotoCanvas. It is a Canvas for image to be drawn on Canvas and to be shown to
* user. Methods are defined to react to key and pointer actions on phone - image scrolling and navigating forward
* or backward within image album.
*
* Application Name: Image Album Demo
* Version: 2.0
* Release Date: 27th September, 2002
* Author: Edmund Wong, Application Engineer, Metrowerks Hong Kong
*
*
* (c) Copyright. 2002. Metrowerks Corp. ALL RIGHTS RESERVED.
* This code is provided "AS IS" without warranty of any kind and subject to Metrowerks Sample Application
* End User License Agreement. NO EXTERNAL DISTRIBUTION OR COMMERCIALIZATION OF THE SOFTWARE IS PERMITTED
* EXCEPT BY WRITTEN AGREEMENT OF METROWERKS."
*/
import javax.microedition.lcdui.*;
/**
* Create a class for implementing the Canvas for showing Images
*/
public class PhotoCanvas extends Canvas {
/**
* Create references
*/
Image img; // for image to be shown on Canvas
String description; //for description to be shown on Canvas
int imageID; // the imageID that will be shown to user as 'Image x of y', where x = imageID and y is the
// total no. of images in Album
int imageByteSize; // for showing size in byte of the image on Canvas when needed, used in displaying
// downloaded image (i.e. when downloadImageCanvasFlag == true)
ImageAlbum imageAlbumReference; // for referencing the main object - ImageAlbum
boolean downloadImageCanvasFlag; // = true if it is for displaying downloaded image,
// = false if displaying images in the Album
Font font = null; // get font object for checking the height of the fonts on Canvas
/**
* Create variables for storing coordinates in pointer action for scrolling large images on screen
*/
int pointerStartX; // x-coordinate of the starting point of the pointer action
int pointerStartY; // y-coordinate of the starting point of the pointer action
int originX; // new origin's x-coordinate for origin translation
int originY; // new origin's y-coordinate for origin translation
int stepX; // storing the distance moved in x-direction during the pointer drag action
int stepY; // storing the distance moved in y-direction during the pointer drag action
int standardStepX; // distance in x-direction that will be moved for each scroll in non-touch screen phone
// (i.e. when hasPointerEvents() == false)
int standardStepY; // distance in y-direction that will be moved for each scroll in non-touch screen phone
// (i.e. when hasPointerEvents() == false)
/**
* Constructor of PhotoCanvas
*/
// constructor for use in creating PhotoCanvas for showing downloaded Image , with information of image size
// (in byte) passed
PhotoCanvas(Image img, String description, int imageByteSize){
this();
this.img = img;
this.description = description;
this.imageByteSize = imageByteSize;
downloadImageCanvasFlag = true; // for indicating this is the Canvas for showing downloaded image
}
// constructor for use in creating PhotoCanvas for showing Images stored in Album
PhotoCanvas(Image img, String description, ImageAlbum imageAlbumReference){
this();
this.img = img;
this.description = description;
this.imageAlbumReference = imageAlbumReference;
downloadImageCanvasFlag = false; // for indicating this is the Canvas for showing images in Album
}
PhotoCanvas(){
originX = 0;
originY = 0;
standardStepX = getWidth()/4; // setting default scrolling distance in x direction (for non-touch screen phone)
standardStepY = getHeight()/4; // setting default scrolling distance in y direction (for non-touch screen phone)
font = Font.getDefaultFont();
}
/**
* Draw the image onto the Canvas
*/
public void paint(Graphics g) {
g.setColor(255,255,255); // fill in the background with white color
g.fillRect(0,0,getWidth(),getHeight());
g.translate(originX, originY); // translate the origin of graphics to the coordinates (originX, originY)
g.setColor(0,0,0); // set color to black to draw string
if (downloadImageCanvasFlag == false){ // Display no. of Images in Album if viewing images in Album
g.drawImage(img, getWidth()/2, font.getHeight(), Graphics.TOP|Graphics.HCENTER); //draw the image
g.drawString(description, 5, 0, Graphics.TOP|Graphics.LEFT);
g.drawString("Image " + imageID + " of " + ImageAlbum.totalImageNumber, 5, font.getHeight() + img.getHeight(), Graphics.TOP|Graphics.LEFT);
} else { // Display size of images when viewing downloaded images
g.drawImage(img, getWidth()/2, font.getHeight()*2, Graphics.TOP|Graphics.HCENTER); //draw the image with two row at top for string
g.drawString("Choose Save Image to Save!", 5, 0, Graphics.TOP|Graphics.LEFT);
g.drawString(description, 5, font.getHeight(), Graphics.TOP|Graphics.LEFT);
g.drawString("Image size: " + imageByteSize + " bytes", 5, font.getHeight()*2 + img.getHeight(), Graphics.TOP|Graphics.LEFT);
}
}
/**
* Following methods provide functionality for user to scroll large image on screen for viewing the whole image,
* and also browsing the images in the Album by navigating forward and backward using key action
*
*
* on touch screen phone:
*
* scroll to view large image - by pointer drag action
*
* navigate to previous image - pressing navigation key UP or LEFT
*
* navigate to next image - pressing navigation key DOWN or RIGHT
*
*
* on non-touch screen phone:
*
* scroll to view large image - using navigation key UP,DOWN,LEFT,RIGHT
*
* navigate to previous image - pressing KEYPAD KEY '1'
*
* navigate to next image - pressing KEYPAD KEY '3'
*
*/
/**
* Method for responding to key action on phone
*/
protected void keyPressed(int keyCode) {
// On A008\6288\388, Game Action LEFT and RIGHT keys are the UP and DOWN arrow keys on top
// of handset, while Game Action UP and DOWN keys are the UP and DOWN arrow keys on the left
// side of the handset.
// keyPressed action for non-touch screen phone
if (hasPointerEvents() == false) {
// translate the origin of Graphics upon receiving navigation key action, distance translated =
// standardStep
switch (getGameAction(keyCode)) {
case UP:
originY += standardStepY;
repaint();
break;
case DOWN:
originY -= standardStepY;
repaint();
break;
case LEFT:
originX += standardStepX;
repaint();
break;
case RIGHT:
originX -= standardStepX;
repaint();
break;
}
if (downloadImageCanvasFlag == false) {
// for Canvas used in showing images in Album
// enable this Canvas to use functions for navigating forward and backward through Album
switch (keyCode) {
case KEY_NUM1:
imageAlbumReference.changePreviousPhoto(this);
break;
case KEY_NUM3:
imageAlbumReference.changeNextPhoto(this);
break;
}
}
}
else { // keyPressed action for touch screen phone
// navigation key UP or LEFT pressed -> navigate to previous image
// navigation key DOWN or RIGHT pressed -> navigate to next image
if ((getGameAction(keyCode) == UP || getGameAction(keyCode) == LEFT) && downloadImageCanvasFlag == false){
imageAlbumReference.changePreviousPhoto(this);
}
if ((getGameAction(keyCode) == DOWN || getGameAction(keyCode) == RIGHT) && downloadImageCanvasFlag == false){
imageAlbumReference.changeNextPhoto(this);
}
}
}
/**
* Method for responding to pointerPressed action on phone - record the coordinates of starting point
*/
protected void pointerPressed(int x, int y) {
if (y < 239 ) { // only respond to pointerPressed event occur outside command area (y coordinate < 239)
pointerStartX = x;
pointerStartY = y;
}
}
/**
* Method for responding to pointerDragged action on phone - calculate the distance that the pointer is dragged,
* same distance and direction is used for moving the image
*/
protected void pointerDragged(int x, int y) {
if (y < 239 ) { // only respond to pointerDragged event occur outside command area (y coordinate < 239)
// drag to a direction to move the image in that direction
stepX = x - pointerStartX ;
originX += stepX;
stepY = y - pointerStartY ;
originY += stepY;
pointerStartX = x;
pointerStartY = y;
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -