package com.ckrocket.util; import java.util.Random; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Image; import java.io.DataInputStream; public abstract class TouchCanvas extends Canvas { public boolean mousePressed; public int dragMouseX, dragMouseY, presMouseX, presMouseY, relMouseX, relMouseY; public int presKeyCode, dragKeyCode, relKeyCode; public boolean pressKey; public long FPS, FPS_Count, FPS_Start; public void keyPressed(int keyCode){ presKeyCode = keyCode; pressKey = true; sleep(100); } public void keyDragged(int keyCode){ dragKeyCode = keyCode; } public void keyReleased(int keyCode){ pressKey = false; presKeyCode = 0; } public void pointerPressed(int x, int y) { mousePressed = true; presMouseX = x; presMouseY = y; } public void pointerDragged(int x, int y) { dragMouseX = x; dragMouseY = y; } public void pointerReleased(int x, int y) { mousePressed = false; relMouseX = x; relMouseY = y; } public String readText(String path) { DataInputStream dis = new DataInputStream(getClass().getResourceAsStream(path)); StringBuffer strBuff = new StringBuffer(); int ch = 0; try { while ((ch = dis.read()) != -1) { strBuff.append((char ) ch); } dis.close(); } catch (Exception e) { System.err.println("ERROR in readText() " + e); } return strBuff.toString(); } public Image resize(Image img, int newWidth, int newHeight) { int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); int[] arrayOld = new int[imageWidth * imageHeight]; int[] arrayNew = new int[newWidth * newHeight]; img.getRGB(arrayOld, 0, imageWidth, 0, 0, imageWidth, imageHeight); for (int y = 0; y < newHeight; y++) { for (int x = 0; x < newWidth; x++) { arrayNew[x + newWidth * y] = arrayOld[x * imageWidth / newWidth + imageWidth * (y * imageHeight / newHeight)]; } } return Image.createRGBImage(arrayNew, newWidth, newHeight, true); } public int dist(int x1, int y1, int x2, int y2) { int dist = (int) (Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))); return dist; } public String getFPS() { FPS_Count++; if (FPS_Start == 0) { FPS_Start = System.currentTimeMillis(); } if (System.currentTimeMillis() - FPS_Start >= 1000) { FPS = FPS_Count; FPS_Count = 0; FPS_Start = System.currentTimeMillis(); } return Long.toString(FPS); } public boolean pointCross(int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) { int x = crossPointX(x1, x2, x3, x4, y1, y2, y3, y4); int y = crossPointY(x1, x2, x3, x4, y1, y2, y3, y4); boolean point = x1 <= x && x2 >= x && x3 <= x && x4 >= x && y1 <= y && y2 >= y && y3 <= y && y4 >= y; return point; } public int crossPointX(int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) { int x = (int) (0 - ((x1 * y2 - x2 * y1) * (x4 - x3) - (x3 * y4 - x4 * y3) * (x2 - x1)) / ((y1 - y2) * (x4 - x3) - (y3 - y4) * (x2 - x1))); return x; } public int crossPointY(int x1, int x2, int x3, int x4, int y1, int y2, int y3, int y4) { int x = crossPointX(x1, x2, x3, x4, y1, y2, y3, y4); int y = (int) (((y3 - y4) * x - (x3 * y4 - x4 * y3)) / (x4 - x3)); return y; } public static int random(int min, int max, int dim) { return min + new Random().nextInt((int)((max-min)/dim)) * dim; } public int randomSmooth(int a, int b) { Random r = new Random(); return a + ((r.nextInt() >>> 1) % (b - a)); } public void sleep(long sleep) { try { Thread.sleep(sleep); } catch (Exception e) { e.printStackTrace(); } } public void repaints() { super.repaint(); sleep(40L); } }