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

📄 sharecanvas.java

📁 一个电子白版程序,J2me程序,注意,使用EC开发,要安装WTK和其插件才能使用
💻 JAVA
字号:
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Image;

import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;

import java.io.InputStream;
import java.util.Vector;
/*
 * Created on 2006-2-18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author blrc
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ShareCanvas extends Canvas implements CommandListener {

	private Command INVITE_COMMAND;
	private Command CANCEL_COMMAND;//to be added
	
	public Alert inviteAlert;
	
	MainMIDlet adaptee;
	SocketConnection currentConnection;
	
	public Vector me;
	public Vector other;
	
	private int meNum;
	
	public Reader reader;
	public Writer writer;
	
	Dimension fristPoint,secondPoint;
	
	public boolean sharing;
	
	Image mymap;	
	
	public ShareCanvas(MainMIDlet adaptee) {
		this.adaptee = adaptee;
		me = new Vector();
		other = new Vector();
		meNum = 0;
		sharing = false;
		inviteAlert = new Alert("info:");//,"invite has been sent",null,AlertType.INFO);
		inviteAlert.setType(AlertType.INFO);
		INVITE_COMMAND = new Command("launch a share",Command.SCREEN,0);
		CANCEL_COMMAND = new Command("cancel a share",Command.SCREEN,0);
		addCommand(INVITE_COMMAND);
		setCommandListener(this);	
		
		try
		{
			mymap = Image.createImage("/mymap.png");
		}
		catch (Exception e)
		{
			System.err.println("Fail to load image mymap.png");
		}
	}

	
	protected void paint(Graphics g) {
//		g.setColor(255,255,188);
//		g.fillRect(0,0,getWidth(),getHeight());
//		g.setColor(0,0,0);
		g.drawImage(mymap,0,0,Graphics.TOP|Graphics.LEFT);
		if (sharing)
		{
			int num = me.size();
			if (num > 0)
			{
				fristPoint = (Dimension)me.elementAt(0);
				for (int i = 1; i < num; i++)
				{
					secondPoint = (Dimension)me.elementAt(i);
					if (fristPoint.x == -2 && fristPoint.y == -2) ;
					else if (secondPoint.x == -2 && secondPoint.y == -2);
					else 
					{
						g.drawLine(fristPoint.x,fristPoint.y,secondPoint.x,secondPoint.y);
					}
					fristPoint = secondPoint;
				}
			}
			
			num = other.size();
			if (num > 0)
			{
				fristPoint = (Dimension)other.elementAt(0);
				for (int i = 1; i < num; i++)
				{
					secondPoint = (Dimension)other.elementAt(i);
					if (fristPoint.x == -2 && fristPoint.y == -2);
					else if(secondPoint.x == -2 && secondPoint.y == -2);
					else 
					{
						g.drawLine(fristPoint.x,fristPoint.y,secondPoint.x,secondPoint.y);
					}
					fristPoint = secondPoint;
				}
			}
		}
		
	}
	
	public void commandAction(Command c, Displayable d) {
		if (c == INVITE_COMMAND)
		{
			SocketThread socketThread = new SocketThread(adaptee,this);
			socketThread.start();			
		}
		else if (c == CANCEL_COMMAND)
		{
			writer.cancel();
			removeCommand(CANCEL_COMMAND);
		    addCommand(INVITE_COMMAND);
		}

	}
	
	public void set_currentConnection(SocketConnection currentConnection)
	{
		this.currentConnection = currentConnection; 
	}
	
	public void accept_invite()
	{
		try
		{
			OutputStream os = currentConnection.openOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF("YES");
			dos.flush();
			System.out.println("writeUTF YES to accept invite");
		    reader = new Reader(this,currentConnection);
		    reader.start();
		    writer = new Writer(this,currentConnection,os,dos);
		    writer.start();
		    Singal.ReaderAlive = true;
		    Singal.WriterAlive = true;
		    System.out.println("and start reader & writer thread");
		    sharing = true;
		    removeCommand(INVITE_COMMAND);
		    addCommand(CANCEL_COMMAND);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}	
	
	protected void pointerPressed(int x, int y)
	{
		if (sharing)
		{
			Dimension dimension = new Dimension();
			dimension.x = x;
			dimension.y = y;
			me.addElement(dimension);
			meNum++;
			if (meNum % 5 == 0)
			{
				writer.set_currentNum(meNum);
				System.out.println("notify writer");
				writer.a_notifyAll();
			}
		}		
	}

	protected void pointerDragged(int x, int y)
	{
		if (sharing)
		{
			Dimension dimension = new Dimension();
			dimension.x = x;
			dimension.y = y;
			me.addElement(dimension);
			meNum++;
			if (meNum % 5 == 0)
			{
				writer.set_currentNum(meNum);
				System.out.println("notify writer");
				writer.a_notifyAll();
			}
			repaint();
		}		
	}

	protected void pointerReleased(int x, int y)
	{
		if (sharing)
		{
			Dimension dimension = new Dimension();
			dimension.x = -2;
			dimension.y = -2;
			me.addElement(dimension);
			meNum++;
			if (meNum % 5 == 0)
			{
				writer.set_currentNum(meNum);
				System.out.println("notify writer");
				writer.a_notifyAll();
			}	
		}		
	}	
	
	public void cancelWriter()
	{
		writer.cancel();
	}
	
	public void adjustCommand()
	{
		removeCommand(INVITE_COMMAND);
	    addCommand(CANCEL_COMMAND);
	}
	
	public void revertCommand()
	{
		removeCommand(CANCEL_COMMAND);
	    addCommand(INVITE_COMMAND);
	}
	
	public void set_share(boolean bool)
	{
		sharing = bool;
		if (sharing == false) repaint();
		adaptee.revert();
	}
	
	public void showByeGUI()
	{
		inviteAlert.setString("Your friend close the connention");
		inviteAlert.setTimeout(Alert.FOREVER);
		adaptee.display.setCurrent(inviteAlert);
	}
}

class SocketThread extends Thread
{
	MainMIDlet adaptee;
	ShareCanvas parent;
	public SocketThread(MainMIDlet adaptee,ShareCanvas parent)
	{
		super();	
		this.adaptee = adaptee;
		this.parent = parent;
	}
	
	public void run()
	{
		try
		{
			SocketConnection sc = (SocketConnection)Connector.open("socket://135.252.6.31:8000");
			System.out.println("start socket client");
			parent.inviteAlert.setString("invite has been sent");
			parent.inviteAlert.setTimeout(2000);
			adaptee.display.setCurrent(parent.inviteAlert);
			InputStream is  = sc.openInputStream();
			DataInputStream dis = new DataInputStream(is);
			String response = dis.readUTF();
			System.out.println("SocketThread connect server, and receive response:" + response);
			if (response.equals("NO"))
			{
				parent.inviteAlert.setString("your invite be rejected!");
				parent.inviteAlert.setTimeout(Alert.FOREVER);
				dis.close();
				is.close();
				sc.close();
				adaptee.display.setCurrent(parent.inviteAlert);
			}
			else if (response.equals("YES"))
			{
				parent.inviteAlert.setString("your invite be accepted!");
				parent.inviteAlert.setTimeout(Alert.FOREVER);
				parent.set_currentConnection(sc);
				parent.reader = new Reader(parent,sc,is,dis);
				parent.reader.start();
				parent.writer = new Writer(parent,sc);
				parent.writer.start();	
				Singal.ReaderAlive = true;
			    Singal.WriterAlive = true;
				sc = null;
				parent.sharing = true;
				adaptee.display.setCurrent(parent.inviteAlert);
				parent.adjustCommand();
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
}

⌨️ 快捷键说明

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