JAVA---圖形處理

來源:互聯網
上載者:User

標籤:字元   aws   font   ntb   date   作用   sam   介面   允許   

抽象視窗化工具(AWT)為圖形化使用者介面提供了API編程介面,使得java可以提供較好的圖形化使用者介面。AWT把圖形處理分為兩層:一是處理原始圖形,直接以點線面的形式畫到介面上;二是提供大量組件,實現可定製的圖形化使用者介面。JAVA程式設計語言中的座標系不同於數學中的座標系,螢幕左上方為(0,0),右下角為螢幕水平向右和垂直向下增長的像素。

方法:

1:public void paint(Graphics g)

2:Repaint :Applet重畫系統時自動調用paint方法。

3:Update方法 :public void Update(Graphics g)  更新容器,向Repaint發出重新整理小應用程式的訊號,預設的Update方法清除Applet畫圖區並調用paint方法。

Graphics類

Graphics類是所有圖形內容相關的抽象類別,允許應用程式在各種裝置上實現組件的畫圖。繪圖物件封裝了JAVA支援的基本渲染操作的狀態資訊,包括畫圖的組件對象、渲染地區的座標(coordinates)、地區(clip)、顏色(Color)、字型(Font)、畫圖模式等。Graphics類提供畫各種圖形的方法,其中包括線、圓和橢圓、矩形、多邊形、映像及各種字型等。

public abstract void clipRect(int x,int y,int width,int height);     指定的地區劃分。

public abstract void drawLine(int x1,int y1,int x2,int y2);         使用使用中色彩,在(x1,y1)和(x2,y2)之間畫線

public  abstract void  drawOval(int x1,int y1,int width,int height);            畫橢圓

public  abstract   void fillOval(int x1,int y1,int width,int height);            畫實心橢圓

public  abstract  void drawPolygon(int[] xPoints,int[] yPoints,int nPoints);   畫x和y座標定義的多邊形

public abstract  void drawRect(int x1,int y1,int width,int height);       畫矩形

public  abstract  void  fillOval(int x,int y,int width,int height);        畫實心矩形

public  abstract  void drawRoundRect(int x1,int y1,int width,int height,int arcwidth,int archeight);     使用使用中色彩畫圓角矩形

public abstract   void drawString(String str,int x,int y);使用當前字型和顏色畫字串str。

public abstract  void setColor(Color c);   設定圖形內容相關的顏色。

public abstract  void setPaintMode(); 設定畫模式

public  abstract  boolean  drawImage(Image img,int x,int y,ImageObserver observer);          畫特定圖;

public abstract void setFont(Font font); 設定特定的font字型;      public Font(String name,int style,int size);

eg: Font f=new Font("TimesName", Font.BOLD+Font.ITALIC,12)

例子:

import java.awt.*;

import java.applet.*;

import java.geom.*;

public  class SimpleGUI extends Applet{

          Image samImage;

          public  void init(){

                       samImage=getImage(getDocumentBase(),"sample.gif");

                       }

         public  void paint(Graphics g){

                     g.clipRect(50,50,180,180);

                      g.drawLine(0,0,20,30);

                      g.drawString("圖形顯示",100,30);

                     Color c=new Color(255,200,0);

                     g.setColor(c);

                    Font f=new Font("TimesRomn",Font.BOLD+Font.ITALIC,24);

                    g.setFont(f);

                    g.drawString("圖形顯示",180,30);

                   g.drawLine(20,20,100,50);

                   g.drawLine(20,20,50,100);

                   g.drawRect(40,40,40,40);

                   g.fillRect(60,60,40,40);

                  g.setColor(Color.red);

                   //3D矩形

                  g.draw3DRect(80,80,40,40,true);

                   g.draw3DRect(120,120,40,40,true);

                  g.filll3DRect(120,210,40,40,true);

                   //橢圓形

                  g.drawOval(150,150,30,40);

                  g.fillOval(170,170,20,20);

                  g.setColor(Color.blue);

                 //圓角矩形

                 g.drawRoundRect(180,180,40,40,20,20);

                 g.fillRoundRect(200,200,40,40,20,20);

                 //多邊形

                  int xC[]={242,260,254,297,242};

                  int yC[]={240,243,290,300,270};

                 g.drawPolygon(xC,yC,5);

                 //圖片

                g.drawImage(samImage,250,50,this);

}

}

Color類

Color類是用來封裝顏色的,使用Color類中較簡單的方法就是使用Color提供的預定義顏色,紅色:Color.red;橙色:Color.orange等。也可以使用RGB顏色模式進行定義,所謂的RGB 即為:red,green,blue三種顏色進行調合,三種基色的範圍是:0~255;

常用的顏色RGB值以及對應的類預定義的參數:

白色:Color.white; (255,255,255)。淺灰色:Color.lightGray;(192,192,192)。灰色:Color.gray;(128,128,128)。深灰色:Color.drakGray;(64,64,64)。黑色:Color.drak;(0,0,0,)。

紅色:Color.red;(255,0,0)。粉紅色:Color.pink;(255,175,175)。橙色:Color.orange;(255,200,0)。黃色:Color.yellow;(255,255,0)。綠色:Color.green;(0,255,0)。品紅色:Color.magenta;(255,0,255)。深藍色:Color.cyan;(0,255,255)。藍色:Color.blue;(0,0,255)。

Color還可以構造一個函數,public Color(int red,int green,int blue,int alpha);   alpha是指透明度,為255是完全不透明,可以正常顯示;當alpha分量為0時,表示完全透明,前三個分量不起作用,而介於0~255之間的值可以製造出顏色不同的層次效果。

eg:

      import java.awt.*;

      import java.applet.*;

      import java.awt.geom.*;

      public  class UseColor extends Applet{

                  public void paint(Graphics oldg){

                                Graphics2D g=(Graphics2D)oldg;

                                g.setColor(Color.blue);

                               g.fill(new Ellipse2D.Float(50,50,150,150));

                               g.setColor(new Color(255,0,0));

                               g.fill(new Ellipse2D.Float(50,50,140,140));

                               g.setColor(new Color(255,0,64));

                               g.fill(new Ellipse2D.Float(50,50,130,130));

                                g.setColor(new Color(155,0,0,128));

                               g.fill(new Ellipse2D.Float(50,50,110,110));

                                g.setColor(new Color(255,0,0,255));

                               g.fill(new Ellipse2D.Float(50,50,90,90));

                              g.setColor(new Color(255,200,0));

                               g.fill(new Ellipse2D.Float(50,50,70,70));

                         }

}

 

JAVA---圖形處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.