欢迎来到天天文库
浏览记录
ID:15419009
大小:25.50 KB
页数:4页
时间:2018-08-03
《双缓冲绘制无闪烁》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、importjava.awt.BasicStroke;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Rectangle;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.awt.event.MouseMotionList
2、ener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.awt.image.BufferedImage;importjavax.swing.JFrame;importjavax.swing.JPanel;publicclassBufferedDrawextendsJPanelimplementsMouseListener,MouseMotionListener{Rectanglerect=newRectangle(0,0,100,50);Bu
3、fferedImagebi=newBufferedImage(5,5,BufferedImage.TYPE_INT_RGB);Graphics2Dbig;intlast_x,last_y;booleanfirstTime=true;Rectanglearea;booleanpressOut=false;publicBufferedDraw(){setBackground(Color.white);addMouseMotionListener(this);addMouseListener(this);}//Handlestheeventoftheuser
4、pressingdownthemousebutton.publicvoidmousePressed(MouseEvente){last_x=rect.x-e.getX();last_y=rect.y-e.getY();//Checkswhetherornotthecursorisinsideoftherectanglewhilethe//userispressingthemouse.if(rect.contains(e.getX(),e.getY())){updateLocation(e);}else{pressOut=true;}}//Handles
5、theeventofauserdraggingthemousewhileholdingdownthe//mousebutton.publicvoidmouseDragged(MouseEvente){if(!pressOut){updateLocation(e);}}//Handlestheeventofauserreleasingthemousebutton.publicvoidmouseReleased(MouseEvente){if(rect.contains(e.getX(),e.getY())){updateLocation(e);}}pub
6、licvoidmouseMoved(MouseEvente){}publicvoidmouseClicked(MouseEvente){}publicvoidmouseExited(MouseEvente){}publicvoidmouseEntered(MouseEvente){}publicvoidupdateLocation(MouseEvente){rect.setLocation(last_x+e.getX(),last_y+e.getY());repaint();}publicvoidpaint(Graphicsg){update(g);}
7、publicvoidupdate(Graphicsg){Graphics2Dg2=(Graphics2D)g;if(firstTime){Dimensiondim=getSize();intw=dim.width;inth=dim.height;area=newRectangle(dim);bi=(BufferedImage)createImage(w,h);big=bi.createGraphics();rect.setLocation(w/2-50,h/2-25);big.setStroke(newBasicStroke(8.0f));firstT
8、ime=false;}big.setColor(Color.white);big.clearR
此文档下载收益归作者所有