欢迎来到天天文库
浏览记录
ID:39551590
大小:61.50 KB
页数:6页
时间:2019-07-06
《java框选虚线矩形》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、代码:DragDao.javaimportjava.awt.BasicStroke;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Point;importjava.awt.Stroke;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.awt.event.MouseMotionListener;importjavax.s
2、wing.JPanel;publicclassDragDaoextendsJPanelimplementsMouseMotionListener,MouseListener{/****/privatestaticfinallongserialVersionUID=1L;/**矩形的起点左上角**/privatePointrectStart=null;/**矩形的终点右下角**/privatePointrectStop=null;/**是否绘制虚线矩形**/privatebooleandottedTag=true;publicDragDao(){th
3、is.setBackground(Color.WHITE);//this.setOpaque(false);this.setSize(1000,1000);rectStart=newPoint(0,0);rectStop=newPoint(0,0);//this.addMouseListener(this);//this.addMouseMotionListener(this);this.addMouseListener(ma_imgpanel);this.addMouseMotionListener(mma);}MouseMotionListen
4、ermma=newMouseMotionListener(){@OverridepublicvoidmouseMoved(MouseEvente){//TODOAuto-generatedmethodstub}@OverridepublicvoidmouseDragged(MouseEvente){rectStop=e.getPoint();repaint();}};MouseListenerma_imgpanel=newMouseListener(){@OverridepublicvoidmouseReleased(MouseEvente){do
5、ttedTag=true;repaint();}@OverridepublicvoidmousePressed(MouseEvente){/**设置可以进行绘制**/dottedTag=true;/**记录起始点**/rectStart=e.getPoint();/**记录起终点**/rectStop=rectStart;}@OverridepublicvoidmouseExited(MouseEvente){//TODOAuto-generatedmethodstub}@OverridepublicvoidmouseEntered(MouseEv
6、ente){//TODOAuto-generatedmethodstub}@OverridepublicvoidmouseClicked(MouseEvente){//TODOAuto-generatedmethodstub}};@OverrideprotectedvoidpaintComponent(Graphicsg){//TODOAuto-generatedmethodstubsuper.paintComponent(g);Graphics2Dg2=(Graphics2D)g;/**矩形的宽度**/intw=Math.abs(rectStop
7、.x-rectStart.x);/**矩形的高度**/inth=Math.abs(rectStop.y-rectStart.y);/**起点终点的最小值作为起点**/Pointmin=newPoint(0,0);min.x=Math.min(rectStop.x,rectStart.x);min.y=Math.min(rectStop.y,rectStart.y);/**起点终点的最小值作为终点**/Pointmax=newPoint(0,0);max.x=Math.max(rectStop.x,rectStart.x);max.y=Math.ma
8、x(rectStop.y,rectStart.y);/**如果是绘制虚线矩形**/if(dottedTag){/**new
此文档下载收益归作者所有