📄 mybackground.java
字号:
package example;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: Sensible Development Ltd</p> * @author Alan Newman * @version 1.0 */import javax.microedition.lcdui.*;public class MyBackground{ private static int imageWidth, imageHeight; private static int tileWidth, tileHeight; private static int canvasWidth, canvasHeight; private Image[] images; private final String BACKGROUND_NAME="background"; private final int BACKGROUND_FRAMES=6; private Image[] imgTile=new Image[BACKGROUND_FRAMES]; private static Image imgBackground; private static int xBackground, yBackground; int xStart=0; int yStart=9; int iImageArray[][]={{0,1,0,0,0,0,3,0,0,5}, {1,0,4,0,0,0,0,4,4,0}, {0,0,3,0,3,4,4,2,0,0}, {0,2,4,3,4,2,4,2,0,0}, {0,4,0,0,3,3,4,0,0,0}, {0,0,0,0,3,1,0,0,1,1}, {0,0,0,3,3,1,0,2,1,1}, {0,0,2,3,4,0,0,0,0,0}, {0,0,0,0,0,0,0,5,0,0}, {0,0,0,0,0,0,0,0,0,0}}; int arrayHeight=10; int arrayWidth=10; public MyBackground() { // load the images from the .jar imgTile=getImages(BACKGROUND_NAME,BACKGROUND_FRAMES); // set the canvas measurements to local class variables. canvasWidth=(MyCanvas.getCanvasWidth()); canvasHeight=(MyCanvas.getCanvasHeight()); // all character frame images are assumed // to be the same height and width tileWidth=imgTile[0].getWidth(); tileHeight=imgTile[0].getHeight(); // set the background image dimensions imageWidth=((canvasWidth/tileWidth)*tileWidth)+(tileWidth*2); imageHeight=((canvasWidth/tileHeight)*tileHeight)+tileHeight; createImage(); xBackground=0; yBackground=-tileHeight; setImage(); } public Image getImage(){return imgBackground;} public void draw(Graphics g) { g.drawImage(getImage(),xBackground,yBackground,g.LEFT|g.TOP); } public void setPosition() { //move the background to provide animation of moving landscape. boolean reset=false; // if the image exceeds the x-axis then reset it // and set the starting point for the next row in the image array // set the redraw flag for redrawing the image if(--xBackground==-tileWidth) { xBackground=0; if(++yStart>9) { yStart=0; } reset=true; } // if the images exceeds the y-axis then reset it // and set the stating point for the next column in the image array // set the redraw flag for redrawing the image if(++yBackground==0) { yBackground=-tileHeight; if(--xStart<0) { xStart=9; } reset=true; } // redraw the background image if reset=true if(reset) { setImage(); } } private static void createImage() { // allocate memory to store the onscreen background image. // assuming all tiles are the same size, make the buffer // the screen size plus one row and column. imgBackground=Image.createImage(imageWidth, imageHeight); } public void setImage() { Graphics g = imgBackground.getGraphics(); int currentWidth=0; int currentHeight=(imageHeight-tileHeight); int xOffset=xStart; int yOffset=yStart; // draw the tiles within the bounds of the allocated image buffer // draw from the starting from the current position within the image array. while (currentHeight>=0) { yOffset=yStart; while (currentWidth<imageWidth) { g.drawImage(imgTile[iImageArray[xOffset][yOffset]],currentWidth,currentHeight,g.LEFT | g.TOP); if(++yOffset>9) { yOffset=0; } currentWidth+=tileWidth; } if((--xOffset)<0) { xOffset=9; } currentWidth=0; currentHeight-=tileHeight; } } private Image[] getImages(String imgName, int frames) { return ExampleMidlet.getImages(imgName, frames); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -