JAVA學習AWT繪圖,javaawt繪圖

來源:互聯網
上載者:User

JAVA學習AWT繪圖,javaawt繪圖

package com.graphics;import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;/** * 1:Graphics類是所有圖形內容相關的抽象基類。 *  * 2:Graphics2D繼承了Graphics類,實現了功能更加強大的繪圖操作的集合。 * 由於Graphics2D類是Graphics類的擴充,也是推薦使用的java繪圖類 * 所以本章主要介紹使用Graphics2D類實現JAVA繪圖 *  * 3:Graphics類使用的不同的方法實現不同的繪製 * @author biexiansheng * */public class DrawCircle extends JFrame{    private final int OVAL_WIDTH=80;//圓形的寬    private final int OVAL_HEIGHT=80;//圓形的高    public DrawCircle(){        super();        initialize();//調用初始化方法    }    //初始化方法    private void initialize(){        this.setSize(300, 200);//設定表單的大小        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定表單的關閉方式        setContentPane(new DrawPanel());//設定表單面板為繪圖面板對象        this.setTitle("繪圖執行個體2");//設定表單標題    }        class DrawPanel extends JPanel{        public void paint(Graphics g){            super.paint(g);            g.drawOval(10, 10, OVAL_WIDTH, OVAL_HEIGHT);//繪製第1個圓形            g.drawOval(80, 10, OVAL_WIDTH, OVAL_HEIGHT);//繪製第2個圓形            g.drawOval(150, 10, OVAL_WIDTH, OVAL_HEIGHT);//繪製第3個圓形            g.drawOval(50, 70, OVAL_WIDTH, OVAL_HEIGHT);//繪製第4個圓形            g.drawOval(120, 70, OVAL_WIDTH, OVAL_HEIGHT);//繪製第5個圓形                    }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        DrawCircle dc=new DrawCircle();//初始化對象且調用構造方法        dc.setVisible(true);//表單可視化    }}

案例運行結果如下

 

package com.graphics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.geom.Ellipse2D;import java.awt.geom.Rectangle2D;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.WindowConstants;/** * 1:Graphics2D是繼承Graphics類編寫的,它包含了Graphics類的繪圖方法並添加了更強的功能 * 是推薦使用的繪圖類, * Graphics2D可以分別使用不同的類表示不同的形狀,如Line2D,Rectangle2D等。 *  * 2:要繪製指定形狀的圖形,需要先建立並初始化該圖類型的對象,這些圖形類必須是Shape介面 * 的實作類別,然後使用Graphics2D類的draw()方法繪製該繪圖物件或者使用fill()方法填充 * 該繪圖物件 * 文法如下 * draw(Shape form)或者fill(Shape form) * 其中form指實現Shape介面的對象。 * @author biexiansheng * */public class DrawFrame extends JFrame{    public DrawFrame(){        super();        initialize();//調用初始化方法    }    //初始化方法    public void initialize(){        this.setSize(300, 200);//設定表單大小        //設定表單的關閉方式        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        add(new CanvasPanel());//設定表單面板為繪圖面板對象        this.setTitle("繪圖執行個體2");//設定表單標題    }        //建立內部類    class CanvasPanel extends JPanel{        public void paint(Graphics g){            super.paint(g);            Graphics2D g2=(Graphics2D)g;//強制類型轉換位Graphics2D類型            //Graphics2D是推薦使用的繪圖類,但是程式設計中提供的繪圖對象大多是            //Graphics類的執行個體,這時應該使用強制類型轉換將其轉換為Graphics            Shape[] shapes=new Shape[4];//聲明圖形數組            shapes[0]=new Ellipse2D.Double(5, 5, 100, 100);//建立圓形對象            //建立矩形對象            shapes[1]=new Rectangle2D.Double(110,5,100,100);//            shapes[2]=new Rectangle2D.Double(15, 15, 80, 80);//建立矩形對象            shapes[3]=new Ellipse2D.Double(120, 15, 80, 80);//建立圓形對象            for(Shape shape:shapes){//遍曆圖型數組                Rectangle2D bounds=shape.getBounds2D();                if(bounds.getWidth()==80){                    g2.fill(shape);//填充圖形                }else{                    g2.draw(shape);//繪製圖形                }            }                    }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        DrawFrame df=new DrawFrame();        df.setVisible(true);    }}

案例運行結果如下,

可以設定顏色,或可以直接使用g2.setColor(Color.RED);

                                                       

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.