欢迎来到天天文库
浏览记录
ID:39501266
大小:18.50 KB
页数:3页
时间:2019-07-04
《简化画板资料》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.io.*;publicclasshuahuaextendsJFrame{DrawPaneldrawingArea;intindex=0;drewings[]itemList=newdrewings[5000];Containerc=getContentPane();publichuahua(){super("简单画板");drawingArea=newDrawPanel();c.add(drawingAre
2、a,BorderLayout.CENTER);itemList[index]=newPenci();setSize(500,500);setVisible(true);show();}publicstaticvoidmain(String[]Strgs){newhuahua();}classmouseaextendsMouseAdapter{publicvoidmousePressed(MouseEvente){itemList[index].x1=itemList[index].x2=e.getX();itemList[index].y1=it
3、emList[index].y2=e.getY();index++;itemList[index]=newPenci();}publicvoidmouseReleased(MouseEvente){itemList[index].x1=e.getX();itemList[index].y1=e.getY();//重新获取x1y1的值虽然移动的时候x1y1在变但那时移动监听的不是释放监听的//如果不重新获取坐标将变成默认的0,0itemList[index].x2=e.getX();itemList[index].y2=e.getY();repai
4、nt();index++;itemList[index]=newPenci();}publicvoidmouseEntered(MouseEvente){}publicvoidmouseExited(MouseEvente){}}classmousebextendsMouseMotionAdapter{publicvoidmouseDragged(MouseEvente){itemList[index-1].x1=itemList[index].x2=itemList[index].x1=e.getX();itemList[index-1].y1
5、=itemList[index].y2=itemList[index].y1=e.getY();//按住移动是获取了x1y1的坐标但这坐标不会赋值给释放动作不同不会相互赋值//index++;itemList[index]=newPenci();repaint();}publicvoidmouseMoved(MouseEvente){}}classDrawPanelextendsJPanel{publicDrawPanel(){setBackground(Color.white);addMouseListener(newmousea());add
6、MouseMotionListener(newmouseb());}@OverridepublicvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2d=(Graphics2D)g;//定义画笔intj=0;while(j<=index){drew(g2d,itemList[j]);j++;}}voiddrew(Graphics2Dg2d,drewingsi){i.drew(g2d);//将画笔传入到各个子类中,用来完成各自的绘图}}}classdrewingsimp
7、lementsSerializable{intx1,y1,x2,y2;voiddrew(Graphics2Dg2d){}}classPenciextendsdrewings{voiddrew(Graphics2Dg2d){g2d.drawLine(x1,y1,x2,y2);//System.out.println(x1);}}
此文档下载收益归作者所有