📄 tools.java
字号:
package game;
import javax.microedition.lcdui.Image;
import java.io.*;
import javax.microedition.lcdui.Graphics;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Font;
public class Tools {
Random rand;
MyCanvas mc;
public Tools(MyCanvas mc) {
this.rand = new Random();
this.mc = mc;
}
/**
* 得到图片对象
* @param name String
* @return Image
*/
public Image getImage(String name)
{
Image img = null;
try {
img = Image.createImage(name);
} catch (IOException ex) {
// System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
}
return img;
}
/**
*
* @param img Image 图片
* @param g Graphics 画笔
* @param x int 要画在什么地方
* @param y int要画在什么地方 坐标
* @param clipx int 图片上的坐标
* @param clipy int
* @param frameWidth int 宽
* @param frameHeight int 高
* @param screenWidth int 屏幕的款
* @param screenHeight int 屏幕的高
*/
public void drawPartImage(Graphics g,Image img,int x,int y,int clipx,int clipy,int frameWidth,int frameHeight)
//1351390244
{
g.setClip(x,y,frameWidth,frameHeight);
g.drawImage(img,x-clipx,y-clipy,g.TOP|g.LEFT);
g.setClip(0,0,mc.cw,mc.ch);
}
//随即函数
public int getRandomNumber(int min,int max)
{
int t ;
if(min>max)
{
t = min;
min = max;
max = t;
}
return Math.abs(rand.nextInt()%(max-min+1))+min;
}
public boolean collisonRC(int row2,int col2,int row3,int col3)
{
return (row2==row3)&&(col2==col3);
}
//碰撞
public boolean inRect(int x1,int y1,int w1 ,int h1,int x2,int y2,int w2,int h2)
{
return !((x1+w1<x2)||(y1+h1<y2)||(x2+w2<x1)||(y2+h2<y1));
}
/**
* 画按右下钮
* @param g Graphics
* @param name String
* @param mc MyCanvas
*/
public static void drawRightButton(Graphics g,String name,MyCanvas mc){
mc = mc;
g.drawString(name,mc.cw,mc.ch,g.BOTTOM | g.RIGHT);
}
/**
* 画左下按钮
* @param g Graphics
* @param name String
* @param mc MyCanvas
*/
public static void drawLeftButton(Graphics g,String name,MyCanvas mc){
mc = mc;
g.drawString(name,0,mc.ch,g.BOTTOM | g.LEFT);
}
/**
* 水平翻转
* @param g Graphics
* @param img Image
* @param x int
* @param y int
*/
public void drawHOverturn(Graphics g,Image img,int x,int y)
{
int width = img.getWidth();
int height = img.getHeight();
for (int i = 0; i < width; i++) {
g.setClip(x + i, y, 1, height);
g.drawImage(img, x - width + 2 * i, y, g.TOP | g.LEFT);
}
g.setClip(0,0,mc.cw,mc.ch);
}
/**
*垂直翻转
* @param g Graphics
* @param img Image
* @param x int
* @param y int
*/
public void drawVOverturn(Graphics g,Image img,int x,int y)
{
int width = img.getWidth();
int height = img.getHeight();
for (int i = 0; i < height; i++) {
g.setClip(x , y+i, width, 1);
g.drawImage(img, x , y- height + 2 * i, g.TOP | g.LEFT);
}
g.setClip(0,0,mc.cw,mc.ch);
}
//画横着的图片
public void drawImageNumber(Graphics g, int number, Image img, int x, int y,
int frameWidth, int frameHeight
) {
String strNumber = number + "";
for (int i = 0; i < strNumber.length(); i++) {
int num = Integer.parseInt(strNumber.charAt(i) + "");
this.drawPartImage(g, img, x + i * frameWidth, y, num * frameWidth,
0,
frameWidth, frameHeight);
}
}
// 获得半透明图片,透明度从0到10共分为11个等级 midp 2.0
public static final Image alfImage(Image img,int alf){
if(img == null){
// System.out.println("alfImage");
return null;
}
if(alf < 0)
alf = 0;
else if(alf > 10)
alf = 10;
int imgW = img.getWidth();
int imgH = img.getHeight();
int[] RGBData = new int[imgW*imgH];
img.getRGB(RGBData,0,imgW,0,0,imgW,imgH);
int tmp = ((alf*255/10) << 24)|0x00ffffff;
for(int i=0;i<RGBData.length;i++)
RGBData[ i ] &= tmp;
Image o_Img = Image.createRGBImage(RGBData,imgW,imgH,true);
return o_Img;
}
/**
* 在制定矩形内,绘制折行文本,超出矩形范围的字符不显示
* @param src 文本字串
* @param g Graphics对象
* @param x 矩形左上角x坐标
* @param y 矩形左上角y坐标
* @param w 矩形宽度
* @param h 矩形高度
* @param leftMargin 左边矩
* @param topMargin 上边距
* @param isVTight 行间距是否紧凑
* @param fontColor 文字颜色
* @param font 绘制所用字体
*/
public static final void drawWrapString(String src, Graphics g, int x,
int y, int w,
int h, int leftMargin,
int topMargin,
int fontColor) {
g.setColor(fontColor);
int count = src.length();
int curCharIndex = 0;
int curX = x + leftMargin;
int curY = y + topMargin;
Font font=Font.getDefaultFont();
int vDelta = font.getHeight();
// if (isVTight) {
// //从上边缘到下边缘的高
// vDelta = font.getBaselinePosition();
// }
while (curCharIndex < count) {
char c = src.charAt(curCharIndex);
if (c == '\n') {
curX = x + leftMargin;
curY += vDelta;
curCharIndex++;
continue;
} else if (c == '\t') {
// 相当于2个空格
curX += font.charWidth(' ') * 2;
curCharIndex++;
continue;
}
if (curX + font.charWidth(c) < x + w) {
g.drawChar(c, curX, curY, Graphics.LEFT | Graphics.TOP);
curCharIndex++;
curX += font.charWidth(c);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -