📄 layermanager.java
字号:
/*
* Created on 2005-12-20 by pcy
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package javax.microedition.lcdui.game;
import javax.microedition.lcdui.Graphics;
public class LayerManager {
/**
* The number of layers in this LayerManager. This value can be null.
*/
private int nlayers; // = 0;
/**
* The Layers in this LayerManager.
*
* @see #append(Layer)
*/
private Layer component[] = new Layer[4];
/**
* the view window co-ordinates.
*/
private int viewX, viewY, viewWidth, viewHeight; // = 0;
public LayerManager() {
setViewWindow(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
}
public void append(Layer l) {
removeImpl(l);
addImpl(l, nlayers);
}
public void insert(Layer l, int index) {
/* Check for correct arguments: index in bounds */
if ((index < 0) || (index > nlayers)) {
throw new IndexOutOfBoundsException();
}
// if the Layer is already present
// remove it
// will throw NullPointerException if the Layer is null
removeImpl(l);
// insert it at the specified index
addImpl(l, index);
}
public Layer getLayerAt(int index) {
if ((index < 0) || (index >= nlayers)) {
throw new IndexOutOfBoundsException();
}
return component[index];
}
public int getSize() {
return nlayers;
}
public void remove(Layer l) {
removeImpl(l);
}
public void paint(Graphics g, int x, int y) {
// if g == null g.getClipX will throw NullPointerException;
// save the original clip
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipW = g.getClipWidth();
int clipH = g.getClipHeight();
// translate the LayerManager co-ordinates to Screen co-ordinates
g.translate(x - viewX, y - viewY);
// set the clip to view window
g.clipRect(viewX, viewY, viewWidth, viewHeight);
// draw last to first
for (int i = nlayers; --i >= 0;) {
Layer comp = component[i];
if (comp.visible) {
// REMINDER: do this if outside Graphics clip region don't paint
// (comp.contains(x - comp.x, y - comp.y)) &&
// paint will happen only in clipped region of view window
comp.paint(g);
}
}
g.translate(-x + viewX, -y + viewY);
g.setClip(clipX, clipY, clipW, clipH);
}
public void setViewWindow(int x, int y, int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException();
}
viewX = x;
viewY = y;
viewWidth = width;
viewHeight = height;
}
private void addImpl(Layer layer, int index) {
// Add component to list;
// allocate new array if necessary.
if (nlayers == component.length) {
Layer newcomponents[] = new Layer[nlayers + 4];
System.arraycopy(component, 0, newcomponents, 0, nlayers);
System.arraycopy(component, index, newcomponents, index + 1,
nlayers - index);
component=null;
component = newcomponents;
} else {
System.arraycopy(component, index, component, index + 1, nlayers
- index);
}
component[index] = layer;
++nlayers;
}
private void removeImpl(Layer l) {
if (l == null) {
throw new NullPointerException();
}
for (int i = nlayers; --i >= 0;) {
if (component[i] == l) {
remove(i);
}
}
}
private void remove(int index) {
System.arraycopy(component, index + 1, component, index, nlayers
- index - 1);
component[--nlayers] = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -