1 / 46

繪圖與動畫

繪圖與動畫. Draw / Paint & Animation. 設定顏色. 顏色的設定有兩種方式 顏色常數. 設定顏色 ( 續 ). 自行指定顏色 以 R (ed) G (reen) B (lue) 三原色自行配製 R  0~255 G  0~255 B  0~255. 繪圖方法. paint() 方法 定義於 Component 類別中 要在容器上繪圖時,必須在程式中實作 paint() 方法,並提供一個 Graphics 類別的物件供繪圖之用 當元件需要繪圖時, Graphics 物件 g 自動的傳給 paint() 方法 語法

neena
Télécharger la présentation

繪圖與動畫

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 繪圖與動畫 Draw/Paint & Animation

  2. 設定顏色 • 顏色的設定有兩種方式 • 顏色常數

  3. 設定顏色(續) • 自行指定顏色 • 以R(ed)G(reen)B(lue)三原色自行配製 • R  0~255 • G  0~255 • B  0~255

  4. 繪圖方法 • paint()方法 • 定義於Component類別中 • 要在容器上繪圖時,必須在程式中實作paint()方法,並提供一個Graphics類別的物件供繪圖之用 • 當元件需要繪圖時,Graphics物件g自動的傳給paint()方法 • 語法 public void paint(Graphics g)

  5. 畫線、矩形 • 畫線 drawLine(int x1, int y1, int x2, int y2) • 畫矩形 drawRect(int x, int y, int width, int height) • 畫實心矩形 fillRect(int x, int y, int width, int height) • 畫圓角矩形 drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) • 畫實心圓角矩形 fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

  6. 畫弧、畫圓 • 畫弧 drawArc(int x1, int y1, int width, int height , int startangle , int arcAngle) • 畫實心弧 fillArc(int x1, int y1, int width, int height , int startangle , int arcAngle) • 畫圓 drawOval(int x, int y, int width, int height) • 畫圓角矩形 fillOval(int x, int y, int width, int height)

  7. 畫多邊形 • 畫多邊線 drawPolyline(int xPoints[], int ypoints[], int npoints) • 畫多邊形 drawPolygon(int xPoints[], int ypoints[], int npoints) • 畫實心多邊形 fillPolygon(int xPoints[], int ypoints[], int npoints)

  8. 畫文字 • drawString(String str, int x, int y) • drawString(String str, float x, float y)

  9. Graphics2D類別 • Graphics2D類別式一個抽象類別,繼承自Graphics類別,對幾何圖形提供「座標轉移」、「顏色控制」、「文字繪製」等等功能 • 提供給Graphics2D類別物件的座標是一個與設備無關的座標體系,稱為「用戶空間(user space)」,Graphics2D提供一個AffineTransform物件可將用戶空間的座標轉換為與設備有關的座標體系,稱為設備空間 • 每一個Graphics2D物件都結合一個GraphicsConfiguration物件,它定義繪製目標的特性,如繪點(pixel)的格式及解析度等特性

  10. Graphics2D的主要屬性(1) • paint屬性 public abstract void setPaint(Paint paint) • 提供繪製圖形線條的顏色

  11. Graphics2D的主要屬性(2) • stroke屬性 public abstract void setStroke(Stroke s) • 提供繪製圖形線條樣式

  12. Graphics2D的主要屬性(2) • 線條的樣式可由BasicStroke類別予以設定虛線樣式 BasicStroke(float width, int cap, int join, float miterlimit, float dash[], float dash_phase)

  13. Graphics2D的主要屬性(3) • font屬性 public abstract void setFont(Font font) • 提供繪製文字的字型

  14. Graphics2D的主要屬性(4、5) • transform屬性 • 提供繪製圖形線條的座標轉換體系 • clip屬性 public abstract void setClip(int x, int y, int w, int h) • 提供繪製圖形的邊界 • (x, y)左上角座標 • 圖形寬為w、高為h

  15. Graphics2D的主要屬性(6) • composite屬性 public abstract void setComposite(Composite comp) • 提供兩個繪製圖形的重疊屬性

  16. 圖形填充漸層色 • 以GradientPaint類別設定漸層色

  17. 圖形重疊顯示模式 • setXORMode()

  18. 顯示圖形 • 使用drawImage()方法 drawImage(Image img1, int x1, int y1, ImageObserver observer)

  19. 動畫 • 動畫的原理是一系列的圖片依一定的時間間隔依序顯示出來 • javax.swing的Timer介面即是用以控制時間間隔的類別 • 當要建立一個Timer類別物件時,必須提供兩個引數 • 延遲時間(以毫秒計) • 處理該動作事件的委託物件addActionListener() ,以start()方法啟動Timer物件

  20. Timer類別的建構子及常用方法 • 建構子 public Timer(int delay, ActionListener listener) • 傾聽物件 public void addActionListener(ActionListener listener) • 事件 • public int getDelay() //傳回延遲時間 • public boolean isRepeats() //傳回是否一直重複 • public boolean isRunning() //傳回Timer物件是否正在執行 • public void restart() //重新啟動Timer物件 • public void setDelay() //設定延遲時間 • public void setRepeats() //設定是否一直重複 • public void start() //啟動Timer物件 • public void stop() //停止Timer物件之執行

  21. 影像處理技巧 • 放大與縮小影像 • 原始尺寸 g.drawImage(img1,x,y,this) img1 : 影像檔名 x,y : 左上角座標位置 this : 目前所屬視窗容器 • 調整影像尺寸 g.drawImage(img1,x,y,w,h,this) w : 影像寬度 h : 影像長度

  22. 影像處理技巧(續) • 翻轉影像 • 原始影像 g.drawImage(img1,x1,y1,x2,y2,0,0,ow,oh,this) 0,0,ow,oh : 原始影像左上角和右下角的相對影像位置 x2=x1+ow y2=y1+oh • 上下翻轉 g.drawImage(img1,x1,y1,x2,y2,0,oh,ow,0,this) 將垂直座標交換 • 左右翻轉 g.drawImage(img1,x1,y1,x2,y2,ow,0,0,oh,this) 將水平座標交換 隨0,0,ow與oh值的變化可完成指定區域的局部翻轉

  23. 影像處理技巧(續) • 旋轉( rotate()方法 ) Graphics2D g2; g2.rotate(math.toRadians(10)); • 傾斜( shear()方法 ) Graphics2D g2; g2.shear(shx, shy); • shx : 水平方向的增量 • shy : 垂直方向的增量 • 平移( translate()方法 ) Graphics2D g2; g2.translate(tx, ty); • 目前的座標系統平移至tx, ty (新的原點座標)

  24. 影像處理技巧(續) • 影像裁減 • 首先利用Graphics2D繪製圖形的方法建立Shape圖形物件(必須匯入java.awt.geom.*) Shape sp1=new Ellipse2D.Double(100,100,250,150); g2.draw(sp1); • 接著使用setClip()方法以sp1為裁減區域 G2.setClip(sp1); • 最後使用drawImage()方法根據sp1的區域範圍將影像顯示出來 Rectangle r1=sh1.getBounds(); g2.drawImage(img1,r1.x,r1.y,r1.width,r1.height,this);

  25. Graphics2D圖形繪製主要方法 • draw() public abstract void draw(Shape s) • 繪製目前Graphics2D物件的圖形s輪廓 • fill() public abstract void fill(Shape s) • 填滿目前Graphics2D物件的圖形 • drawString() • 參考前面【畫文字】部分 • drawImage() • 參考前面【顯示圖形】部分

  26. Graphics2D的幾何圖形 • 定義於java.awt.geom程式套件中,因此程式必須匯入 import java.awt.geom.*; • 點 • Point2D.Float(x, y) Point2D.Float p1=new Point2D.Float(1.0f, 2.0f); • Point2D.Double(x, y) Point2D.Double p1=new Point2D.Double(3.0, 4.0);

  27. Graphics2D的幾何圖形(續) • 直線 • Line2D.Float • Line2D.Double • 矩形 • Rectangle2D.Float • Rectangle2D.Double • 圓角矩形 • RoundRectangle2D.Float • RoundRectangle2D.Double

  28. Graphics2D的幾何圖形(續) • 弧形 • Arc2D.Float • Arc2d.Double • 橢圓形 • Ellipse2D.Float • Ellipse2D.Double

  29. 選擇器(chooser) • 色彩選擇器 • javax.swing.JColorChooser JColorChooser cc1=new JColorChooser(); getContentPane().add(cc1,BorderLayout.CENTER); Or color1=showDialog(sample.this,“選取顏色”,color1);

  30. 選擇器(chooser)(續) • 檔案選擇器 • javax.swing.JFileChooser 必須匯入import javax.swing.filechooser.*; JFileChooser fc1=new JFileChooser(); fc1.showOpenDialog(new JFrame()); • 開啟檔名的指定 File file1; //必須匯入 java.io.*; file1=fc1.getSeletedFile(); String fname; fname=file1.getName();

More Related