预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共21页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

计算机科学与技术学院 赵志崑 zhaozk@sdufe.edu.cn在组件上绘制图形paintComponent方法绘制直线绘制矩形绘制圆角矩形绘制椭圆绘制弧线绘制多边形设置颜色输出文字设置字体见ImageExample.java classImagePanelextendsJPanel{ privateImageunitsImage=null; privateintunitX; privateintunitY; publicImagePanel(){ Toolkitkit=Toolkit.getDefaultToolkit(); unitsImage=kit.getImage("globe.gif"); } publicvoidpaintComponent(Graphicsg){ super.paintComponent(g); g.drawRect(10,10,100,100); g.drawImage(unitsImage,unitX,unitY,null); } publicvoidsetUnitLocation(intaX,intaY){ unitX=aX; unitY=aY; repaint(); } }见ImageExample1.java publicvoidpaintComponent(Graphicsg){ …… intsx1=118*unitIndex; intsy1=0; intsx2=sx1+117; intsy2=sy1+97; intdx1=unitX; intdy1=unitY; intdx2=dx1+117; intdy2=dy1+97; g.drawImage(unitsImage,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,null); } publicvoidsetUnitLocation(intaX,intaY){ …… unitIndex=(unitIndex+1)%4; repaint(); }等待图像加载Java支持的图片格式Graphics2D缓冲区绘图见DoubleBufferExample.java classPhotoPanelextendsJPanel {privateBufferedImagebackImage=newBufferedImage(1024,800, BufferedImage.TYPE_BYTE_INDEXED); publicvoidpaintComponent(Graphicsg) { super.paintComponent(g); GraphicsbackG=backImage.getGraphics(); backG.drawImage(photoImage,0,0,null); g.drawImage(backImage,0,0,null); backG.dispose(); } publicColorgetColor(intx,inty) { intc=backImage.getRGB(x,y); returnnewColor(c); } }将图片保存到文件感谢!