圖形圖像和多媒體.ppt_第1頁
圖形圖像和多媒體.ppt_第2頁
圖形圖像和多媒體.ppt_第3頁
圖形圖像和多媒體.ppt_第4頁
圖形圖像和多媒體.ppt_第5頁
已閱讀5頁,還剩39頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

圖形 圖象與多媒體 本章重點設置字型設置顏色幾何圖形繪制方法圖像顯示技術基礎 繪圖基礎 基本圖形包括點 線 圓 矩形等 是構成復雜圖形的基礎 繪制基本圖形要使用AWT中的Graphics類 它提供了各種基本圖形的繪制方法 可以直接引用這些方法 確定平面坐標系 坐標原點 0 0 位于整個區(qū)域的左上角 一個坐標點對應屏幕上的一個像素 必須是整數(shù) Graphics類的基本功能 java awt包中的Graphics提供建立字體 設定顏色 顯示圖像 文本 繪制和填充各種圖形的功能 Graphics2D類繼承Graphics提供更多的狀態(tài)和屬性 使應用程序能繪制出更加豐富多彩的圖形 要在某個組件中繪圖 需要在這個組件所屬的類中重寫paint 方法 在該方法中繪圖 如果要在JComponent子類的組件中繪圖 則需重寫paintComponent 方法 自型和顏色 繪制文本的方法有三種 在指定的位置繪制字符串drawString Stringstr intx inty g drawString 中國Wxyz 10 60 10 60 字型和顏色 文字字型有三個要素 字體 name 風格 style 字號 size 字體 宋體 黑體 TimesNewRoman等風格 Font PLAIN 正常 Font BOLD 粗體 Font ITALIC 斜體 Font BOLD Font ITALIC字號 整數(shù) 單位是磅Java中Font類的對象代表字體Fontfont newFont 宋體 Font PLAIN 12 繪圖時使用Graphics對象的setFont方法設置字體 字型和顏色 Java中使用Color類設置顏色 生成顏色的方法有兩種 使用預定義的顏色 Color RED Color YELLOW等通過紅綠藍 RGB 的值合成顏色 例如使用3個0 255的整數(shù)創(chuàng)建對象Colorc newColor 255 0 0 繪圖時使用Graphics對象的setColor 方法設置顏色 使用Component對象的setBackground 方法設置背景色 繪圖模式 繪圖模式指繪制的圖形與之前繪制的圖形有重疊時 重疊部分的顏色如何確定 正常模式 后繪制的圖形覆蓋原先的圖形 setPaintMode 異或模式 將正要繪圖的顏色 原先圖形的顏色和設定的顏色做特定的運算 得到實際的繪圖顏色 setXORMode g setColor Color BLUE g drawString dsbdsfsfs 20 50 g setColor Color YELLOW g fillRect 10 10 100 50 g setColor Color BLUE g drawString dsbdsfsfs 20 50 g setColor Color YELLOW g fillRect 10 10 100 50 XOR繪圖模式 setBackground Color yellow 設此顏色為Bg setXORMode Color red 設此顏色為Cg setColor Color green 設此顏色為D規(guī)則1 用背景色畫圖出現(xiàn)設置的顏色CB B CsetBackground Color yellow g setXORMode Color red g setColor Color YELLOW g fillRect 20 20 80 40 紅色 XOR繪圖模式 規(guī)則2 一個圖形重畫時會清除原有圖形D D BsetBackground Color yellow g setXORMode Color red g setColor Color BLUE g fillRect 20 20 80 40 g fillRect 20 20 80 40 清除 XOR繪圖模式 規(guī)則3 背景色和繪圖顏色不一樣時 為兩者的混合色B D B和D的混合色setBackground Color yellow g setXORMode Color red g setColor Color BLUE g fillRect 20 20 80 40 黃 藍 XOR繪圖模式 規(guī)則4 某區(qū)已經(jīng)用D著色 再用E著色D E B和E的混合色 B和E不同 setBackground Color yellow g setXORMode Color red g setColor Color BLUE g fillRect 20 20 80 40 g setColor Color GREEN g fillRect 20 20 40 40 黃 藍 綠 Graphics的繪圖方法 畫線段drawLine intx1 inty1 intx2 inty2 普通矩形drawRect intx inty intwidth intheight 用線框圍起來的矩形fillRect intx inty intwidth intheight 填充矩形圓角矩形drawRoundRect intx inty intwidth intheight intarcW intarcH 用線框圍起來的圓角矩形fillRoundRect intx inty intwidth intheight intarcW intarcH 填充圓角矩形如果后四個參數(shù)相等 畫出來的是圓形 Graphics的繪圖方法 三維矩形draw3DRect intx inty intwidth intheight booleanraised fill3DRect intx inty intwidth intheight booleanraised raised為true表示突出的 false是凹陷的畫橢圓drawOval intx inty intwidth intheight fillOval intx inty intwidth intheight 畫以 x y 為原點 即矩形的左上角 寬為width 高為height的矩形的內(nèi)切橢圓 Graphics的繪圖方法 畫圓弧drawArc intx inty intwidth intheight intstartAngle intarcAngle fillArc intx inty intwidth intheight intstartAngle intarcAngle 得到的弧由startAngle開始 并以當前顏色轉arcAngle度 角度的0度位于3點鐘位置 正值指示逆時針旋轉 負值則指示順時針旋轉 g drawRect 5 5 100 50 g drawArc 5 5 100 50 0 360 g drawRect 5 5 100 50 g drawArc 5 5 100 50 0 270 g drawRect 5 5 100 50 g drawArc 5 5 100 50 90 90 Graphics的繪圖方法 畫多邊形drawPolygon int xPoints int yPoints intnPoints fillPolygon int xPoints int yPoints intnPoints int px1 50 90 10 50 int py1 10 50 50 10 g fillPolygon px1 py1 4 50 10 90 50 10 50 Graphics的繪圖方法 使用Polygon類創(chuàng)建多邊形 Polygonp newPolygon p addPoint 50 10 p addPoint 90 50 p addPoint 10 50 p addPoint 50 10 g fillPolygon p 50 10 90 50 10 50 Graphics的繪圖方法 擦除矩形塊用背景色填充一個矩形塊 相當于擦除矩形塊位置的圖形clearRect intx inty intwidth intheight 50 10 90 50 10 50 int px1 50 90 10 50 int py1 10 50 50 10 g fillPolygon px1 py1 4 g clearRect 35 30 15 15 clipRect 限定作圖顯示區(qū)域 區(qū)域內(nèi)的圖形可以顯示clipRect intx inty intwidth intheight g setColor Color YELLOW g fillRect 10 10 100 100 g clipRect 20 20 50 50 g setColor Color YELLOW g fillRect 10 10 100 100 復制圖形 copyArea intx inty intwidth intheight intdx intdy x 源矩形的x坐標 y 源矩形的y坐標 width 源矩形的寬度 height 源矩形的高度 dx 水平偏移 右為正 左為負dy 垂直偏移 下為正 上為負 g setColor Color YELLOW g fillRect 0 0 100 100 g setColor Color red g drawRect 30 30 50 50 g copyArea 30 30 50 50 100 100 paint repaint update方法 paint 應該繪制組件的內(nèi)容時調(diào)用此方法 組件第一次在屏幕上顯示 組件的大小改變了 部件顯示的內(nèi)容受損需要維護 比如 先前擋住組件的其它物體移走了 于是組件被擋住的部分曝露出來 repaint 對于重量級組件 JFrame JApplet JDialog和awt組件 先調(diào)用update方法 在update方法中實現(xiàn)部分圖形的修改 然調(diào)用paint方法 對于輕量級 JComponent的子孫 組件調(diào)用paint方法 Graphics2D類的繪圖方法 java awt Graphics2D擁有更強大二維的圖形處理能力 提供坐標變換 顏色管理以及文字布局等更精確的控制 publicabstractclassGraphics2DextendsGraphics Stroke屬性 Stroke屬性控制線條的寬度 筆形的樣式 線段連接方式或短劃線圖案 該屬性的設置需要創(chuàng)建BasicStroke對象 再調(diào)用setStroke 方法來設置 cap樣式 join樣式 publicvoidpaint Graphicsg Graphics2Dg2d Graphics2D g Line2Dline newLine2D Double 30 0 10 0 180 0 10 0 g2d draw line BasicStrokebs newBasicStroke 20f BasicStroke CAP BUTT BasicStroke JOIN ROUND g2d setStroke bs Line2Dline2 newLine2D Double 30 0 40 0 180 0 40 0 g2d draw line2 bs newBasicStroke 20f BasicStroke CAP SQUARE BasicStroke JOIN ROUND g2d setStroke bs Line2Dline3 newLine2D Double 30 0 80 0 180 0 80 0 g2d draw line3 bs newBasicStroke 20f BasicStroke CAP ROUND BasicStroke JOIN ROUND g2d setStroke bs Line2Dline4 newLine2D Double 30 0 120 0 180 0 120 0 g2d draw line4 publicvoidpaint Graphicsg bs newBasicStroke 20f BasicStroke CAP BUTT BasicStroke JOIN ROUND g2d setStroke bs int x 30 160 200 int y 160 400 200 g2d setColor Color GREEN g2d drawPolygon x y 3 JOIN ROUND JOIN MITER JOIN BEVEL paint屬性 paint屬性控制填充效果 BasicStrokebs newBasicStroke 5 5f g2d setStroke bs GradientPaintgp newGradientPaint 30 0f 30 0f Color RED 180 0f 30 0f Color GREEN g2d setPaint gp Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform屬性 Transform屬性用來實現(xiàn)常用圖形的平移 縮放 旋轉和斜切變換等操作 旋轉變換 AffineTransformrotate AffineTransform getRotateInstance Math PI 2 30 0 30 0 g2d setTransform rotate Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line AffineTransformrotate newAffineTransform rotate rotate Math PI 2 30 0 150 0 g2d setTransform rotate Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform屬性 縮放 AffineTransformscale AffineTransform getScaleInstance 2 2 g2d setTransform scale Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform屬性 平移 AffineTransformtransform AffineTransform getTranslateInstance 10 50 g2d setTransform transform Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line transform屬性 斜切 AffineTransformshear AffineTransform getShearInstance 5 2 g2d setTransform shear Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d draw line clipe屬性 clipe屬性用于實現(xiàn)剪裁的效果 區(qū)域內(nèi)可見Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 g2d setClip 30 30 160 30 g2d draw line Composit屬性 Composit設置圖形重疊區(qū)的效果AlphaCompositea AlphaComposite getInstance intrule floatalpha Alpha為0表示透明 1 0表示不透明 Graphics2D的繪圖方法 Graphics2D類保留了Graphics類的繪圖方法 同時增加了一些新方法 新方法把幾何形狀作為對象來繪制 在java awt geom包中聲明的一系列類 分別用于創(chuàng)建各種幾何圖形 publicvoidpaint Graphicsg 將參數(shù)g強制類型轉換成Graphics2D類型Graphics2Dg2d Graphics2D g 使用圖形類的靜態(tài)Double方法創(chuàng)建幾何圖形對象Line2Dline newLine2D Double 30 0 30 0 180 0 30 0 以圖形對象為參數(shù) 調(diào)用Graphics2D對象的draw方法繪制圖形g2d draw line Graphics2D的幾何圖形類 線段起點 30 30 終點 180 30 Line2Dline newLine2D Double 30 30 180 30 g2d draw line publicabstractclassLine2DimplementsShape Cloneable publicstaticclassDoubleextendsLine2D publicDouble doubleX1 doubleY1 doubleX2 doubleY2 Graphics2D的幾何圖形類 矩形左上角點坐標 20 20 寬100 高50Rectangle2Drect newRectangle2D Double 20 20 100 50 g2d draw rect g2d fill rect Graphics2D的幾何圖形類 圓角矩形左上角點坐標 20 20 寬100 高50 圓角橫向直徑20 縱向直徑40RoundRectangle2DroundRect newRoundRectangle2D Double 20 20 100 50 20 40 g2d draw roundRect Graphics2D的幾何圖形類 橢圓左上角點 20 30 寬100 高50矩形的內(nèi)切橢圓Ellipse2Dellipse newEllipse2D Double 20 30 100 50 g2d draw ellipse Graphics2D的幾何圖形類 圓弧左上角坐標 20 30 寬100 高50矩形的內(nèi)切圓弧 90度開始 逆時針轉90度Arc2Darc newArc2D Double 20 30 100 50 90 90 Arc2D OPEN g2d draw arc Arc2Darc2 newArc2D Double 20 30 100 50 0 90 Arc2D CHORD g2d draw arc2 Arc2Darc3 newArc2D Double 20 30 100 50 0 90 Arc2D PIE g2d draw arc3 二次曲線 一條二次曲線需要三個點確定 始點 控制點和終點起點 20 10 控制點 90 65 終點 55 115 QuadCurve2Dqc newQuadCurve2D Double 20 10 90 65 55 115 QuadCurve2Dqc2 newQuadCurve2D Double 20 10 15 63 55 115 QuadCurve2Dqc3 newQuadCurve2D Double 20 10 54 64 55 115 g2d draw qc g2d

溫馨提示

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

評論

0/150

提交評論