UIControl.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:8k
源码类别:
J2ME
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- /**
- *
- * @author swaroop_kumar
- */
- public abstract class UIControl {
- protected int width, height,x,y;
- protected int selectionBorderColor = -1;
- protected int borderColor = -1;
- protected int bgColor = -1;
- protected int selectionBgColor = -1;
- protected Image bgImage,selectionBgImage;
- boolean visible = true;
- boolean selected;
- boolean selectable = true;
- private ScrollableContainer parent;
- private EventManager eventManager;
- public static final int RECT_TYPE = 0;
- public static final int ROUND_RECT_TYPE = 1;
- private int bgType = RECT_TYPE;
- private static final int ARC_RADIOUS_WIDTH = 22;
- private static final int ARC_RADIOUS_HEIGHT = 22;
- UIControl storeControl;
- private int borderThickness = 1;
- protected boolean pointerDragged(int x, int y) {
- return false;
- }
- BaseTimer timer;
- protected boolean pointerReleased(int x, int y) {
- storeControl = this;
- if(getEventListener() != null)
- {
- DisplayManager.getInst().setBlockKeyPad(true);
- TimerInterface timerBase = new TimerInterface() {
- public void task(int data) {
- getEventListener().event(EventManager.FIRE_PRESSED, storeControl,null);
- DisplayManager.getInst().setBlockKeyPad(false);
- }
- };
- timer = BaseTimer.schedule(600, timerBase, 0);
- }
- return true;
- }
- public EventManager getEventListener()
- {
- return eventManager;
- }
- public void setEventManager(EventManager manager)
- {
- eventManager = manager;
- }
- public void setVisible(boolean visible) {
- this.visible = visible;
- }
- public boolean isVisible() {
- return visible;
- }
- public void setBgImage(Image bgImage) {
- this.bgImage = bgImage;
- if(bgImage != null && (width < bgImage.getWidth() || height < bgImage.getHeight()))
- {
- setWidth(bgImage.getWidth());
- setHeight(bgImage.getHeight());
- }
- }
- public Image getBgImage() {
- return bgImage;
- }
- public ScrollableContainer getParent() {
- return parent;
- }
- public void setParent(ScrollableContainer parent) {
- this.parent = parent;
- }
- public boolean isSelected() {
- return selected;
- }
- public boolean isSelectable() {
- return selectable;
- }
- public void setSelectable(boolean selectable) {
- this.selectable = selectable;
- }
- public abstract void paint(Graphics g);
- public void setPoistion(int x,int y)
- {
- setX(x);setY(y);
- }
- public void setBgType(int bgType) {
- this.bgType = bgType;
- }
- public boolean keyRepeated(int key)
- {
- return keyPressed(key);
- }
- public void paintUI(Graphics g)
- {
- if(!isVisible())
- return;
- g.translate(x, y);
- int clipX, clipY , clipWidth,clipHeight;
- clipX = g.getClipX();
- clipY = g.getClipY();
- clipWidth = g.getClipWidth();
- clipHeight = g.getClipHeight();
- g.clipRect(0, 0, width, height);
- paintBackground(g);
- paint(g);
- paintForground(g);
- g.setClip(clipX, clipY, clipWidth, clipHeight);
- g.translate(-x, -y);
- }
- public int getBorderThickness()
- {
- return this.borderThickness;
- }
- protected final void paintForground(Graphics g)
- {
- if(selected && selectionBorderColor != -1)
- {
- g.setColor(selectionBorderColor);
- for (int i = 0; i < borderThickness; i++) {
- if(bgType == ROUND_RECT_TYPE)
- {
- g.drawRoundRect(i, i, width - 1 - (i << 1), height - 1 - (i << 1), ARC_RADIOUS_WIDTH, ARC_RADIOUS_HEIGHT);
- }else{
- g.drawRect(i, i, width - 1 - (i << 1), height - 1 - (i << 1));
- }
- }
- }
- else if(borderColor != -1)
- {
- g.setColor(borderColor);
- if(bgType == ROUND_RECT_TYPE)
- {
- g.drawRoundRect(0, 0, width - 1, height - 1, ARC_RADIOUS_WIDTH, ARC_RADIOUS_HEIGHT);
- }else{
- g.drawRect(0, 0, width - 1 , height - 1);
- }
- }
- }
- protected final void paintBackground(Graphics g)
- {
- if(selected && selectionBgColor != -1)
- {
- g.setColor(selectionBgColor);
- if(bgType == ROUND_RECT_TYPE)
- {
- g.fillRoundRect(0, 0, width - 1, height - 1, ARC_RADIOUS_WIDTH, ARC_RADIOUS_HEIGHT);
- }else{
- g.fillRect(0, 0, width - 1 , height - 1);
- }
- }
- else if(bgColor != -1)
- {
- g.setColor(bgColor);
- if(bgType == ROUND_RECT_TYPE)
- {
- g.fillRoundRect(0, 0, width - 1, height - 1, ARC_RADIOUS_WIDTH, ARC_RADIOUS_HEIGHT);
- }else{
- g.fillRect(0, 0, width - 1 , height - 1);
- }
- }
- if(selectionBgImage != null && isSelected())
- {
- g.drawImage(selectionBgImage, (width - selectionBgImage.getWidth()) >> 1, (height - selectionBgImage.getHeight()) >> 1, 0);
- }
- else if(bgImage != null)
- {
- g.drawImage(bgImage, (width - bgImage.getWidth()) >> 1, (height - bgImage.getHeight()) >> 1, 0);
- }
- }
- public void setSelectionBgImage(Image selectionBgImage) {
- this.selectionBgImage = selectionBgImage;
- if(selectionBgImage != null && (width < selectionBgImage.getWidth() || height < selectionBgImage.getHeight()))
- {
- setWidth(selectionBgImage.getWidth());
- setHeight(selectionBgImage.getHeight());
- }
- }
- public int getHeight() {
- return height;
- }
- public void setHeight(int height) {
- this.height = height;
- }
- public int getWidth() {
- return width;
- }
- public void setSelected(boolean selected)
- {
- this.selected = selected;
- }
- public void setWidth(int width) {
- this.width = width;
- }
- public void showNotify()
- {
- selected = false;
- }
- public void hideNotify()
- {
- selected = false;
- }
- public boolean keyPressed(int keycode)
- {
- int gameKey = DisplayManager.getInst().getGameAction(keycode);
- if(getEventListener() != null && (gameKey == KeyEvent.KEY_SOFT_CENTER || keycode == KeyEvent.KEY_SOFT_CENTER || keycode == DisplayManager.KEY_NUM5 ))
- {
- getEventListener().event(EventManager.FIRE_PRESSED, this,null);
- }
- return false;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public boolean keyReleased(int keycode)
- {
- return false;
- }
- public int getBgColor() {
- return bgColor;
- }
- public void setBgColor(int bgColor) {
- this.bgColor = bgColor;
- }
- public int getBorderColor() {
- return borderColor;
- }
- public void setBorderColor(int borderColor) {
- this.borderColor = borderColor;
- }
- public int getSelectionBgColor() {
- return selectionBgColor;
- }
- public void setSelectionBgColor(int selectionBgColor) {
- this.selectionBgColor = selectionBgColor;
- }
- public Image getSelectionBgImage() {
- return selectionBgImage;
- }
- public int getSelectionBorderColor() {
- return selectionBorderColor;
- }
- public void setSelectionBorderColor(int selectionBorderColor) {
- this.selectionBorderColor = selectionBorderColor;
- }
- }