Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226835
  • 博文数量: 45
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 915
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 16:03
文章分类

全部博文(45)

文章存档

2011年(1)

2008年(44)

我的朋友

分类: Java

2008-06-06 15:03:27

采用低级界面重绘自定义按钮

举例代码如下:

import java.io.DataInputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.*;
/**
 *
 * @author Administrator
 * 在屏幕上画按钮,实现布防,利用线程实现GPRS连接
 *
 */

public class CanvasButton extends Canvas implements Runnable {
    private String strXqbh = "1";
    private String strSbbh = "010101020";
    private Alert anAlert = null;
    private Thread t = new Thread(this);
    private int end_x;
    private int end_y;
    /**
     *
     */

    protected void paint(Graphics g) {
        //清屏

        g.setColor(0x00000000);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(0x00FFFFFF);
        g.fillRoundRect(15, 20, 50, 30, 10,10);
        g.setColor(0x00FF0000);
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.FACE_SYSTEM,
                Font.SIZE_LARGE));
        g.drawString("布防", 42, 43, Graphics.HCENTER|Graphics.BOTTOM);        
        
    }
    /*
     * (非 Javadoc)
     * @see javax.microedition.lcdui.Canvas#pointerPressed(int, int)
     * 实现触幕屏GPRS通信
     */

    
    public void pointerPressed(int x, int y){
        if(x > 20 && x <50 && y>20 && y< 30 ){
            t.start();
        }
    }
    public void pointerDragged(int x, int y){
     end_x = x;
     end_y = y;
        
    }
    public void pointerReleased(int x, int y){
        end_x = x;
        end_y = y;
    }
    
    public void Set() {
        String result;
        int i = 0;
        String url ="" + strXqbh
                            + "&TerminalNO=" + strSbbh + "&Model=Set";
        try {
            HttpConnection hc = (HttpConnection) Connector
                    .open(url,Connector.READ,true);        
        
            hc.setRequestMethod(HttpConnection.GET);
            hc.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            
            DataInputStream dis = hc.openDataInputStream();
            int k;
            byte[] data = new byte[1024];
            while ((k = dis.read()) != -1) {
                data[i] = (byte) k;
                i++;
            }
            result = new String(data, 0, i);
            System.out.println("result---"+result);
            if (result.equals("success")) {                
                System.out.println("显示:布防成功!");
                //anAlert = new Alert("当前防区状态", "显示:布防成功!!", null, AlertType.INFO);

                //anAlert.setTimeout(15000);

                //Display.getDisplay(this).setCurrent(anAlert);

            } else {
                System.out.println("显示:布防失败");
                //anAlert = new Alert("当前防区状态", "显示:布防失败!!", null, AlertType.INFO);

                //anAlert.setTimeout(15000);

                //display.setCurrent(anAlert);

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("i--"+i);
        }
    }
    public void run(){
     try{
         this.Set();
     }catch(Exception ex){
         ex.printStackTrace();
     }
    }

}

阅读(2109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~