// ## JAVANOID BUFFER CLASS ##################################
// this class implements a sort
of triple buffer : 1 for the background
// image & the objects that
you draw for a long time, 1 where the moving
// objects are drawn, and 1 for
display (see update method).
// Width & height of the
game canvas
protected int width;
protected int height;
Applet applet;
// On & off graphics for triple
(!) buffering
Graphics offGraphics, offBackGraphics;
//
Fontmetrics object
FontMetrics fontMetrics;
// Buffer & background
images
Image offImage, offBackImage, backImage;
Dimension minSize;
//
Color for texts & background
Color textColor = new Color (255, 223,
0);
Color backColor = new Color (0, 0, 0);
//
Constructor
public jnbuffer (Applet a, int w,
int h) {
applet = a;
width = w; height = h;
//
Off images & graphics
offImage = applet.createImage(width,
height);
offBackImage = applet.createImage(width,
height);
offGraphics =
offImage.getGraphics();
offBackGraphics = offBackImage.getGraphics();
minSize = new Dimension(width, height);
resize(width, height);
fontMetrics = offGraphics.getFontMetrics();
computeBackground(); clearBackground();
}
// À§·Î
//
Compute background image
void computeBackground() {
int[] pix = new int[width * height];
int index = 0;
for (int j=0;j<height;j++) {
int blue = 255 * j / (height - 1);
for (int i=0;i<width;i++) {
int red = 255 *
i / (width - 1);
pix[index++] =
(255 << 24) | (red << 16) | blue;
}
}
backImage = applet.createImage(new MemoryImageSource(width,
height, pix, 0, width));
}
// À§·Î
public void drawImage(Image i, int
x, int y) {
offGraphics.drawImage(i, x, y, this);
}
public void drawString(String s,
int x, int y) {
offGraphics.setColor(textColor);
offGraphics.drawString(s, x, y);
}
// À§·Î
public void drawPermanentString(String
s, int x, int y) {
offBackGraphics.setColor(textColor);
offBackGraphics.drawString(s, x, y);
}
public void drawPermanentImage (Image
i, int x, int y) {
offBackGraphics.drawImage(i, x, y, this);
}
// À§·Î
public void clearPermanentImage
(int x, int y, int w, int h) {
// With JDK1.1, it should
be
// offBackGraphics.drawImage(backImage,
x, y, x + w, y + h, x, y, x + w, y + h, this);
Graphics tempGraphics = offBackImage.getGraphics();
tempGraphics.clipRect(x, y, w, h);
tempGraphics.drawImage(backImage, 0, 0,
this);
}
public void clearBackground() {
if (offBackGraphics==null) offBackGraphics
= offBackImage.getGraphics();
offBackGraphics.drawImage(backImage, 0,
0, this);
}
// À§·Î
// Update & paint methods
public void paint(Graphics g) {
update(g); }
public void update(Graphics g) {
if (g==null) g = this.getGraphics();
g.drawImage(offImage, 0, 0, this);
if (offGraphics==null) offGraphics = offImage.getGraphics();
offGraphics.drawImage(offBackImage, 0,
0, this);
}
// À§·Î
// Dimension functions
public int getStringWidth(String
s) { return fontMetrics.stringWidth(s); }
public int getStringHeight() {
return fontMetrics.getHeight(); }
public int getStringAscent() {
return fontMetrics.getAscent(); }
public int getWidth() {
return width; }
// À§·Î
public int getHeight() {
return height; }
// Canvas resizing methods
public Dimension preferredSize()
{ return minimumSize(); }
public synchronized Dimension minimumSize()
{ return minSize; }
}
// À§·Î
// ## JAVANOID MOVING OBJECT #################################
// A javanoid moving object is
an object that moves by itself. It has dX
// & dY values that give its
movement in relative coordinates
// Initial & current movement
of the object
protected int dX0, dY0;
protected int dX, dY;
// Constructor
public jnmovingobject(jnbuffer c,
Image i, int w, int h, int x, int y, int dx, int dy) {
super(c, i, w, h, x, y);
dX0 = dx; dY0 = dy;
dX = dx; dY = dy;
}
// À§·Î
public void die() {
dX = 0; dY = 0; super.die(); }
//
Object movement settings
public void setX(int x) {
if (x > maxX)
{ X = maxX; invertDX(); }
else if
(x < minX) {X = minX; invertDX();
}
else X = x;
}
// À§·Î
public void setY(int y) {
if (y > maxY)
die();
else if (y < minY) { Y = minY; invertDY();
}
else Y = y;
}
public void setDXDY(int dx, int
dy) { dX = dx; dY = dy; }
public void setDX(int dx) {
dX = dx; }
// À§·Î
public void setDY(int dy) {
dY = dy; }
public void invertDXDY() {
dX = - dX; dY = - dY; }
public void invertDX() {
dX = - dX; }
public void invertDY() {
dY = - dY; }
// À§·Î
public void reset() {
dX = dX0; dY = dY0; super.reset(); }
//
Object life : move + drawing (dies when at the bottom of the screen)
public void live() {
if (alive) {
setX(X + dX);
setY(Y + dY);
draw();
}
}
// À§·Î
//
Object requests
public int getDX() {
return dX; }
public int getDY() {
return dY; }
}
// À§·Î