如下所示:
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(我自己的面板,是用于繪圖和顯示繪圖的區(qū)域) class mypanel extends jpanel{ //覆蓋jpanel的paint方法 public void paint(graphics g){ //graphics是繪圖的重要類,可以理解成一只畫筆 //1、調(diào)用父類函數(shù)完成初始化(不可少) super .paint(g); // system.out.println("paint被調(diào)用"); g.drawoval( 10 , 10 , 30 , 30 ); //先畫出一個圓 g.drawline( 10 , 10 , 40 , 40 ); // 畫直線 g.drawrect( 10 , 10 , 40 , 60 ); //畫矩形邊框 g.setcolor(color.blue); //設(shè)置顏色 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類提供了兩個和繪圖相關(guān)最重要的方法
1)paint(graphics g)繪制組件外觀
2)repaint()刷新組件的外觀
當(dāng)組件第一次在屏幕顯示時,程序會自動的調(diào)用paint()方法來繪制組件。
2、關(guān)于graphics類
運行效果如下:
以上這篇java繪圖技術(shù)基礎(chǔ)(實例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。