⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bluepadcanvas.java

📁 索爱的蓝牙例程源程序
💻 JAVA
字号:
//------------------------------------------------------------------
//  ____                      _____      _                           
// / ___|  ___  _ __  _   _  | ____|_ __(_) ___ ___ ___  ___  _ __   
// \___ \ / _ \| '_ \| | | | |  _| | '__| |/ __/ __/ __|/ _ \| '_ \  
//  ___) | (_) | | | | |_| | | |___| |  | | (__\__ \__ \ (_) | | | | 
// |____/ \___/|_| |_|\__, | |_____|_|  |_|\___|___/___/\___/|_| |_| 
//                    |___/                                          
//------------------------------------------------------------------
// Copyright (c) 2003 Sony Ericsson Mobile Communications AB
//                    Research Trinagle Park, NC 27709
//  
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
//------------------------------------------------------------------
package example.bluetooth;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;

/**
 * The BluePad Canvas extends canvas and implements support for 
 * the drawing methods. This MIDlet creates a canvas which is a 
 * scratch pad or white board when connected to another phone running 
 * this same MIDlet, anything you draw on one screen will automatically 
 * appear on the other screen. The mobile phone must support touch 
 * screen and the BlueTooth API.
 *
 * @author Paul H. Nichols
 * @version 1.0
 */
public class BluePadCanvas extends Canvas {

    BluePad bluePad;
    Image offScreenImage;
    int width;
    int height;

    // PEN color - init to black.
    int pen_r = 0;
    int pen_g = 0;
    int pen_b = 0;

    Image connectImage;
    Image connectingImage;
    Image disconnectImage;

    /**
     * Keep track of previous pointer position.
     */
    private int previous_x, previous_y;
    private boolean modeErase = false;

    private static final int ERASE_WIDTH = 12;
    private static final int ERASE_HEIGHT = 12;
    private static final int ERASE_HALF_WIDTH = ERASE_WIDTH/2;
    private static final int ERASE_HALF_HEIGHT = ERASE_HEIGHT/2;

    private int iconWidth = 16;
    private int iconHeight = 16;

    public BluePadCanvas(BluePad pad)
    {
        bluePad = pad;
        width = getWidth();
        height = getHeight();

        // Create off-screen image and paint it white.
        offScreenImage = Image.createImage(width , height);
        clearScreen(false);

        // Create connect icon images
        try {
          connectImage = Image.createImage("/images/connected.png");
          disconnectImage = Image.createImage("/images/disconnected.png");
          connectingImage = Image.createImage("/images/connecting.png");
          iconHeight = connectImage.getHeight();
          iconWidth = connectImage.getWidth();
        }
        catch (IOException e) {
            bluePad.printString("Exception: " + e);
        }
    }

    /**
     * Paint the offScreen image onto the canvas.
     */
    protected void paint(Graphics g) 
    {
        g.drawImage(offScreenImage, 0, 0, Graphics.TOP|Graphics.LEFT);

        // Draw status icon in upper left-hand corner based on state.
        if (bluePad.getState() == bluePad.CONNECT && connectImage != null) {
            g.drawImage(connectImage, 0, 0, Graphics.TOP|Graphics.LEFT);
        }
        else if (bluePad.getState() == bluePad.CONNECTING && connectingImage != null) {
            g.drawImage(connectingImage, 0, 0, Graphics.TOP|Graphics.LEFT);
        }
        else if (disconnectImage != null) {
            g.drawImage(disconnectImage, 0, 0, Graphics.TOP|Graphics.LEFT);
        }
    }

    protected void hideNotify()
    {
    }

    protected  void showNotify() 
    {
    }

    /**
     * Used to calculate bounding box.
     */
    private int paint_x, paint_y, paint_width, paint_height;

    /**
     * draw a line from x1,y1 to x2,y2
     * The color of the line is detemined by (cR, cG, cB)
     */
    public void drawLine(int x1, int y1, int x2, int y2, int cR, int cG, int cB ) {
        Graphics g = offScreenImage.getGraphics();
        g.setColor(cR, cG, cB);
        g.drawLine(x1, y1, x2, y2);

        // Calculate bounding box for line and
        // then set the area to repaint.
        if (x1 < x2) {
            paint_x = x1;
            paint_width = x2 - x1 + 1;
        }
        else {
            paint_x = x2;
            paint_width = x1 - x2 + 1;
        }

        if (y1 < y2) {
            paint_y = y1;
            paint_height = y2 - y1 + 1;
        }
        else {
            paint_y = y2;
            paint_height = y1 - y2 + 1;
        }

        repaint(paint_x, paint_y, paint_width, paint_height);
    }

    /**
     * Erase the given rectangle.
     */
    public void eraseRect(int x, int y, int w, int h) {
        Graphics g = offScreenImage.getGraphics();
        g.setColor(255, 255, 255);
        g.fillRect(x, y, w, h);
        repaint(x, y, w, h);
    }

    /**
     * Erases the canvas.
     */
    public void clearScreen(boolean repaint) {
        Graphics g = offScreenImage.getGraphics();
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, offScreenImage.getWidth(), offScreenImage.getHeight());

        if (repaint) {
            repaint();
        }
    }

    /**
     * Set the pen color for drawing on this canvas.
     */
    public void setPenColor(int cR, int cG, int cB) {
        pen_r = cR;
        pen_g = cG;
        pen_b = cB;
    }

    /**
    * Set the mode to eraser mode.
    */
    public void setEraserMode(boolean eraserMode) {
        modeErase = eraserMode;
    }

    /**
     * Repaint the Icon Status area of the canvas.
     */
    public void repaintStatusIcon() {
      repaint(0, 0, iconWidth, iconHeight);
    }


    /**
     * Called when a pointer drag event occurs.
     */
    protected  void pointerDragged(int x, int y) 
    {
        if (modeErase) {
            eraseRect(x-ERASE_HALF_WIDTH, y-ERASE_HALF_HEIGHT, ERASE_WIDTH, ERASE_HEIGHT);
            bluePad.eraseRectRemote(x-ERASE_HALF_WIDTH, y-ERASE_HALF_HEIGHT, ERASE_WIDTH, ERASE_HEIGHT);
        }
        else {
            drawLine(previous_x, previous_y, x, y, pen_r, pen_g, pen_b);
            bluePad.drawLineRemote(previous_x, previous_y, x, y, pen_r, pen_g, pen_b);
            previous_x = x;
            previous_y = y;
        }
    }

    /**
     * Called when pointer is pressed.
     */
    protected  void pointerPressed(int x, int y) 
    {
        if (modeErase) {
        }
        else {
            previous_x = x;
            previous_y = y;
        }
    }

    /**
     * Called when pointer is released.
     */
    protected  void pointerReleased(int x, int y) 
    {
        if (modeErase) {
        }
        else {
            drawLine(previous_x, previous_y, x, y, pen_r, pen_g, pen_b);
            bluePad.drawLineRemote(previous_x, previous_y, x, y, pen_r, pen_g, pen_b);
            previous_x = x;
            previous_y = y;
        }
    }

    /**
     * If the size of the screen increases, then
     * create a new off-screen image.
     */
    protected  void sizeChanged(int w, int h) 
    {
        if (w > width || h > height) {
            // We need a new image

            Image newImage = Image.createImage(w, h);
            Graphics g = newImage.getGraphics();
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, w, h);
            g.drawImage(offScreenImage, 0, 0, Graphics.TOP|Graphics.LEFT);
            offScreenImage = newImage;
        }
        width = w;
        height = h;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -