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

📄 mapcanvas.java

📁 java+mapxtreme 开发校园房管系统
💻 JAVA
字号:
package com.oyc.mapxtreme.applet;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.util.Iterator;

/**
 * 画布基类: 用于的绘制各种图形,包括历史记录中的图形
 * @author 三峡大学理学院 欧阳超
 *
 */
public class MapCanvas extends Canvas {
	
	//当前运行的applet
	protected MainApplet applet = null;
	
	//画布大小
	protected int width;
	protected int height;
	
	//地图图像
	private Image image = null;
	
	//离屏图像及离屏设备
	private Image offImg = null;
	private Graphics offG = null;
	
	//媒体跟踪器
	private MediaTracker tracker = null;
	
	//画图历史管理器
	private DrawHistoryManager hMgr = null;
	private DrawHistoryBean hBean = null;
	
	
	/**
	 * 构造器: 根据当前运行的applet,画布宽度高度创建一个画布对象
	 * @param applet
	 * @param width
	 * @param height
	 */
	public MapCanvas(MainApplet applet, int width, int height){
		this.applet = applet;
		this.width = width;
		this.height = height;
		
		//创建离屏图像及离屏设备
		offImg = applet.createImage(width, height);
		offG = offImg.getGraphics();
		tracker = new MediaTracker(this);
		
		//创建历史管理器
		hMgr = new DrawHistoryManager();
	}
	
	/**
	 * 绘制离屏图像
	 */
	public void paint(Graphics g) {
		g.drawImage(offImg, 0, 0, this);
	}
	
	/**
	 * 更新画布
	 */
	public void update(Graphics g){
		this.paint(g);
	}
	
	/**
	 * 更新当前地图
	 * @param img 地图图片
	 */
	public void updateMap(Image img){
		this.image = img;
		tracker.addImage(img, 0);
		try {
			tracker.waitForID(0);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		offG.drawImage(img, 0, 0, this);
		this.repaint();
	}
	
	/**
	 * 在地图上画一个矩形,用于放大,缩小
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public void drawRect(int x, int y, int width, int height) {
		offG.drawImage(image, 0, 0, this);
		//画矩形
		offG.setColor(Color.red);
		offG.drawRect(x, y, width, height);
		this.repaint();
	}
	
	/**
	 * 绘制一个圆
	 * @param x
	 * @param y
	 * @param radius
	 */
	public void drawOval(int x, int y, int radius) {
		offG.drawImage(image, 0, 0, this);
		//绘制圆
		offG.setColor(Color.red);
		offG.drawOval(x, y, radius*2, radius*2);
		this.repaint();
	}
	
	/**
	 * 在屏幕上画一个标签,用于显示鼠标当前的状态
	 * @param x  当前鼠标x坐标
	 * @param y  当前鼠标y坐标
	 * @param width
	 * @param height
	 * @param label  标签内的文字
	 */
	public void drawLabel(int x, int y, int width, int height, String label){
		offG.drawImage(image, 0, 0, this);
		offG.setColor(Color.black);
		offG.drawRect(x+4, y+4, width+1, height+1);
		offG.setColor(Color.white);
		offG.fillRect(x+5, y+5, width, height);
		offG.setColor(Color.black);
		offG.drawString(label, x+10, y+18);
		this.repaint();
	}
	
	/**
	 * 平移地图
	 * @param x
	 * @param y
	 */
	public void panMap(int x, int y){
		offG.setColor(Color.lightGray);
		offG.fillRect(0, 0, this.width, this.height);
		offG.drawImage(image, x, y, this);
		this.repaint();
	}
	
	/**
	 * 在地图上画一条直线
	 * @param x1
	 * @param y1
	 * @param x2
	 * @param y2
	 * @param historyModel 是否开启历史模式,画出保存在历史记录中的图像
	 * @param saveModel    是否开启保存模式,以保存当前的画图记录
	 */
	public void drawLine(int x1, int y1, int x2, int y2, boolean historyModel, boolean saveModel, String label, int labelW, int labelH){
		offG.drawImage(image, 0, 0, this);
		offG.setColor(Color.red);
		
		if(historyModel){
			this.drawHistory(offG);
		}
		offG.drawLine(x1, y1, x2, y2);
		if(saveModel){
			hMgr.addHistory(new DrawHistoryBean(x1, y1, x2, y2, 0, 0, 0, 0, "line", "draw", Color.red));
		}
		
		//画标签
		offG.setColor(Color.black);
		offG.drawRect(x2+4, y2+4, labelW+1, labelH+1);
		offG.setColor(Color.white);
		offG.fillRect(x2+5, y2+5, labelW, labelH);
		offG.setColor(Color.black);
		offG.drawString(label, x2+10, y2+18);
		this.repaint();
	}
	
	/**
	 * 清图:首先删除画图历史记录,然后将地图重新画出
	 *
	 */
	public void clear(){
		hMgr.clearHistory();
		offG.drawImage(image, 0, 0, this);
		this.repaint();
	}
	
	/**
	 * 画保存在历史记录中的图像
	 * @param g 画图设备
	 */
	private void drawHistory(Graphics g){
		Iterator iter = this.hMgr.getHistory();
		if(iter == null){
			return ;
		}
		while(iter.hasNext()){
			hBean = (DrawHistoryBean) iter.next();
			
			g.setColor(hBean.getColor());
			//画直线
			if(hBean.getType().equals("line")){
				g.drawLine(hBean.getX1(), hBean.getY1(), hBean.getX2(), hBean.getY2());
				continue ;
			}
			//画矩形
			if(hBean.getType().equals("rect")){
				if(hBean.getModel().equals("draw")){
					g.drawRect(hBean.getX1(), hBean.getY1(), hBean.getWidth(), hBean.getHeight());
				}else if(hBean.getModel().equals("fill")){
					g.fillRect(hBean.getX1(), hBean.getY1(), hBean.getWidth(), hBean.getHeight());
				}
				continue ;
			}
			//画圆
			if(hBean.getType().equals("arc")){
				if(hBean.getModel().equals("draw")){
					g.drawArc(hBean.getX1(), hBean.getY1(), hBean.getWidth(), hBean.getHeight(), hBean.getStartAngle(), hBean.getArcAngle());
				}else if(hBean.getModel().equals("fill")){
					g.fillArc(hBean.getX1(), hBean.getY1(), hBean.getWidth(), hBean.getHeight(), hBean.getStartAngle(), hBean.getArcAngle());
				}
				continue ;
			}
		}
	}

}

⌨️ 快捷键说明

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