listing13-24_movingbackground.java

来自「着几乎所有智能机厂商都将有自己配套的App Store,甚至并非智能手机制造商的」· Java 代码 · 共 66 行

JAVA
66
字号
//#condition polish.usePolishGui
package com.apress.ui;

import javax.microedition.lcdui.Graphics;

/**
 * <p>Paints a background that follows the currently focused item</p>
 *
 * <p>Copyright Enough Software 2004, 2005</p>
 * @author Robert Virkus, j2mepolish@enough.de
 */
public class MovingBackground extends Background {
	
	private final int color;
	private final int speed;
	private int nextX;
	private int nextY;
	private int currentX;
	private int currentY;

	public MovingBackground( int color, int speed ) {
		super();
		this.color = color;
		this.speed = speed;
	}

	public void paint(int x, int y, int width, int height, Graphics g) {
		if (x != this.nextX || y != this.nextY ) {
			this.nextX = x;
			this.nextY = y;
		}
		g.setColor( this.color );
		g.fillRect( this.currentX, this.currentY, width, height );
	}

	public boolean animate() {
		if (this.currentX == this.nextX && this.currentY == this.nextY ) {
			return false;
		}
		if ( this.currentX < this.nextX ) {
			this.currentX += this.speed;
			if ( this.currentX > this.nextX ) {
				this.currentX = this.nextX;
			}
		} else if ( this.currentX > this.nextX ) {
			this.currentX -= this.speed;
			if ( this.currentX < this.nextX ) {
				this.currentX = this.nextX;
			}
		}
		if ( this.currentY < this.nextY ) {
			this.currentY += this.speed;
			if ( this.currentY > this.nextY ) {
				this.currentY = this.nextY;
			}
		} else if ( this.currentY > this.nextY ) {
			this.currentY -= this.speed;
			if ( this.currentY < this.nextY ) {
				this.currentY = this.nextY;
			}
		}
		return true;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?