天天看点

第四节 简易画板DrawFrame(面板)DrawMouse(监听器)

简易画板

  • DrawFrame(面板)
  • DrawMouse(监听器)

DrawFrame(面板)

import java.awt.FlowLayout;


import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;

public class DrawFrame {
	public static void main(String[] args) {
		DrawFrame dr=new DrawFrame();
		dr.showFrame();
	}

	
	public void showFrame(){
		JFrame jf=new JFrame();
		jf.setSize(800,800);
		jf.setTitle("画图工具");
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(3);
		FlowLayout flow= new FlowLayout();
		jf.setLayout(flow);
		String[] name= {"直线","矩形","三角形","多边形","红色","黑色","清除"};
		DrawMouse mouse=new DrawMouse();
		for(int i=0;i<name.length;i++) {
			JButton jbu=new JButton(name[i]);  //没有共用一个对象,添加完后替换了上一个对象
			jf.add(jbu);
			jbu.addActionListener(mouse);
		}
		jf.setVisible(true);
		Graphics g=jf.getGraphics();
		jf.addMouseListener(mouse);//鼠标监听器是给窗体加不是给按钮加
		mouse.setGr(g);

	}
}
           

DrawMouse(监听器)

import java.awt.Color;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class DrawMouse implements MouseListener,ActionListener{

	private Graphics gr;
	private int x1,y1,x2,y2,x3,y3,c_x1,c_x2,c_y1,c_y2;
	private String name;
	private int count=1;
	public void setGr(Graphics g) {
		gr=g;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		name = e.getActionCommand();
		System.out.println("按钮:"+name);
		if("红色".equals(name)) {
			gr.setColor(Color.red);
		}else if("黑色".equals(name)) {
			gr.setColor(Color.black);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("点击");

		
		if("三角形".equals(name)) {
			//System.out.println("22222222222");
		x3=e.getX();
		y3=e.getY();
		gr.drawLine(x1, y1, x3, y3);
		gr.drawLine(x2, y2, x3, y3);
		}
		else if("多边形".equals(name)) {
			//System.out.println("1111111111111");
			if(e.getClickCount()==1) {
			x3=e.getX();
			y3=e.getY();
			System.out.println("点击时: x3= "+x3+" y3="+y3);
			gr.drawLine(x2, y2, x3, y3);
			x2=x3;
			y2=y3;
			}
			if(e.getClickCount()==2) {
				x3=e.getX();
				y3=e.getY();
				//System.out.println("点击时: x3= "+x3+" y3="+y3);
				gr.drawLine(x1, y1, x3, y3);
				count=1;
			}
			
		}
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("按下");
		
		if(count==1&&"多边形".equals(name)) {
			x1=e.getX();
			y1=e.getY();
			System.out.println("第一次按下时:  x1="+x1+" y1="+y1);
		}
		else if(count==2&&"多边形".equals(name)) {
			c_x1=e.getX();
			c_y1=e.getY();
			System.out.println("第二次按下时时:  x1="+x1+" y1="+y1+"  x2="+x2+"  y2="+y2);
		}
		
		else if(count==1&&"直线".equals(name)){
			x1=e.getX();
			y1=e.getY();
		}
		else if(count==2&&"三角形".equals(name)) {
			c_x1=e.getX();//替身作用 点击前 的按下由 c_x1 c_y1接受
			c_y1=e.getY();
		}
		else if(count==1&&"三角形".equals(name)) {
			x1=e.getX();
			y1=e.getY();
		}
		else if(count==1&&"矩形".equals(name)) {
			x1=e.getX();
			y1=e.getY();
			}
				
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("松开");
		
		if(count==1&&"多边形".equals(name)) {
			x2=e.getX();
			y2=e.getY();
			System.out.println("第一次松开时时:  x1="+x1+" y1="+y1+"  x2="+x2+"  y2="+y2);
			gr.drawLine(x1, y1, x2, y2);
			count++;
		}
		else if(count==2&&"多边形".equals(name)) {
			c_x2=e.getX();
			c_y2=e.getY();
			System.out.println("第二次松开时时:  x1="+x1+" y1="+y1+"  x2="+x2+"  y2="+y2);
			//count=1; 不加count=1 多边形多次点击不会干扰原身 依然只会用替身
		}
		
		else if(count==1&&"直线".equals(name)) {
			x2=e.getX();
			y2=e.getY();
			gr.drawLine(x1, y1, x2, y2);
			
		}
		else if(count==1&&"三角形".equals(name)) {
			x2=e.getX();
			y2=e.getY();
			gr.drawLine(x1, y1, x2, y2);
			count++;
		}
		else if(count==2&&"三角形".equals(name)) {
			c_x2=e.getX();
			c_y2=e.getY();
			count=1;
		}
		else if(count==1&&"矩形".equals(name)) {
			x2=e.getX();
			y2=e.getY();
			gr.drawRect(x1, y1, x2-x1, y2-y1);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("进入");
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("退出");
	}

}