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

📄 skin.java

📁 Micro Window Toolkit(MWT)是一个用于开发J2ME用户界面(UI)的工具包。它具有友好
💻 JAVA
字号:
package prototype;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class Skin {
	static final private int TOP_CENTER = 0;
	static final private int TOP_RIGHT = 1;
	static final private int MIDDLE_RIGHT = 2;
	static final private int BOTTOM_RIGHT = 3;
	static final private int BOTTOM_CENTER = 4;
	static final private int BOTTOM_LEFT = 5;
	static final private int MIDDLE_LEFT = 6;
	static final private int TOP_LEFT = 7;
	static final private int MIDDLE_CENTER = 8;

	private Image[] images;
	private int[] colors;

	// If the component is doubleBuffered, this class will try to create
	// a cache to improve performance.
	// However, if the component is changing its size permanently,
	// the effect will be not good.
	// That is why, there's countdown before creating the cache.
	// (If COUNT is 0, there's no countdown)
	final static private int COUNT = 4;


	/** Creates a "dummy" skin, paint calls are ignored. */
	public Skin() {
		images = null;
		colors = null;
	}

	/** Creates a "colour rectangle" skin.
	 *  @see <a href="#colourrect">Colour Rectangle Skin</> */
	public Skin(int[] colors) {
		this.colors = new int[colors.length];
		System.arraycopy(colors,0,this.colors,0,colors.length);
		images = null;
	}

	/**
	 * Creates an image skin.
	 * @param images The array with 9 images in the correspoding order
	 * @param newSize The new size, or 0 to skip resizing
	 * @see <a href="#imageskin">Image Skin</a>
	 */
	public Skin(Image[] images, int newSize) {
		colors = null;

		if(images.length != 9) throw new IllegalArgumentException();
		this.images = new Image[9];
		System.arraycopy(images,0,this.images,0,9);

		if(newSize == 0) return;

		for(int index=TOP_CENTER; ;index=BOTTOM_CENTER) {
			if(this.images[index].getWidth() != newSize) {
				final Image t = this.images[index];
				this.images[index] = Image.createImage(newSize,t.getHeight());
				for(int i=0; i<newSize ;i+=t.getWidth())
					this.images[index].getGraphics().drawImage(t,i,0,0);
			}
			if(index == BOTTOM_CENTER) break;
		}

		for(int index=MIDDLE_LEFT; ;index=MIDDLE_RIGHT) {
			if(this.images[index].getHeight() != newSize) {
				final Image t = this.images[index];
				this.images[index] = Image.createImage(t.getWidth(),newSize);
				for(int i=0; i<newSize ;i+=t.getHeight())
					this.images[index].getGraphics().drawImage(t,0,i,0);
			}
			if(index == MIDDLE_RIGHT) break;
		}

		final Image t = this.images[MIDDLE_CENTER];
		if(t.getWidth() != newSize || t.getHeight() != newSize) {
			this.images[MIDDLE_CENTER] = Image.createImage(newSize,newSize);
			for(int x=0; x<this.images[MIDDLE_CENTER].getWidth() ;x+=t.getWidth())
				for(int y=0; y<this.images[MIDDLE_CENTER].getHeight() ;y+=t.getHeight())
					this.images[MIDDLE_CENTER].getGraphics().drawImage(t,x,y,0);
		}
	}

	/**
	 * Gets a clone of this skin.<br>
	 * @see <a href="#extending">Extending a Skin</a>
	 */
	public Skin clone() {
		Skin s = new Skin();
		copy(s);
		return s;
	}
	/**
	 * Copies this skin into the given skin.<br>
	 * @see <a href="#extending">Extending a Skin</a>
	 */
	protected void copy(Skin skin) {
		skin.images = images;
		skin.colors = colors;
	}

	/**
	 * Paints a component into the given graphics object.<br>
	 * If the component is double buffered, this method may call {@link #createBuffer(int, int)}.
	 */
	final public void paint(Component c, Graphics g) {
		
		// if component is resized, start counting
		if(c.getWidth() != c.skinWidth || c.getHeight() != c.skinHeight) {
			c.skinWidth = c.getWidth(); // ojo si se cambia de lugar!
			c.skinHeight = c.getHeight();
			c.skinImage = null;
		}
		paint(g,c.getWidth(),c.getHeight());
	}

	
	protected Image createBuffer(int width, int height) {
		Image buf = Image.createImage(width, height);
		this.paint(buf.getGraphics(),width, height);
		return buf;
	}

	/** Paints this skin into the given graphics object with the given width and height. */
	protected void paint(Graphics g, int width, int height) {
		if(images == null) {
			if(colors == null) return;
			g.setColor(colors[0]);
			g.fillRect(0,0,width,height);
			for(int i=0; i<colors.length-1 ;i++) {
				g.setColor(colors[i+1]);
				g.drawRect(i,i,width-i*2-1,height-i*2-1);
			}
			return;
		}
		final int cx = g.getClipX();
		final int cy = g.getClipY();
		final int cw = g.getClipWidth();
		final int ch = g.getClipHeight();
		int ww = width-images[MIDDLE_RIGHT].getWidth() - cx;
		ww = (ww < cw)? ww : cw;
		int hh = height-images[BOTTOM_CENTER].getHeight() - cy;
		hh = (hh < ch)? hh : ch;

		// Center
		g.setClip(cx,cy,ww,hh);
		for(int xx=images[MIDDLE_LEFT].getWidth(); xx<width-images[MIDDLE_RIGHT].getWidth() ; xx += images[MIDDLE_CENTER].getWidth())
			for(int yy=images[TOP_CENTER].getHeight(); yy<height-images[BOTTOM_CENTER].getHeight() ; yy += images[MIDDLE_CENTER].getHeight())
				g.drawImage(images[MIDDLE_CENTER],xx,yy,0);

		// Horizontal
		g.setClip(cx,cy,ww,ch);
		for(int i=images[MIDDLE_LEFT].getWidth(); i<=width ;i+=images[BOTTOM_CENTER].getWidth()) {
			g.drawImage(images[TOP_CENTER],i,0,0);
			g.drawImage(images[BOTTOM_CENTER],i,height-images[BOTTOM_CENTER].getHeight(),0);
		}

		// Vertical
		g.setClip(cx,cy,cw,hh);
		for(int i=images[TOP_LEFT].getHeight(); i<=height-images[BOTTOM_LEFT].getHeight() ;i+=images[MIDDLE_RIGHT].getHeight()) {
			g.drawImage(images[MIDDLE_LEFT],0,i,0);
			g.drawImage(images[MIDDLE_RIGHT],width-images[MIDDLE_RIGHT].getWidth(),i,0);
		}

		// Diagonals
		g.setClip(cx,cy,cw,ch);
		g.drawImage(images[TOP_RIGHT],width-images[TOP_RIGHT].getWidth(),0,0);
		g.drawImage(images[BOTTOM_RIGHT],width-images[BOTTOM_RIGHT].getWidth(),height-images[BOTTOM_RIGHT].getHeight(),0);
		g.drawImage(images[BOTTOM_LEFT],0,height-images[BOTTOM_LEFT].getHeight(),0);
		g.drawImage(images[TOP_LEFT],0,0,0);
	}
}

⌨️ 快捷键说明

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