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

📄 textarea.java

📁 用java实现的红外线通讯源码
💻 JAVA
字号:
/*Copyright (C) 2004  Juho Vähä-HerttuaThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.*/package jmirc;import java.io.*;import java.util.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;public class TextArea {	private int left;	private int top;	private int width;	private int height;	private int fontheight;	private int position, emptylines;	private boolean scrollbar;	private String[][] scrollbuffer;	private int[][] colourbuffer;	private int bufindex;	private final int MAX_LINES;;			public TextArea(int left, int top, int width, int height, int buflines, boolean scrollbar) {		position = 0;		this.left = left;		this.top = top;		this.width = width;		this.height = height;		this.scrollbar = scrollbar;		fontheight = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL).getHeight();		MAX_LINES = buflines;		scrollbuffer = new String[MAX_LINES][];		colourbuffer = new int[MAX_LINES][];		emptylines = MAX_LINES;	}	public void setSize(int newtop, int newheight) {		boolean end = isAtEndpos();		this.top = newtop;		this.height = newheight;		if (end) setPosition(-1);		else setPosition(position);	}			// -1 is home, -2 is pageup, -3 is pagedown	public void setPosition(int pos) {		int screenlines = (height / fontheight);		int maxpos = MAX_LINES - emptylines - screenlines;		if (maxpos < 0) maxpos = 0;		if (pos == -3) updatePosition(screenlines);		else if (pos == -2) updatePosition(0-screenlines);		else if (pos < 0) position = maxpos;		else if (pos > maxpos) position = maxpos;		else position = pos;	}	public void updatePosition(int dpos) {		if (position+dpos < 0) setPosition(0);		else setPosition(position+dpos);	}	public boolean isAtEndpos() {		return (position >= MAX_LINES - emptylines - (height / fontheight));	}	private void addLine(String[] strings, int[] colours) {		scrollbuffer[bufindex] = strings;		colourbuffer[bufindex] = colours;		bufindex = (bufindex+1)%MAX_LINES;		if (emptylines > 0) emptylines--;	}	/* In the colour integer the format is following:	     lowest 4 bits indicate the foreground colour	     next 4 bits indicate the background colour	     next 3 bits indicate the font style as in Font.STYLE_* */	public void addText(String[] strings, int[] colours) {		int new_width;		String tmpline;		Font font;		Vector rets, reti;		rets = new Vector();		reti = new Vector();		font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);		new_width = 0;		for (int i=0; i<strings.length; i++) {			String line;			String[] s;			line = null;			tmpline = null;			font = Font.getFont(Font.FACE_SYSTEM, (colours[i]/256)&7, Font.SIZE_SMALL);			s = Utils.splitString(strings[i], " ");			for (int j=0; j<s.length; j++) {				// Notice that width includes now scrollbar so it's decreased here in 3 places				// using 2 pixels for scrollbar so we decrease 5 (1 pixel left, 1 right)				if (tmpline == null) {					tmpline = s[j];					if (new_width + font.stringWidth(s[j]) > width-5) {						line = "";						new_width = 0;					}				}				else if (new_width + font.stringWidth(" " + s[j]) > width-5) {					line = tmpline;					tmpline = " " + s[j];					new_width = 0;				}				else {					tmpline += " " + s[j];				}				new_width += font.stringWidth(" " + s[j]);				while (line != null) {					// we don't want to add an empty line					if (!line.equals("")) {						rets.addElement(line);						reti.addElement(new Integer(colours[i]));					}					String[] sarray = new String[rets.size()];					int[] iarray = new int[reti.size()];					for (int k=0; k<sarray.length && k<iarray.length; k++) {						sarray[k] = (String) rets.elementAt(k);							iarray[k] = ((Integer) reti.elementAt(k)).intValue();					}					addLine(sarray, iarray);					rets.removeAllElements();					reti.removeAllElements();					if (font.stringWidth(tmpline) > width-5) {						int k;						for(k=1; font.stringWidth(tmpline.substring(0, k))<width-5; k++);						line = tmpline.substring(0, k-1);						tmpline = " " + tmpline.substring(k-1);					}					else line = null;				}			}			rets.addElement(tmpline);			reti.addElement(new Integer(colours[i]));		}		if (rets.size() > 0 || reti.size() > 0) {			String[] sarray = new String[rets.size()];			int[] iarray = new int[reti.size()];			for (int i=0; i<sarray.length && i<iarray.length; i++) {				sarray[i] = (String) rets.elementAt(i);				iarray[i] = ((Integer) reti.elementAt(i)).intValue();			}			addLine(sarray, iarray);			rets.removeAllElements();			reti.removeAllElements();		}	}		public void draw(Graphics g) {		int mls = (height / fontheight); // max lines in screen		int lastcolour = 0;		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));		g.setColor(0);		// loops through every line on screen		for(int i=0; i<mls; i++) {			int leftpixels = 1;			int idx = (bufindex+emptylines+position+i)%scrollbuffer.length;			String[] strings = scrollbuffer[idx];			int[] colours = colourbuffer[idx];			if (strings == null || colours == null) break; // we get null and stop iterating			for (int j=0; j<strings.length && j<colours.length; j++) {				if (colours[j] != lastcolour) {					g.setFont(Font.getFont(Font.FACE_SYSTEM, (colours[j]/256)&7, Font.SIZE_SMALL));					g.setColor(getColor(colours[j]&0x0f));					lastcolour = colours[j];				}				g.drawString(strings[j], leftpixels, top+i*fontheight, g.LEFT | g.TOP);				leftpixels += g.getFont().stringWidth(strings[j]);			}		}		if (scrollbar && MAX_LINES - emptylines > mls) {			int startpos = ((position*height) / (MAX_LINES-emptylines-mls));			g.setColor(200,200,200);			g.fillRect(width-3, top, 2, height);			g.setColor(0,0,0);			g.fillRect(width-3, top+startpos, 2, 10);		}	}        private int getColor(int numb) {		switch (numb) {			case 0:  return 0x00000000;			case 1:  return 0x00aa0000;			case 2:  return 0x0000d200;			case 3:  return 0x00aa5522;			case 4:  return 0x000000aa;			case 5:  return 0x00aa00aa;			case 6:  return 0x0000aaaa;			case 7:  return 0x00aaaaaa;			case 8:  return 0x00444444;			case 9:  return 0x00ff4444;			case 10:  return 0x0044ff44;			case 11:  return 0x00ffff44;			case 12:  return 0x004444ff;			case 13:  return 0x00ff44ff;			case 14:  return 0x0044ffff;			case 15:  return 0x00ffffff;		}		return 0x00FFFFFF;	}	public void clear() {		scrollbuffer = new String[MAX_LINES][];		colourbuffer = new int[MAX_LINES][];		emptylines = MAX_LINES;	}}

⌨️ 快捷键说明

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