標籤: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繪圖技術基礎