画图,重绘_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 画图,重绘

画图,重绘

 2011/10/24 8:01:49  文小东  http://574617308-qq-com.iteye.com  我要评论(0)
  • 摘要:画图板绘图-重绘总结1.创建一形状shape接口,绘制所有图形都要继承这个接口importjava.awt.Color;/***形状抽象类,所有形状都必须继承这个类*@authorwenxiaodong**/publicabstractclassShape{intx1,x2,y1,y2;Colorcolor;/***绘制形状的方法*/publicabstractvoiddraw(java.awt.Graphicsg);}2.创建一个队列接口,子类实现添加,删除,修改
  • 标签:画图 重绘

画图板绘图-重绘总结

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);
			}
		}
	

		
	
	}
	
	
	
	

?

发表评论
用户名: 匿名