Util.java
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:18k
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Random;
- import java.util.Vector;
- import javax.microedition.lcdui.Alert;
- import javax.microedition.lcdui.AlertType;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.rms.RecordStore;
- import javax.microedition.rms.RecordStoreException;
- import javax.microedition.rms.RecordStoreFullException;
- import javax.microedition.rms.RecordStoreNotOpenException;
- /**
- *
- * @author swaroop_kumar
- */
- public class Util {
-
- public static final int CURSOR_COLOR = 0;
- public static final int CURSOR_LENGTH = 10;
- private static Image arrowImage;
- private static Image arrowSelImage;
- private static Random random = new Random(System.currentTimeMillis());
- public static void drawCursor(Graphics g,int x,int y){
- // g.setColor(Util.CURSOR_COLOR);
- // g.drawLine(x - (CURSOR_LENGTH >> 1), y, x + (CURSOR_LENGTH >> 1), y);
- // g.drawLine(x , y - (CURSOR_LENGTH >> 1), x, y + (CURSOR_LENGTH >> 1));
- try {
- if(arrowImage == null)
- {
- arrowImage = Image.createImage("/arrow.png");
-
- }
- g.drawImage(arrowImage, x, y, 0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void drawSelectionCursor(Graphics g,int x,int y){
- try {
- if(arrowSelImage== null)
- {
- arrowSelImage = Image.createImage("/arrowSel.png");
-
- }
- g.drawImage(arrowSelImage, x, y, 0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static boolean equalsIgnoreCase(String str1,String str2)
- {
- if(str1.toUpperCase().equals(str2.toUpperCase()))
- {
- return true;
- }
- return false;
- }
- public static int searchItem(String arry[] , String item)
- {
- for (int i = 0; i < arry.length; i++) {
- if(arry[i].equals(item))
- return i;
-
- }
- return -1;
- }
- public static int getMaxCharHeight(GTantra font,String text)
- {
- int max = 0;
- for (int i = 0; i < text.length(); i++) {
- if(max < font.getCharHeight(text.charAt(i)))
- {
- max = font.getCharHeight(text.charAt(i));
- }
-
- }
- return max;
- }
-
- private static int clipX, clipY , clipWidth,clipHeight;
- public static void setClipNSave(int x, int y, int w, int h , Graphics g)
- {
- clipX = g.getClipX();
- clipY = g.getClipY();
- clipWidth = g.getClipWidth();
- clipHeight = g.getClipHeight();
- g.clipRect(x, y, w, h);
- }
- public static void restoreSetClip(Graphics g)
- {
- g.setClip(clipX, clipY, clipWidth, clipHeight);
- }
- public static Image resizeImageWithTransperency(Image image, int newWidth, int newHeight) {
-
- DisplayManager.getInst().startBusyMode();
- int width = image.getWidth();
- int height = image.getHeight();
- int argbData[] = new int[image.getWidth() * image.getHeight()];
- image.getRGB(argbData,0,image.getWidth(),0,0, image.getWidth(), image.getHeight());
- int out[] = new int[newWidth*newHeight];
- for (int yy = 0; yy < newHeight; yy++) {
- int dy = yy * height / newHeight;
- for (int xx = 0; xx < newWidth; xx++) {
- int dx = xx * width / newWidth;
- out[(newWidth*yy)+xx]=argbData[(width*dy)+dx];
- }
- }
- DisplayManager.getInst().stopBusyMode();
- return Image.createRGBImage(out, newWidth, newHeight, true);
- }
- public static Image resizeImage(Image sourceImage, int imgWidth, int imgHeight) {
- if(sourceImage.getWidth() == imgWidth && sourceImage.getHeight() == imgHeight)
- return sourceImage;
- int width = sourceImage.getWidth();
- int height = sourceImage.getHeight();
- Image img = Image.createImage(imgWidth, height);
- Graphics graphics = img.getGraphics();
- int ratio = (width << 16) / imgWidth;
- int position = ratio / 2;
- for (int i = 0; i < imgWidth; i++) {
- graphics.setClip(i, 0, 1, height);
- graphics.drawImage(sourceImage, i - (position >> 16), 0, Graphics.LEFT | Graphics.TOP);
- position += ratio;
- }
- Image finalImage = Image.createImage(imgWidth, imgHeight);
- graphics = finalImage.getGraphics();
- ratio = (height << 16) / imgHeight;
- position = ratio / 2;
- for (int j = 0; j < imgHeight; j++) {
- graphics.setClip(0, j, imgWidth, 1);
- graphics.drawImage(img, 0, j - (position >> 16), Graphics.LEFT | Graphics.TOP);
- position += ratio;
- }
- return finalImage;
- }
- /**
- * Spliting the string according to the width without breaking the word except
- * when there is a big word which does not fit in the width available
- * @param str the text to be split.
- * @param font the text font
- * @param width width in which string is to be fit
- * @return splited string
- */
- public static String[] divideString(String str, GTantra font, int width) {
- Vector vector = new Vector();
- LineEnumeration emu = new LineEnumeration(font,str,width);
- vector.addElement(str);
- while(emu.hasMoreElements())
- {
- vector.addElement(emu.nextElement());
- }
- String data[] = new String[vector.size()];
- for (int i = 0; i < vector.size(); i++) {
- data[i] = vector.elementAt(i).toString();
- }
- return data;
-
- }
- public static void fillTriangle(Graphics _g, int _x, int _y, int _width, int direction ) {
- int stroke = _g.getStrokeStyle();
- _g.setStrokeStyle(Graphics.SOLID);
- for (int i = 0; i < _width >> 1; i++) {
- switch (direction) {
- // previous
- case 0:
- _g.drawLine(_x + i, _y - i, _x + i, _y + i);
- break;
- //next
- case 1:
- _g.drawLine(_x - i, _y - i, _x - i, _y + i);
- break;
- // up
- case 2:
- _g.drawLine(_x - i, _y + i, _x + i, _y + i);
- break;
- // down
- case 3:
- _g.drawLine(_x - i, _y - i, _x + i, _y - i);
- break;
- }
- }
- _g.setStrokeStyle(stroke);
- }
- /**
- * Draws filled triangle
- * @param startPtX X coordinate of starting point of the triangle
- * @param startPtY Y coordinate of starting point of the triangle
- * @param arrowDirection decides the direction of arrow left/right/up/down
- * @param arrowWidth size of the arrow
- * @param horizontal decides the alignment of arrow horizontal/vertical
- */
- public static void drawFilledTriangle(Graphics g, int startPtX, int startPtY, boolean arrowDirection, int arrowWidth, boolean horizontal) {
- int wdth = arrowWidth;
- if (horizontal) {
- /**horizontal arrow is to be drawn*/
- int i = 0;
- boolean var = true;
- while (var == true) {
- /**left arrow is to be drawn*/
- if (arrowDirection == true) {
- g.drawLine(startPtX - i, startPtY - i, startPtX - i, wdth + i);
- /**right arrow is to be drawn*/
- } else {
- g.drawLine(startPtX + i, startPtY - i, startPtX + i, wdth + i);
- }
- if ((startPtY - i) - (wdth + i) <= 1) {
- var = false;
- }
- i++;
-
- }
- } else {
- /**vertical arrow is to be drawn*/
- int i = 0;
- boolean var = true;
- while (var == true) {
- /**up arrow is to be drawn*/
- if (arrowDirection == true) {
- g.drawLine(startPtX + i, startPtY - i, startPtX + wdth - i, startPtY - i);
- /**down arrow is to be drawn*/
- } else {
- g.drawLine(startPtX + i, startPtY + i, startPtX + wdth - i, startPtY + i);
- }
- if (wdth - i == arrowWidth >> 1) {
- var = false;
- }
- i++;
- }
- }
- }
- public static String getEllipsisString(String str,int width,GTantra font)
- {
- if(str == null)
- return null;
- if(font.getStringWidth(str) > width )
- {
-
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < str.length(); i++) {
- if( font.getStringWidth(buffer.toString()) + font.getCharWidth(str.charAt(i)) < width)
- {
- buffer.append(str.charAt(i));
- }
- else
- break;
- }
- if(buffer.toString().length() <= 3)
- {
- buffer.delete(0, buffer.length() -1);
- buffer.append("...");
- }else
- {
- buffer.delete(buffer.length() - 3, buffer.length() );
- buffer.append("...");
- }
- return buffer.toString();
- }
- else
- return null;
- }
- public static final String months[] = {"JANUARY","FEBRUARY","MARCH","APRIL","MAY" ,"JUNE","JULY" ,"AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
- public static Date getCurrentDate()
- {
- return cal.getTime();
- }
- public static int getCurrentDay()
- {
-
- return cal.get(Calendar.DAY_OF_MONTH);
- }
- public static int getCurrentMonth()
- {
-
- return cal.get(Calendar.MONTH);
- }
- public static String getCurrentMonthString()
- {
-
- return months[cal.get(Calendar.MONTH)].toUpperCase();
- }
- public static final int getCurrentYear()
- {
-
- return cal.get(Calendar.YEAR);
- }
- public static final long MILLIES_IN_DAY = 24*60*60*1000;
- static Calendar cal = Calendar.getInstance();
- public static final int getNextDay(long time)
- {
- Date savedDate = cal.getTime();
- Date date = new Date(time);
- cal.setTime(date);
- int day = getCurrentDay();
- cal.setTime(savedDate);
- return day;
- }
- public static final int getNextMonth(long time)
- {
- Date savedDate = cal.getTime();
- Date date = new Date(time);
- cal.setTime(date);
- int month = getCurrentMonth();
- cal.setTime(savedDate);
- return month;
- }
- public static void printMilliesInfos(long tmp)
- {
- System.out.println("Day:"+Util.getNextDay(tmp));
- System.out.println("month:"+Util.getNextMonth(tmp));
- System.out.println("year:"+Util.getNextYear(tmp));
- System.out.println("hour:"+Util.getNextHour(tmp));
- System.out.println("minute:"+Util.getNextMin(tmp));
- }
- public static final int getNextMin(long time)
- {
- Date savedDate = cal.getTime();
- Date date = new Date(time);
- cal.setTime(date);
- int min = cal.get(Calendar.MINUTE);
- cal.setTime(savedDate);
- return min;
- }
- public static final int getNextHour(long time)
- {
- Date savedDate = cal.getTime();
- Date date = new Date(time);
- cal.setTime(date);
- int hour = cal.get(Calendar.HOUR_OF_DAY);
- cal.setTime(savedDate);
- return hour;
- }
- public static final int getNextYear(long time)
- {
- Date savedDate = cal.getTime();
- Date date = new Date(time);
- cal.setTime(date);
- int year = getCurrentYear();
- cal.setTime(savedDate);
- return year;
- }
- public static final String getNextMonthString(long time)
- {
- Date savedDate = cal.getTime();
- Date date = new Date(time);
- cal.setTime(date);
- String month = getCurrentMonthString();
- cal.setTime(savedDate);
- return month;
- }
- public static boolean isValidDay(String day)
- {
- try {
- int dayValue = Integer.parseInt(day);
- if(dayValue <= 0 || dayValue > 31)
- return false;
- else
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- public static boolean isValidMonth(String month)
- {
- try {
- int dayValue = Integer.parseInt(month);
- if(dayValue <= 0 || dayValue > 12)
- return false;
- else
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- public static void showInfoAlert(String str)
- {
- Alert alert = new Alert("Information",str,null,AlertType.INFO);
- alert.setTimeout(Alert.FOREVER);
- FrameWorkMidlet.getDisplay().setCurrent(alert, DisplayManager.getInst());
- }
- public static void showErrorAlert(String str)
- {
- Alert alert = new Alert("Error",str,null,AlertType.ERROR);
- alert.setTimeout(Alert.FOREVER);
- FrameWorkMidlet.getDisplay().setCurrent(alert, DisplayManager.getInst());
- }
- public static int getMaxDayOfMonth(int month,int year)
- {
- int maxDays = 30;
- switch(month){
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- maxDays = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- maxDays = 30;
- break;
- case 2:
- if((year%4 == 0 && year%100 != 0)||(year%400==0)){
- maxDays = 29;
- }
- else{
- maxDays = 28;
- }
- break;
- }
- return maxDays;
- }
- public static boolean isValidDate(int day,int month,int year)
- {
- int totalDays = getMaxDayOfMonth(month, year);
- if(totalDays < day)
- return false;
- else
- return true;
- }
- public static long getDateMilis(int year, int month, int day) {
- Calendar c = Calendar.getInstance();
- Date actualDate = c.getTime();
- c.set(Calendar.YEAR, year);
- c.set(Calendar.MONTH, month);
- c.set(Calendar.DATE, day);
- Date date = c.getTime();
- c.setTime(actualDate);
- return (date.getTime());
- }
- /**
- * Get date in miliseconds with respect to 1st Jan 2009 00:00:00
- * @param year
- * @param month
- * @param day
- * @param hour
- * @param minutes
- * @return Miliseconds
- */
- public static long getDateMilis(int year, int month, int day, String hoursMins) {
- int hour = Integer.parseInt(hoursMins.substring(0, hoursMins.indexOf(":")));
- hoursMins = hoursMins.substring(hoursMins.indexOf(":") + 1, hoursMins.length());
- int minutes = Integer.parseInt(hoursMins);
- Calendar c = Calendar.getInstance();
- Date actualDate = c.getTime();
- c.set(Calendar.YEAR, year);
- c.set(Calendar.MONTH, month);
- c.set(Calendar.DATE, day);
- c.set(Calendar.HOUR_OF_DAY, hour);
- c.set(Calendar.MINUTE, minutes);
- c.set(Calendar.SECOND, 0);
- c.set(Calendar.MILLISECOND, 0);
- Date date = c.getTime();
- c.setTime(actualDate);
- return (date.getTime());
- }
- public static String[] split(String str,String dele)
- {
- str = new String(str.trim());
- Vector vect = new Vector();
- int index = 0;
- while((index = str.indexOf(dele))!= -1)
- {
- vect.addElement(str.substring(0, index));
- index += dele.length();
- str = str.substring(index, str.length());
- }
- vect.addElement(str);
- String tmp[] = new String[vect.size()];
- for (int i = 0; i < vect.size(); i++) {
- tmp[i] = vect.elementAt(i).toString();
- if( (index = tmp[i].indexOf("##"))!= -1)
- {
- StringBuffer buffer = new StringBuffer(tmp[i]);
- buffer.deleteCharAt(index);
- buffer.deleteCharAt(index);
- buffer.insert(index , " ");
- tmp[i] = buffer.toString();
- }
- }
- return tmp;
- }
- /**
- * Write the given string into log file of application.
- * @param str String which is to be write in log file of application.
- */
- public static RecordStore store = null;
- public static void loging(String str)
- {
-
- byte[] by=str.getBytes();
- try {
- if(store == null)
- store=RecordStore.openRecordStore("LogReader", true);
- store.addRecord(by, 0, by.length);
- } catch (RecordStoreNotOpenException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (RecordStoreFullException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (RecordStoreException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * will return random number including min and max value
- * @param min
- * @param max
- * @return
- */
- public static int getRandomNumber(int min,int max)
- {
- int num = Math.abs(random.nextInt());
- return min + (num % (max - min + 1));
- }
- public static boolean isInRect(UIControl control,int x,int y)
- {
- return isInRect(control.getX() , control.getY() , control.getWidth() , control.getHeight() , x, y);
- }
- public static boolean isInRect(int xPos,int yPos, int width,int height , int x,int y )
- {
- if(x >= xPos && x <= xPos + width && y >= yPos && y <= yPos + height)
- {
- return true;
- }
- return false;
- }
- public static int getApproxDistance( int x1, int y1,int x2, int y2 )
- {
- int dx = Math.abs(x1 - x2);
- int dy = Math.abs(y1 - y2);
- int min, max;
- if ( dx < dy )
- {
- min = dx;
- max = dy;
- } else {
- min = dy;
- max = dx;
- }
- // coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min )
- return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) +
- ( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 );
- }
- }