Java繪圖技術基礎

來源:互聯網
上載者:User

標籤:ima   fill   size   aws   visible   resource   opera   this   ram   

 1 public class Demo1 extends JFrame{ 2     MyPanel mp=null; 3      4     public static void main(String[] args){ 5         Demo1 demo=new Demo1(); 6     } 7      8     public Demo1(){ 9         mp=new MyPanel();10         this.add(mp);11         this.setSize(400,300);12         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);13         this.setVisible(true);14     }15 }16 //定義一個MyPanel(我自己的面板,是用於繪圖和顯示繪圖的地區)17 class MyPanel extends JPanel{18     //覆蓋JPanel的paint方法19     public void paint(Graphics g){        //Graphics是繪圖的重要類,可以理解成一隻畫筆20         //1、調用父類函數完成初始化(不可少)21         super.paint(g);22 //        System.out.println("paint被調用");23         g.drawOval(10, 10, 30, 30);          //先畫出一個圓24         g.drawLine(10, 10,40, 40);        // 畫直線25         g.drawRect(10, 10, 40, 60);       //畫矩形邊框26         27         g.setColor(Color.BLUE);            //設定顏色28         g.fillRect(70, 70, 40, 60);       //填充矩形29         30         g.setColor(Color.gray);31         g.fillRect(150, 150, 30,40);32         33         //在面板上畫出圖片34         Image im=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/imag_1"));35         g.drawImage(im, 200, 200, 200, 150, this);  //顯示36         //畫出字型37         g.setColor(Color.GREEN);38         g.setFont(new Font("隸書",Font.BOLD,30));39         g.drawString("祖國萬歲!", 100, 80);40         41     }42 }

1、Component類提供了兩個和繪圖相關最重要的方法

1)paint(Graphics g)繪製組件外觀

2)repaint()重新整理組件的外觀

當組件第一次在螢幕顯示時,程式會自動的調用paint()方法來繪製組件。

2、關於Graphics類

運行效果如下:

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.