画图,重绘

画图板绘图-重绘总结

1.创建一形状shape接口,绘制所有图形都要继承这个接口

import java.awt.Color;

/**
 * 形状抽象类,所有形状都必须继承这个类
 * @author wenxiaodong
 *
 */
public abstract class Shape {
	int x1,x2,y1,y2;
	Color color;
	/**
	 * 绘制形状的方法
	 */
	public abstract void draw(java.awt.Graphics g);

}

 2.创建一个队列接口,子类实现添加,删除,修改,调用等功能

public interface ListInterface <E>{
//添加
	public void add(E e);
//调用
	public E get(int index);
//队列大小
	public int size();
//删除全部元素
	public void deleteAll();
//删去指定下标元素
	public void deleteone(int index);
//添加元素到指定下标
	public void add(E e,int index);
//修改指定元素
	public void modefy(E e,int index);
}

 

3.定义一个类实现鼠标监听器

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

public class DrawListener implements java.awt.event.MouseListener{

	private int x1,y1,x2,y2;
	private Graphics g;
	private javax.swing.ButtonGroup group;
	private String type="Line";//要绘制的形状
	private Color color=Color.green;
	
	//定义一个队列对象用来保存绘制的形状
	private ListImp<Shape> shapes ;//问题

	
	public DrawListener(Graphics g,javax.swing.ButtonGroup group,ListImp<Shape> shapes){
		this.g= g;
		this.group = group;
		this.shapes = shapes;
	}
	
	

	public void mousePressed(MouseEvent e) {
		x1 = e.getX();
		y1 = e.getY();
		//得到选中的形状
		type = group.getSelection().getActionCommand();
	}

	
	
	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		
		//sh是shape类型的

		Shape sh=null;
		
		if(type.equals("Line")){
			//创建直线对象
			sh = new Line(x1,y1,x2,y2,color);	
		}else if(type.equals("Rect")){
			sh = new Rect(x1,y1,x2,y2,color); 
		}else if(type.equals("Oval")){
			sh = new Oval(x1,y1,x2,y2,color);	
		}else if(type.equals("Arc")){
			sh=new Arc(x1,y1,x2,y2,color);
		}
						
		
		//绘制形状
		sh.draw(g);
		
		//保存到队列中
		shapes.add(sh);	
	}

		
		
	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mouseClicked(MouseEvent e) {

	}
}

 

4.定义一个类,创建窗体,并在窗体上截取画布,将监听器加在画布上,最后用队列保存Shape对象,实现重绘

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
/**
 * 窗体类
 * @author wenxiaodong
 *2011-10-20 下午09:20:32
 */
public class Draws extends JFrame{
	
	private ListImp<Shape> shapes=new ListImp<Shape>() ;
	private java.awt.Graphics g;
	
	public static void main(String[] args){
		Draws ds=new Draws();
		ds.dra();
	}
	
	public void dra(){
		this.setTitle("画图板");
		this.setSize(600,500);
		
		//不能改变大小
		this.setResizable(false);
		
		this.setLayout(new FlowLayout(0,0,0));
		
		
		
		javax.swing.JPanel left=new javax.swing.JPanel();
		//定义尺寸对象
		Dimension  di=new Dimension(80,400);
		//设置组件大小
		left.setPreferredSize(di);
		//设置背景色
		left.setBackground(Color.green);
		
		
		javax.swing.JPanel right=new javax.swing.JPanel();
		//定义尺寸对象
		Dimension  di2=new Dimension(514,400);
		//设置组件大小
		right.setPreferredSize(di2);
		//设置背景色
		right.setBackground(Color.white);
		
		
		javax.swing.JPanel bottom=new javax.swing.JPanel();
		//定义尺寸对象
		Dimension  di3=new Dimension(595,80);
		//设置组件大小
		bottom.setPreferredSize(di3);
		//设置背景色
		bottom.setBackground(Color.GRAY);	
		
		this.add(left);
		this.add(right);
		this.add(bottom);
		

		
		
		javax.swing.ButtonGroup group=new javax.swing.ButtonGroup();
		
		javax.swing.JRadioButton line=new javax.swing.JRadioButton("Line");
		line.setActionCommand("Line");
		line.setSelected(true);
		
		javax.swing.JRadioButton rect=new javax.swing.JRadioButton("Rect");
		rect.setActionCommand("Rect");
		
		javax.swing.JRadioButton oval=new javax.swing.JRadioButton("Oval");
		oval.setActionCommand("Oval");
		
		javax.swing.JRadioButton arc=new javax.swing.JRadioButton("Arc");
		arc.setActionCommand("Arc");
		
	
		group.add(line);
		group.add(rect);
		group.add(oval);
		group.add(arc);
	
		left.add(line);
		left.add(rect);
		left.add(oval);
		left.add(arc);
		
		javax.swing.JButton bu_color=new javax.swing.JButton("选颜色");
		
		bottom.add(bu_color);
	
		
		this.setDefaultCloseOperation(3);
		this.setVisible(true);	
		g=right.getGraphics();
		DrawListener lis=new DrawListener(g,group,shapes);
		right.addMouseListener(lis);
		
	}
	

		
		/**
		 * 重写父类绘制窗体的方法
		 */
		public void paint(java.awt.Graphics g){
			
			//调用父类的方法来正确的绘制窗体
			super.paint(g);
			
			//调用绘制形状的方法
			drawShape(this.g);
		}
		
		
		//重新绘制形状的方法
		public void drawShape(Graphics g){
			//遍历队列
			for(int i=0;i<shapes.size();i++){
				//取出形状对象
				Shape sh = shapes.get(i);
				//绘制
				sh.draw(g);
			}
		}
	

		
	
	}
	
	
	
	

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值