📄 tools.java
字号:
/**
* A bunch of static tools
* @author Martin J. Wells
*/
//#ifdef nokia
//# import com.nokia.mid.ui.DeviceControl;
//#endif
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.util.Random;
public final class Tools
{
/**
* Commonly used so we precalc it.
*/
public static final int GRAPHICS_TOP_LEFT = Graphics.LEFT | Graphics.TOP;
private static final Random randomizer = new Random();
/**
* A method to obtain a random number between a miniumum and maximum
* range.
* @param min The minimum number of the range
* @param max The maximum number of the range
* @return
*/
public static final int getRand(int min, int max)
{
int r = Math.abs(randomizer.nextInt());
return (r % ((max - min + 1))) + min;
}
/**
* Test for a collision between two rectangles using plane exclusion.
* @return True if the rectangle for from the b coordinates intersects those
* of a.
*/
public static final boolean isIntersectingRect(int ax, int ay,
int aw, int ah,
int bx, int by,
int bw, int bh)
{
if (by + bh < ay || // is the bottom b above the top of a?
by > ay + ah || // is the top of b below bottom of a?
bx + bw < ax || // is the right of b to the left of a?
bx > ax + aw) // is the left of b to the right of a?
return false;
return true;
}
/**
* Returns true if rectangle b is wholly within rectangle a
*/
public static final boolean isRectWithinRect(int ax, int ay, int aw, int ah,
int bx, int by, int bw, int bh)
{
if (isPointInRect(bx, by, ax, ay, aw, ah) &&
isPointInRect(bx + bw, by, ax, ay, aw, ah) &&
isPointInRect(bx, by + bh, ax, ay, aw, ah) &&
isPointInRect(bx + bw, by + bh, ax, ay, aw, ah))
return true;
return false;
}
public static final boolean isPointInRect(int px, int py, int x, int y, int w, int h)
{
if (px < x) return false;
if (px > x + w) return false;
if (py < y) return false;
if (py > y + h) return false;
return true;
}
/**
* Take an array of existing objects and expand it's size by a given number
* of elements.
* @param oldArray The array to expand.
* @param expandBy The number of elements to expand the array by.
* @return A new array (which is a copy of the original with space for more
* elements.
*/
public final static int[] expandArray(int[] oldArray, int expandBy)
{
int[] newArray = new int[oldArray.length + expandBy];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
/**
* Take a 2D array of existing objects and expand it's size by a given number
* of elements.
* @param oldArray The array to expand.
* @param expandBy The number of elements to expand the array by.
* @return A new array (which is a copy of the original with space for more
* elements.
*/
public final static Image[][] expandArray(Image[][] oldArray, int expandBy)
{
Image[][] newArray = new Image[oldArray.length + expandBy][];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
return newArray;
}
private static Image[] smallStarImages;
private static final int MAX_STAR_WIDTH = 3;
private static final int MAX_STAR_HEIGHT = 3;
/**
* Draws a background star field using little star images (ranging in size
* from 1 to 3 pixel square; loaded from the general.png image file). The
* field is drawn as a 256 by 256 pixel map of stars using a simple
* placement method of incrementing a value to give an impression of
* distribution of stars across the map.
* @param graphics The graphics context upon which to draw.
* @param offsetX The relative x offset of the current view port.
* @param offsetY The relative y offset of the current view port.
* @param viewWidth The width of the current view port.
* @param viewHeight The height of the current view port.
*/
public final static void drawStarField(Graphics graphics,
int offsetX, int offsetY,
int viewWidth, int viewHeight)
{
// Load the static star graphics inmageset.
if (smallStarImages == null)
smallStarImages = ImageSet.extractFrames(ImageSet.loadClippedImage(
"/general.png", 0, 0), 0, 6, 4, 2, 3, 3);
// Draw the stars on the background by running through all the positions
// in the 256 by 256 star map jumping in different increments to give
// a scatterred but consistent distribution of stars.
int jumpBy = 160;
for (int i = 0; i < (256 * 256); i += jumpBy)
{
int starX = i / 256 - (offsetX);
int starY = i % 256 - (offsetY);
// Check whether the star image will fit on the current view and then
// draw a frame relative to the position in the map.
if (starX > -MAX_STAR_WIDTH && starX < viewWidth &&
starY > -MAX_STAR_HEIGHT && starY < viewHeight)
graphics.drawImage(smallStarImages[i % smallStarImages.length],
starX, starY, Graphics.TOP | Graphics.LEFT);
// Change the jump value increment to randomise the distribution
// somewhat.
jumpBy += 33;
}
}
public final static void vibrate(int strength, int time)
{
if (StarAssault.getApp().isOptionVibrate())
{
//#ifdef nokia
//# DeviceControl.startVibra(strength, time);
//#endif
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -