用Java实现一个简易画板

2022-06-19 14:49:15

该代码主要实现的功能是能绘制简单几何图形,如:直线,矩形,椭圆等,能选择所画几何图形的边框颜色。

画图板包含以下几个部分:
1.画图的界面
创建一个DrawPad类
2.绘制的工具
获取方法:窗体对象.getGraphics();
3.鼠标监听器
编写一个实现MouseListener接口的类
4.鼠标事件类

首先,编写一个DrawPad类

publicclassDrawPad{publicstaticvoidmain(String[]args){	
	DrawPad drawpad=newDrawPad();
	drawpad.init();}privatevoidinit(){//添加一个窗体
	JFrame draw=newJFrame();//设置窗体属性
	draw.setTitle("画板");
	draw.setSize(600,500);
	draw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加菜单条JMB
	JMenuBar JMB=newJMenuBar();
	draw.setJMenuBar(JMB);//将菜单条JMB添加到窗体上//添加菜单JM,JM1
	JMenu JM=newJMenu("File");
	JMB.add(JM);//将菜单添加到菜单条上
	JMenu JM1=newJMenu("Edit");
	JMB.add(JM1);//添加菜单项JMI
	JMenuItem JMI=newJMenuItem("new");
	JM.add(JMI);//将菜单项添加到菜单上//添加面板
	JPanel JP=newJPanel();
	JP.setBackground(Color.WHITE);
	JP.setPreferredSize(newDimension(0,80));
	draw.add(JP);

	DrawListener mouse=newDrawListener();//定义图形数组 按钮jbutton
	String[]name={"直线","矩形","椭圆","分形","波纹"}for(int i=0;i<name.length;i++){
	JButton button=newJButton(name[i]);
	JP.add(button);
	jbutton.addActionListener(mouse);//在按钮上添加动作监听器}//添加图形的颜色按钮
	Color color[]={Color.RED,Color.BLUE,Color.CYAN};for(int i=0;i<color.length;i++){
	JButton button=newJButton(color[i]);
	button.setBackground(color[i]);
	button.setPreferredSize(newDimension(30,30));
	JP.add(button);
	button.addActionListener(mouse);}
	draw.setVisible(true);//可视化 在这行代码之前要完成所有组件的添加//获取画笔
	Graphics a= draw.getGraphics();
	System.out.println(a);
	
	draw.addMouseListener(mouse);
	mouse.a=a;}}

然后,编写一个实现MouseListener,ActionListener接口的类

publicclassDrawListenerimplementsMouseListener,ActionListener{public Graphics a;int x1,x2,y1,y2;public String name;publicvoidsetA(Graphics a){
		Graphics A= a;}@OverridepublicvoidmouseClicked(MouseEvent e){// TODO Auto-generated method stub
		System.out.println("点击");//a.drawLine(100,100,400,100);}@OverridepublicvoidmousePressed(MouseEvent e){// TODO Auto-generated method stub
		 x1=e.getX();
		 y1=e.getY();
		System.out.println("按下");}@OverridepublicvoidmouseReleased(MouseEvent e){// TODO Auto-generated method stub
		 x2=e.getX();
		 y2=e.getY();/*if((x1>x2)&&(y1>y2)) {
			a.drawLine(x1, y1, x2, y2);
			//    左上角的坐标   宽  高  
			a.drawRect(x2, y2, (x1-x2) ,(y1-y2));
		}else if((x1>x2)&&(y1<y2)){
			a.drawLine(x1, y1, x2, y2);
			//    左上角的坐标   宽  高  
			a.drawRect(x2, y1, (x1-x2) ,(y2-y1));
		}else if((x1<x2)&&(y1>y2)){
			a.drawLine(x1, y1, x2, y2);
			//    左上角的坐标   宽  高  
			a.drawRect(x1, y2, (x2-x1) ,(y1-y2));
		}else if((x1<x2)&&(y1<y2)){
			a.drawLine(x1, y1, x2, y2);
			//    左上角的坐标   宽  高  .
			a.drawRect(x1, y1, (x2-x1) ,(y2-y1));
		
		}*///选择不同的图形进行绘制,以按钮上的文本进行区分if("直线".equals(name)){
		 a.drawLine(x1, y1, x2, y2);}if("矩形".equals(name)){
		 a.drawRect(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));}if("椭圆".equals(name)){
			a.drawOval(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x2-x1),Math.abs(y2-y1));}if("分形".equals(name)){double x=0,y=0;double a1=-1.8,b=-2.0,c=-0.5,d=-0.9;for(int i=0;i<100000;i++){double x3=Math.sin(a1*y)-c*Math.cos(a1*x);double y3=Math.sin(b*x)-d*Math.cos(b*y);
		x=x3;
		y=y3;int px=(int)(x3*100+x1);int py=(int)(y3*100+y1);
		
		a.drawLine(px, py, px, py);}}if("波纹".equals(name)){double x4=0,y4=0;double a2=1.40,b2=1.56,c2=1.40,d2=-6.56;for(int i=0;i<100000;i++){double x5=Math.sin(a2*x4)-Math.cos(b2*y4);double y5=Math.sin(c2*x4)-Math.cos(d2*y4);
			x4=x5;
			y4=y5;int px=(int)(x5*100+x1);int py=(int)(y5*100+y1);
		
			a.drawLine(px, py, px, py);}}
		
		
		
		
		 System.out.println("释放");}@OverridepublicvoidmouseEntered(MouseEvent e){// TODO Auto-generated method stub
		System.out.println("进入");}@OverridepublicvoidmouseExited(MouseEvent e){// TODO Auto-generated method stub
		System.out.println("离开");}publicvoidactionPerformed(ActionEvent e){if("".equals(e.getActionCommand())){
			
			JButton jb=(JButton)e.getSource();
			
			Color color=jb.getBackground();//获取按钮的背景颜色
			a.setColor(color);//将画笔设置为按钮的背景颜色}else{
			name=e.getActionCommand();}}}

最后,结果截图如下:
在这里插入图片描述

  • 作者:weixin_45723735
  • 原文链接:https://blog.csdn.net/weixin_45723735/article/details/108070896
    更新时间:2022-06-19 14:49:15