`

我的画图板1.0<细说2.正文,画图板的主体与监听器>

阅读更多
(Second):画图板的创建与监听器的实现
1.画图板的建立方法,都是基本的
(这里我是直接拷贝我的源代码的一部分,有些语句有点点问题,比如说:JButton应该写成javax.swing.JButton。但在源代码中有了引用,所以我就直接省略了,你可以在我的文章《一个可以重绘的画图板》程序中看到源代码)
 
          /**
	 *生成 画板的方法
	 */
	public void CreatBox(){
		//设置名称
		this.setTitle("画图板");
		//设置大小
		this.setSize(700,500);
		//布局
		FlowLayout fl=new FlowLayout();
		this.setLayout(fl);
		
		//添加组件
		JButton jb1=new JButton("直线");
		jb1.setActionCommand("line");
		this.add(jb1);
		
		JButton jb2=new JButton("矩形");
		jb2.setActionCommand("rect" );
		this.add(jb2);
		
		JButton jb3=new JButton("椭圆");
		jb3.setActionCommand("oval" );
		this.add(jb3);
		
		JButton jb4=new JButton("三角形");
		jb4.setActionCommand("triangle" );
		this.add(jb4);
		
		JButton jb5=new JButton("多边形");
		jb5.setActionCommand("polygon" );
		this.add(jb5);
		
		
		//设置可见性
		this.setVisible(true);
		//设置默认关闭按钮
		this.setDefaultCloseOperation(3);
		
		//得到这个画布
		gp=this.getGraphics();
		
		/**
		 * 创建监听器
		 * 1:鼠标监听器;
		 * 2:动作监听器;
		 */
		DrawListener dw=new DrawListener(gp);
		//给画布添加鼠标监听器
		this.addMouseListener(dw);
		//给按钮添加动作监听器
		jb1.addActionListener(dw);
		jb2.addActionListener(dw);
		jb3.addActionListener(dw);
		jb4.addActionListener(dw);
		jb5.addActionListener(dw);
	}

2.创立一个监听器,既是鼠标监听器又是事件监听器
如下:
public class DrawListener implements java.awt.event.ActionListener,java.awt.event.MouseListener{}


程序如下:

	//重写构造器
	public DrawListener(Graphics gp){
		this.gp=gp;
	}

	/*********************************鼠标监听器********************************/
	public void mouseClicked(MouseEvent e) {
 	    if(i==3&&"polygon".equals(str)){
 	    	counter=0;
 	    	x2=e.getX();
 	    	y2=e.getY();
 	    	
 	    	gp.drawLine(x1, y1, x2, y2);
	    	gp.drawLine(tempX, tempY, x2, y2);
 	    }
	}
	public void mousePressed(MouseEvent e) {
		//按下鼠标得到鼠标坐标点
		if(!"triangle".equals(str)&&!"polygon".equals(str)){
		x1=e.getX();
		y1=e.getY();
		}
	}
	public void mouseReleased(MouseEvent e) {
		//松开鼠标得到鼠标坐标点
		if(!"triangle".equals(str)&&!"polygon".equals(str)){
		x2=e.getX();
		y2=e.getY();
		}
		//画直线
		if("line".equals(str)){
			gp.drawLine(x1, y1, x2, y2);
		}
		//画矩形
		if("rect".equals(str)){
			if(x1<x2&&y1<y2){
				gp.drawRect(x1, y1, x2-x1, y2-y1);
			}
			if(x1>x2&&y1<y2){
				gp.drawRect(x2, y1, x1-x2, y2-y1);
			}
			if(x1>x2&&y1>y2){
				gp.drawRect(x2, y2, x1-x2, y1-y2);
			}
			if(x1<x2&&y1>y2){
				gp.drawRect(x1, y2, x2-x1, y1-y2);
			}
		}
		//画椭圆
		if("oval".equals(str)){
			if(x1<x2&&y1<y2){
				gp.drawOval(x1, y1, x2-x1, y2-y1);
			}
			if(x1>x2&&y1<y2){
				gp.drawOval(x2, y1, x1-x2, y2-y1);
			}
			if(x1>x2&&y1>y2){
				gp.drawOval(x2, y2, x1-x2, y1-y2);
			}
			if(x1<x2&&y1>y2){
				gp.drawOval(x1, y2, x2-x1, y1-y2);
			}
		}
		//画三角形
		if("triangle".equals(str)){

			if(counter==0){
				x1=e.getX();
				y1=e.getY();
				tempX=x1;
				tempY=y1;
				counter++;
			}
			else if(counter==1){
				x2=e.getX();
				y2=e.getY();
				gp.drawLine(x1, y1, x2,y2);
				counter++;
			}
			else if(counter==2){
				x1=e.getX();
				y1=e.getY();
				gp.drawLine(x1, y1, x2, y2);
				gp.drawLine(x1, y1, tempX,tempY);
				counter=0;
			}
			
		}
		//多边形
		if("polygon".equals(str)){
	    
			if(counter==0){
				x1=e.getX();
				y1=e.getY();
				counter++;
			}
			else if(counter==1){
				x2=e.getX();
				y2=e.getY();
				gp.drawLine(x1, y1, x2, y2);
				tempX=x2;
				tempY=y2;
				counter++;
			}
			else  if(counter!=0&&counter!=1){
				x2=e.getX();
				y2=e.getY();
				gp.drawLine(x2, y2, tempX, tempY);
				tempX=x2;
				tempY=y2;
			}
		}
	}
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


	/*********************************动作监听器********************************/
	public void actionPerformed(ActionEvent e) {
		str=e.getActionCommand();
	}

}
说明:
1.计数器counter
在出现多条直线的时候用到它,这个还是很方便的,不会的同学们多多看看这个counter在三角形Triangle及多边形Polygon中的使用
2.生成图形
直线,矩形,椭圆与三角形都是没有什么问题,都是用鼠标左键松开的时候就画了。主要是多边形,我用的方法是点击了鼠标右键之后,会自动将所画的直线自动连接上。
3.不能保存图形
用这两个类所建立的画图有个问题,就是当你的窗体发生改变时,比如说,最小化到任务栏,或者被你拖动后桌面的边缘看不到了等等,你所画的图形都是消失,ok,不要着急。接着看我后面的详解
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics