Java程序設(shè)計(jì)——12圖形繪制_第1頁(yè)
Java程序設(shè)計(jì)——12圖形繪制_第2頁(yè)
Java程序設(shè)計(jì)——12圖形繪制_第3頁(yè)
Java程序設(shè)計(jì)——12圖形繪制_第4頁(yè)
Java程序設(shè)計(jì)——12圖形繪制_第5頁(yè)
已閱讀5頁(yè),還剩16頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院趙志崑趙志崑在組件上繪制圖形在組件上繪制圖形 圖形可以繪制在任意Swing組件上,但一般繪制在面板(JPanel)上,因?yàn)槊姘迨强瞻椎摹@L制圖形需要三步: 定義一個(gè)擴(kuò)展自JPanel的新類; 覆蓋其paintComponent方法,將繪圖的語(yǔ)句添加在這個(gè)方法中; 創(chuàng)建一個(gè)新類的對(duì)象,添加到要顯示的容器中。見見PanelExample.javapublic class PanelExample extends JFrame public PanelExample() contentPane.add(new MyPanel(); class MyPanel extends JP

2、anel public void paintComponent(Graphics g) /繪圖部分代碼super.paintComponent(g);g.drawRect(10,10,100,50); 趙志崑paintComponentpaintComponent方法方法 此方法是一個(gè)回調(diào)方法,聲明如下: public void paintComponent(Graphics g) paintComponent方法在組件需要繪制時(shí)被自動(dòng)調(diào)用: 面板首次顯示時(shí); 面板尺寸變化時(shí); 其它窗口遮住面板時(shí); 組件的repaint()方法被調(diào)用時(shí)。 paintComponent方法的參數(shù): Graphi

3、cs g:繪圖對(duì)象,所有繪圖動(dòng)作都是對(duì)其方法的調(diào)用。g相當(dāng)于一塊畫布,主要有以下幾類方法: 繪制簡(jiǎn)單幾何圖形,如矩形、橢圓等; 繪制圖像,如圖片; 繪制文字; 設(shè)置畫筆屬性,如顏色、文字字體、繪圖模式等。趙志崑繪制直線繪制直線void drawLine(int x1, int y1, int x2, int y2) 繪制一條線。見見GraphicsExample.javag.drawLine(10,20,60,50);(x1,y1)(x1,y1)XY趙志崑繪制矩形繪制矩形void drawRect(int x, int y, int width, int height) 繪制一個(gè)矩形。void

4、 fillRect(int x, int y, int width, int height) 填充一個(gè)矩形。見見GraphicsExample.javag.drawRect(10,20,60,50);g.fillRect(80,20,60,50);(x,y)widthheight趙志崑繪制圓角矩形繪制圓角矩形void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) 繪制一個(gè)圓角矩形void fillRoundRect(int x, int y, int width, int heig

5、ht, int arcWidth, int arcHeight) 填充一個(gè)圓角矩形(x,y)widthheight見見GraphicsExample.javag.drawRoundRect(10,10,100,50,20,15);g.fillRoundRect(160,10,100,50,20,15);arcHeightarcWidth趙志崑繪制橢圓繪制橢圓void drawOval(int x, int y, int width, int height) 繪制一個(gè)橢圓void fillOval(int x, int y, int width, int height) 填充一個(gè)橢圓(x,y)w

6、idthheight見見GraphicsExample.javag.drawOval(10,10,100,50);g.fillOval(160,10,100,50);趙志崑繪制弧線繪制弧線void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) 繪制一條弧。void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 填充一個(gè)扇形。見見GraphicsExample.javag.drawArc(1

7、0,10,100,50,0,60);g.fillArc(160,10,100,50,0,60);(x,y)widthheightstartAnglearcAngle趙志崑繪制多邊形繪制多邊形void drawPolygon(Polygon p) 繪制一個(gè)多邊形void fillPolygon(Polygon p) 填充一個(gè)多邊形見見GraphicsExample.javaPolygon p = new Polygon();p.addPoint(10,10);p.addPoint(100,30);p.addPoint(50,50);p.addPoint(100,70);p.addPoint(30

8、,100);g.drawPolygon(p);p.translate(150,0);g.fillPolygon(p);(10,10)(100,30)(50,50)(100,70)(30,100)150趙志崑設(shè)置顏色設(shè)置顏色void setColor(Color c):將當(dāng)前畫筆顏色設(shè)置為c。Color getColor():讀取當(dāng)前畫筆顏色。顏色的構(gòu)造: 構(gòu)造函數(shù)Color(int r, int g, int b):參數(shù)為紅、綠、藍(lán)的值(0-255)。 直接使用Color類中的靜態(tài)對(duì)象:如Color.blue, Color.yellow, Color.orange, 見見ColorExampl

9、e.javaint red = 0;int green = 0;int blue = 0;int gray = 0;for(red = 0; red = 255; red += 16) g.setColor(new Color(red,green,blue);g.fillRect(red+16,16,15,15);趙志崑輸出文字輸出文字 將字符串輸出到特定位置: void drawString(String str, int x, int y)(x,y)見見TextExample.javag.drawString(Hello World!,50,50);g.drawString(世界你好!,5

10、0,100);趙志崑設(shè)置字體設(shè)置字體設(shè)置當(dāng)前字體:void setFont(Font font)獲取當(dāng)前字體:Font getFont()Font的構(gòu)造器:Font(String name, int style, int size) name:字體名稱,可以用下面的方法獲取系統(tǒng)支持的所有字體: String GraphicsEnvironment. getLocalGraphicsEnvironment(). getAvailableFontFamilyNames() style:字體形式,為Font.PLAIN,F(xiàn)ont.BOLD,F(xiàn)ont.ITALIC。 size:字體的高度,單位為像素。

11、這些設(shè)置字體的方法同樣可以應(yīng)用于組件上顯示的字體。見見FontExample.javapublic void paintComponent(Graphics g) super.paintComponent(g);g.setFont(new Font(fontName,fontStyle,fontSize);g.drawString(Hello World!,50,50);g.drawString(世界你好!,50,100);趙志崑見見ImageExample.javaclass ImagePanel extends JPanel private Image unitsImage = null;

12、private int unitX;private int unitY;public ImagePanel() Toolkit kit = Toolkit.getDefaultToolkit();unitsImage = kit.getImage(globe.gif);public void paintComponent(Graphics g) super.paintComponent(g);g.drawRect(10,10,100,100);g.drawImage(unitsImage,unitX,unitY,null);public void setUnitLocation(int aX,

13、 int aY) unitX = aX;unitY = aY;repaint();繪制圖像繪制圖像-1 -1drawImage(Image img, int dx, int dy, ImageObserver observer) 將img中圖片繪制到當(dāng)前畫布。globe.gifunitsImage趙志崑見見ImageExample1.javapublic void paintComponent(Graphics g) int sx1 = 118 * unitIndex;int sy1 = 0;int sx2 = sx1 + 117;int sy2 = sy1 + 97;int dx1 = un

14、itX;int dy1 = unitY;int dx2 = dx1 + 117;int dy2 = dy1 + 97;g.drawImage(unitsImage,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,null);public void setUnitLocation(int aX, int aY) unitIndex = (unitIndex + 1) % 4;repaint();繪制圖像繪制圖像-2 -2drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx

15、2, int sy2, ImageObserver observer) 將img中的一個(gè)矩形區(qū)域繪制到當(dāng)前畫布的一個(gè)矩形區(qū)域,且可拉伸。 透明的部分不繪制(gif圖片可以指定一個(gè)透明色),用于繪制不規(guī)則圖像。unitsImageduke.gif(dx1,dy1)(dx2,dy2)(sx1,sy1)0123(sx2,sy2)11898趙志崑等待圖像加載等待圖像加載問題: Java加載圖片文件(kit.getImage)的時(shí)候,采用異步的方式,即圖片文件可能還沒有加載完,getImage方法就返回了。這是為了適應(yīng)網(wǎng)絡(luò)速度慢下加載圖片的時(shí)間比較長(zhǎng)的情況。 此時(shí),如果使用圖片的數(shù)據(jù),則是錯(cuò)誤的。因此需

16、要一種機(jī)制等待圖片加載完成。解決: Java采用一個(gè)MediaTracker來跟蹤圖片的加載過程。見見ImageExample1.javapublic ImagePanel() Toolkit kit = Toolkit.getDefaultToolkit();unitsImage = kit.getImage(duke.gif);MediaTracker tracker = new MediaTracker(this);tracker.addImage(unitsImage,0);trytracker.waitForID(0);catch(Exception e)System.out.pri

17、ntln(e);使用MediaTracker的方法:1、創(chuàng)建一個(gè)MediaTracker對(duì)象;2、用addImage方法將正在裝入的圖片加入到MediaTracker對(duì)象,并賦予一個(gè)編號(hào);3、用waitForID(編號(hào))方法等待圖片裝入完成。趙志崑JavaJava支持的圖片格式支持的圖片格式 Java支持三種圖片格式: JPEG:Joint Photographic Experts Group,支持全24位色彩。它是通過精確地記錄每個(gè)像素的光亮但同時(shí)平均它們的色調(diào)的方法壓縮圖片,是有損壓縮。 GIF:Graphics Interchange Format,采用顏色索引的方式存儲(chǔ)圖片。一個(gè)GIF

18、圖片中只能有不多于256種的色彩,因此無法存儲(chǔ)高質(zhì)量照片。一個(gè)GIF文件可以包含幾張圖形以及每張圖形的持續(xù)值,以產(chǎn)生動(dòng)畫效果。它也有有限度的可透明性:調(diào)色板中的某個(gè)色彩可被指定為透明色。 PNG:Portable Network Graphics,無損壓縮,適合在網(wǎng)絡(luò)中傳播;具有8位、24位和32位三中色彩深度;支持Alpha通道透明( 32位)和色彩索引透明( 8位)。趙志崑Graphics2DGraphics2D Graphics類是一個(gè)抽象類,所以paintComponent(Graphics g)方法中的參數(shù)g不可能是一個(gè)Graphics類型的對(duì)象。參數(shù)g實(shí)際上是Graphics的子類

19、Graphics2D類型的對(duì)象。 Graphics2D類提供的功能比Graphics類強(qiáng)大,包括: 支持繪制更復(fù)雜的形狀,如二次曲線、三次曲線; 支持更復(fù)雜的坐標(biāo)變換,如旋轉(zhuǎn)等; 支持設(shè)置線型,如實(shí)線、虛線、線條粗細(xì); 支持更復(fù)雜的填充方法,如多種顏色著色。 要使用Graphics2D類的功能時(shí),可以直接進(jìn)行造型。public void paintComponent(Graphics g) super.paintComponent(g);Graphics2D g2 = (Graphics2D)g;/g2支持Graphics2D類的所有方法趙志崑緩沖區(qū)繪圖緩沖區(qū)繪圖 問題: Graphics類沒

20、有能夠取得某一點(diǎn)的顏色的方法。 組件沒有將圖片保存到文件的方法。 解決: BufferedImage類(Image類的子類)有取色功能(getRGB方法)和保存到文件功能。 采用雙緩沖區(qū)繪圖的方法,將BufferedImage和JPanel聯(lián)系起來: 創(chuàng)建一個(gè)BufferedImage對(duì)象,作為后臺(tái)繪圖緩沖區(qū); 所有繪圖操作均在BufferedImage對(duì)象上進(jìn)行; 繪制完成后,將BufferedImage對(duì)象整個(gè)繪制到JPanel上; 這樣,BufferedImage對(duì)象和JPanel的圖像將完全一樣。globe.gif(文件)unitImage(Image)backBuffer(Buffe

21、redImage)Graphics g(JPanel的paintComponent方法)趙志崑見見DoubleBufferExample.javaclass PhotoPanel extends JPanel private BufferedImage backImage = new BufferedImage(1024,800,BufferedImage.TYPE_BYTE_INDEXED); public void paintComponent(Graphics g) super.paintComponent(g);Graphics backG = backImage.getGraphics();backG.drawImage(photoImage,0,0,null);g.drawImage(backImage,0,0,null);backG.dispose(); public Color getColor(int x, int y) int c = backImage.getRGB(x,y);return new Color(c); 讀取圖像數(shù)據(jù)讀取圖像數(shù)據(jù)采用雙緩沖區(qū)繪圖的方法后,將BufferedImage和JPanel的圖像完全相同。因此,若想從JPanel某個(gè)位置

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論