java繪圖技術的詳解及實例
簡單實例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class demo1 extends jframe{ mypanel mp= null ; public static void main(string[] args){ demo1 demo= new demo1(); } public demo1(){ mp= new mypanel(); this .add(mp); this .setsize( 400 , 300 ); this .setdefaultcloseoperation(jframe.exit_on_close); this .setvisible( true ); } } //定義一個mypanel(我自己的面板,是用于繪圖和顯示繪圖的區域) class mypanel extends jpanel{ //覆蓋jpanel的paint方法 public void paint(graphics g){ //graphics是繪圖的重要類,可以理解成一只畫筆 //1、調用父類函數完成初始化(不可少) super .paint(g); // system.out.println("paint被調用"); g.drawoval( 10 , 10 , 30 , 30 ); //先畫出一個圓 g.drawline( 10 , 10 , 40 , 40 ); // 畫直線 g.drawrect( 10 , 10 , 40 , 60 ); //畫矩形邊框 g.setcolor(color.blue); //設置顏色 g.fillrect( 70 , 70 , 40 , 60 ); //填充矩形 g.setcolor(color.gray); g.fillrect( 150 , 150 , 30 , 40 ); //在面板上畫出圖片 image im=toolkit.getdefaulttoolkit().getimage(panel. class .getresource( "/imag_1" )); g.drawimage(im, 200 , 200 , 200 , 150 , this ); //顯示 //畫出字體 g.setcolor(color.green); g.setfont( new font( "隸書" ,font.bold, 30 )); g.drawstring( "祖國萬歲!" , 100 , 80 ); } } |
1、component類提供了兩個和繪圖相關最重要的方法
1)paint(graphics g)繪制組件外觀
2)repaint()刷新組件的外觀
當組件第一次在屏幕顯示時,程序會自動的調用paint()方法來繪制組件。
2、關于graphics類
運行效果如下:
以上就是java繪圖技術的詳解,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://www.cnblogs.com/cxq1126/p/7294014.html