editorcanvas.java

来自「一个可视化编辑器的基础结构」· Java 代码 · 共 85 行

JAVA
85
字号
package org.goshawk.workflow.GUI.Display;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.Iterator;
import org.goshawk.workflow.GUI.ShapeShell.ShapeShell;

public class EditorCanvas extends Panel 
{
	private EditorCanvasExectuor executor = new EditorCanvasExectuor(this);
	
	private static final long serialVersionUID = -2271634378286788915L;

	private Image doubleBuffer = null;
	
	private ShapeMap shapeMap = new ShapeMap();
	
	private int width = 0;
	
	private int height = 0;
	
	public EditorCanvas(int width,int height)
	{
		this.width = width;
		this.height = height;
		doubleBuffer = new BufferedImage(width,height,BufferedImage.TYPE_USHORT_555_RGB);
		this.setBounds(0,0,width,height);
	}

	public ShapeMap getShapeMap()
	{
		return shapeMap;
	}

	public void addShapeShell(String id,ShapeShell shape)
	{
		shapeMap.put(id,shape);
	}
	
	public ShapeShell getShapeShell(String id)
	{
		return (ShapeShell)shapeMap.get(id);
	}
	
	public void setShapeMap(ShapeMap shapeMap)
	{
		this.shapeMap = shapeMap;
	}

	public void paint(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) doubleBuffer.getGraphics();
		g2.fillRect(0,0,width,height);
		Color color = new Color(0,2,3);
		g2.setColor(color);
		Collection shapeSet = shapeMap.values();
		Iterator iter = shapeSet.iterator();
		while(iter.hasNext())
		{
			ShapeShell shape = (ShapeShell)iter.next();
			if(shape!=null)
			{
				shape.paint(g2);
			}
		}
		g.drawImage(doubleBuffer,0,0,this);
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	/**移去canvas中java.awt.Component对象,并使这些对象失去焦点**/
	public void removeComponent()
	{
		this.executor.removeComponent();
	}
}

⌨️ 快捷键说明

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