package com.ckrocket.gui; import javax.microedition.lcdui.Canvas; import java.util.Random; public abstract class TouchCanvas extends Canvas { boolean mousePressed; int dragMouseX, dragMouseY, presMouseX, presMouseY, relMouseX, relMouseY; long FPS, FPS_Count, FPS_Start; 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 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 int random (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 repaint(){ super.repaint(); sleep(40L); } }