版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Java的GUI設(shè)計(jì) 1 java.awt 2 AWT組件 3 窗口和菜單設(shè)計(jì) 4 布局管理 5 Java圖形設(shè)計(jì) 6 Java2D,1 java.awt 抽象窗口工具集(Abstract Window Tools) (1)基本控制組件 Button Checkbox Choice List Menu Textfield (2)復(fù)雜控制組件Canvas Textarea (3)其他控制組件 Scrollbar Label (4)容器 是一種特殊的組件,用來(lái)包含其他組件Panel Windows Dialog Filedialog Frame,Component,Button Canvas Ch
2、eckbox Label List Scrollbar Textfield Textarea,Container,Panel,Window,Frame,Dialig,Filedialig,AWT的功能包括以下方面: (1)豐富的圖形界面組件; (2)強(qiáng)大的事件處理模型圖形和圖象工具,包括形狀、顏色、字體; (3)布局管理器,可以進(jìn)行靈活的窗口布局而與特定窗口的尺寸和屏幕分辨率; (4)無(wú)關(guān)數(shù)據(jù)傳送類,可以通過(guò)本地平臺(tái)的剪貼板來(lái)進(jìn)行剪切和粘貼操作; (5)打印和無(wú)鼠標(biāo)操作。 java.awt是java基本包中最大的一個(gè),其中定義了所有GUI組件類,以及其他用于構(gòu)造圖形界面的類,如字體類Font、
3、繪圖類Graphics和圖像類Image等。表6-1列出了AWT中的主要軟件包。,Component類的主要方法有: void enable():使組件可用 void disable():使組件不可用 void show():顯示組件 void paint():繪制圖形 void repaint():刷新,Container類的主要方法: void add(Component c):將指定組件c加入到容器中 void SetLayout():設(shè)置布局管理器,import java.awt.*; public class no61 extends Frame public no61() setT
4、itle(ButtonExample); setLayout(new FlowLayout(); add (new Button(push me); pack(); show();/設(shè)置布局,加入按鈕 ,public boolean action(Event e,Object arg) /事件驅(qū)動(dòng) System.out.println(Button)e.target).getLabel(); return true; public static void main(String args) new no61(); ,import java.applet.*; import java.awt.*
5、; public class no62 extends Applet TextField txt; public void init() txt=new TextField(10); add(txt); add(new Button(show); public boolean action(Event evt,Object arg) showStatus(txt.getText(); return true; ,import java.applet.*; import java.awt.*; public class no62 extends Applet Label l1; TextFiel
6、d txt1; Label l2; Button b1; Button b2; Label l3;,public void init() l1=new Label(1+1=); add(l1); txt1=new TextField(1); add(txt1); b1=new Button(ok); add(b1); b2=new Button(cacel); add(b2); l2=new Label(Your answer is:); add(l2); l3=new Label( init ); add(l3);,public boolean action(Event evt,Object
7、 arg) String caption=(String)arg; if(evt.target instanceof Button) if(caption=ok) int w=Integer.valueOf(txt1.getText().intValue(); if(w=2) l3.setText(right!); else l3.setText(wrong!); if(caption=cacel) txt1.setText(); l3.setText(); return true;,通過(guò)以上例子說(shuō)明以下兩點(diǎn): (1) Java的GUI設(shè)計(jì)既可用于Java Application,也可用于Ja
8、va Applet。 (2) Java的GUI設(shè)計(jì)包括以下方面: 界面上放置哪些組件,每個(gè)組件的功能及初始值是什么。 這些組件以什么樣的布局放置。以上兩例因組件較少,因此采用缺省的流式布局,本章后面會(huì)詳細(xì)介紹Java布局管理器。 如何進(jìn)行事件處理。即會(huì)發(fā)生哪些事件,相應(yīng)進(jìn)行什么處理。以上兩例使用JDK1.0中的action方法中實(shí)現(xiàn)了事件驅(qū)動(dòng);在后面一章將詳細(xì)介紹Java的事件處理機(jī)制。,2 AWT組件 1文本域 TextField 文本域一般用來(lái)讓用戶輸入象姓名、信用卡號(hào)這樣的信息,它是一個(gè)能夠接收用戶的鍵盤輸入的小塊區(qū)域。 (1)創(chuàng)建文本域 (構(gòu)造方法) 在創(chuàng)建文本域時(shí),有四種類型 .,T
9、extField t1,t2,t3,t4,t1=newTextField(); /空的文本域,t2=newTextField(20); /長(zhǎng)度為20的空的文本域,t3=newTextField(“你好”);/帶有初始文本內(nèi)容的文本域,t4=newTextField(“你好”,30);/帶有初始文本內(nèi)容并具有指定長(zhǎng)度的文本域,(2)主要方法 t1=setText(“AAA”); String S=t2.getText();,2 按鈕 Button (1)創(chuàng)建按鈕 Button b1=new Button(“OK”); (2)主要方法 b1.setLabel(“CACEL”); String S=
10、b1.getLabel();,3 靜態(tài)文本 Label Label L1=new Label(“INPUT:”); L1.setText(“NAME:”);,import java.applet.*; import java.awt.*; public class guiexample1 extends Applet public void init() Label l1=new Label(用戶名:); add(l1); TextField txt1=new TextField(8); add(txt1); Label l2=new Label(密碼 :); add(l2);,TextFie
11、ld txt2=new TextField(8); txt2.setEchoChar(*); add(txt2); Button b1=new Button(登錄); add(b1); Button b2=new Button(取消); add(b2); ,4文本區(qū)域TextArea 四種構(gòu)造方法. 其他方法: getRows(),setRows(),getColumns(),setColumns(),append(),insert(),5復(fù)選框 Checkbox 構(gòu)造方法:五種 其他主要方法: getLabel() ,setLabel(), getState(),setTate(),6單選鈕
12、 CheckboxGroup 創(chuàng)建單選鈕 : CheckboxGroup checkboxgroup1; Checkbox c1,c2; Checkboxgroup1=new CheckboxGroup(); C1=new Checkbox (“option1”, checkboxgroup1,true); C2=new Checkbox (“option2”, checkboxgroup1,false); 其他主要方法: getselectedCheckbox() setselectedCheckbox(),7 滾動(dòng)條 Scrollbar 構(gòu)造方法:三種 其他主要方法: getMaximum
13、() getMinimum() getValue() setMaximum() setMinimum() setValue(),8 下拉列表 Choice add() getItem() getSelectedIndex() getSelectedItem() select(int) select(String),9 滾動(dòng)表,import java.applet.*; import java.awt.*; public class guiexample2 extends Applet public void init() Label l1=new Label(姓名:); add(l1); Te
14、xtField txt1=new TextField(8); add(txt1); Label l2=new Label(性別 :); add(l2);,CheckboxGroup group1= new CheckboxGroup(); Checkbox c1=new Checkbox(男,group1, true); add(c1); Checkbox c2=new Checkbox(女,group1, false); add(c2); Label l3=new Label(愛好 :); add(l3); Checkbox c3=new Checkbox(文學(xué)); add(c3); Che
15、ckbox c4=new Checkbox(音樂); add(c4);,Checkbox c5=new Checkbox(體育); add(c5); Label l4=new Label(簡(jiǎn)歷 :); add(l4); TextArea txt2=new TextArea(簡(jiǎn)歷 :); txt2.setRows(10); add(txt2); Label l5=new Label(民族:); add(l5);,List list1=new List(3); list1.add(漢); list1.add(回); list1.add(其他); add(list1); Label l6=new L
16、abel(政治面貌:); add(l6); Choice choice1=new Choice(); choice1.addItem(團(tuán)員); choice1.addItem(黨員); choice1.addItem(其他); add(choice1);,10 畫布Canvas 為圖形操作提供容器平臺(tái),進(jìn)而在其上進(jìn)行圖形操作. Paint()方法 getPreferredSize()方法 11面板 panel 包含其他控件的控件 12 Applet Applet本身不在AWT包中,但它是Panel的子類,可以繼承Panel的所有方法。我們可以在Applet中放置各種組件或面板,也可在Apple
17、t中繪圖,import java.awt.*; import java.applet.*; public class CanvasExample extends Applet public void init() MyCanvas canvas=new MyCanvas();/創(chuàng)建畫布 this.add(canvas);/在Applet容器中顯示畫布 class MyCanvas extends Canvas/擴(kuò)展Canvas public void paint(Graphics g) ,Dimension size=this.getSize(); g.drawRect(0,0,size.wi
18、dth-1,size.height-1);/繪制畫布的外圍矩形區(qū)域 g.setColor(Color.blue);/用藍(lán)色繪制字符串 g.drawString(THis is Canvas,120,20); g.setColor(Color.red);/用紅色繪制填充矩形 g.fillRect(10,30,60,60); g.setColor(Color.green);/用綠色繪制線條 g.drawLine(120,150,20,90); public Dimension getPreferredSize() return new Dimension(200,200);/ 覆蓋此方法設(shè)置畫布大
19、小,不然無(wú)法正確顯示 ,3 窗口和菜單設(shè)計(jì) 3.1 彈出式窗口Frame (1)構(gòu)造方法 Frame() Frame(String) (2)其他方法,3.2對(duì)話框Dialog組件 用來(lái)顯示或者提示信息的彈出式窗口 Frame與Dialog的區(qū)別: Dialog不能實(shí)現(xiàn)菜單容器 Dialog依賴與其他的窗口 3.3文件對(duì)話框FileDialog 用來(lái)打開和保存文件時(shí)讓用戶選擇用戶名,import java.awt.event.*; import java.awt.*; public class Frame1 extends Frame public Frame1() setTitle(Frame
20、1); /設(shè)置窗口的標(biāo)題 setLayout(new FlowLayout(); /設(shè)置布局 l1=new Label(1+1=); add(l1); txt1=new TextField(1); add(txt1); b1=new Button(ok); add(b1);,b2=new Button(cacel); add(b2); l2=new Label(Your answer is:); add(l2); l3=new Label( init ); add(l3); /以上為在Frame上放置多個(gè)組件 setResizable(false); /設(shè)置窗口不可縮放 Label l1; T
21、extField txt1; Label l2; Button b1; Button b2; Label l3;,public static void main(String args) Frame1 nowFrame=new Frame1(); nowFrame.addWindowListener(new WindowAdapter() /窗口事件處理 public void windowClosing(WindowEvent e) System.exit(0); ); nowFrame.pack(); nowFrame.show(); 說(shuō)明:Frame1是Frame的子類,在這里覆蓋了Fr
22、ame的構(gòu)造方法。在main()方法中,除了創(chuàng)建Frame1對(duì)象,顯示窗口外,還加入一個(gè)窗口事件處理,即當(dāng)關(guān)閉窗口后,關(guān)閉整個(gè)系統(tǒng)。,import java.awt.event.*; import java.awt.*; public class guiexample1 extends Frame private Button b1=new Button(登錄); private Button b2=new Button(重置); private Button db1=new Button(確定); private Dialog d=new Dialog(this,登錄結(jié)果,false); p
23、rivate Label l3=new Label(); TextField txt1,txt2; public guiexample1(String ss) super(ss); d.setLayout(new FlowLayout();,d.add(l3); d.add(db1); db1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) d.dispose(); ); d.setSize(200,100); setLayout(new FlowLayout(); Label
24、l1=new Label(用戶名:); add(l1); txt1=new TextField(5); add(txt1);,Label l2=new Label(密碼 :); add(l2); txt2=new TextField(8); txt2.setEchoChar(*); add(txt2); b1.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) b1_actionPerformed(e); ); add(b1); b2.addActionListener(new Ac
25、tionListener() public void actionPerformed(ActionEvent e),txt1.setText(); txt2.setText(); ); add(b2); protected void b1_actionPerformed(ActionEvent e) String s1=txt1.getText(); String s2=txt2.getText(); if(s1.equals(admin) ,public static void main(String args) guiexample1 nowFrame=new guiexample1(di
26、alog example ); nowFrame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); nowFrame.pack(); nowFrame.show(); ,import java.awt.event.*; import java.awt.*; public class filedialog extends Frame public filedialog(String ss) super(ss); setLayout(new FlowLa
27、yout(); Label l1=new Label(init); Label l2=new Label(init); add(l1); add(l2); FileDialog dfiledialog =new FileDialog(filedialog.this); dfiledialog.setTitle(保存文件對(duì)話框窗口);,dfiledialog .setMode(FileDialog.SAVE); setSize(300,300); show(); dfiledialog.show(); String FileName=dfiledialog.getFile(); String D
28、irName=dfiledialog.getDirectory(); if(FileName=null) l1.setText(你取消了文件的選擇); else l1.setText(你選擇的文件名是:+FileName); l2.setText(你選擇的文件目錄是:+DirName); ,public static void main(String args) filedialog nowFrame=new filedialog(保存文件對(duì)話框?qū)嵗?; nowFrame.addWindowListener(new WindowAdapter() public void windowClosi
29、ng(WindowEvent e) System.exit(0); ); nowFrame.pack(); nowFrame.show(); ,3.4 菜單 使用Frame的setMenuBar(MenuBar)方法 (1)菜單的組織,file,edit,help,open,save,Menu,MenuBar,MenuItem,菜單欄 MenuBar:可包含幾個(gè)菜單 add(Menu) 菜單 Menu:可包含幾個(gè)菜單項(xiàng) add(MenuItem),菜單項(xiàng)MenuItem 用Java創(chuàng)建菜單的過(guò)程如下: (1) 創(chuàng)建一個(gè)菜單欄。 (2) 調(diào)用Frame的setMenuBar()方法將菜單欄加入F
30、rame中。 (3) 分別創(chuàng)建若干個(gè)Menu對(duì)象,并加入MenuBar中。 對(duì)于每個(gè)Menu對(duì)象,分別創(chuàng)建若干個(gè)MenuItem對(duì)象,并加入Menu中。,子菜單CheckboxMenuItem 該類創(chuàng)建一個(gè)可選擇的的菜單項(xiàng),即用于選擇后,其左邊會(huì)打上對(duì)鉤。,快捷菜單PopupMenu 這類菜單是Menu的子類,特點(diǎn)是可以在指定位置動(dòng)態(tài)地彈出。,import java.awt.*; public class aa extends Frame Menu menu1, menu2, menu3; Label label1; public aa(String ss) super(ss); resize
31、(700,700); label1=new Label(now); add(label1);,MenuBar mb = new MenuBar(); menu1 = new Menu(file); menu1.add(new MenuItem(new); menu1.addSeparator(); menu1.add(new MenuItem(close); mb.add(menu1); menu2 = new Menu(edit); ,menu3 = new Menu(other); menu3.add(new MenuItem(o1); menu3.add(new MenuItem(o2)
32、; menu2.add(menu3); mb.add(menu2); setMenuBar(mb); show(); public static void main(String args) new aa(example menu); ,4 布局管理 4.1順序布局(FlowLayout) 是最基本的一種布局,缺省布局就是順序布局。順序布局指的是把圖形元件一個(gè)接一個(gè)地從左到右,從上到下地放置.,4.2 邊界布局 (BorderLayout) 邊界布局包括五個(gè)區(qū):北區(qū)、南區(qū)、東區(qū)、西區(qū)和中區(qū)。這幾個(gè)區(qū)分布規(guī)律是“上北下南,左西右東”。,North,South,West,East,Center,i
33、mport java.awt.*; import java.applet.*; public class FlowLayoutExample extends Applet Label txt=new Label(null); Button bnorth=new Button(North); Button bsouth=new Button(South); Button beast=new Button(East); Button bwest=new Button(West); public void init() this.setLayout(new FlowLayout(); /設(shè)置Flow
34、Layout布局 add(bnorth); add(bsouth);,add(beast); add(bwest); add(txt); ,import java.awt.*; import java.applet.*; public class exa3 extends Applet Label txt=new Label(null); public void init() setLayout(new BorderLayout();/設(shè)置邊界布局 setBackground(Color.red);/設(shè)置背景色 setForeground(Color.white); /設(shè)置前景色 add(No
35、rth,new Button(North); add(South,new Button(South); add(East,new Button(East); add(West,new Button(West); add(Center,txt); ,4.3 網(wǎng)格布局 (GridLayout) 網(wǎng)格布局把面板分成一個(gè)個(gè)的網(wǎng)格,你可以給出網(wǎng)格的行數(shù)和列數(shù)。 setLayout(newGridLayout(4,2);,public class GridLayoutExample extends Applet Label txt=new Label(null); Button bnorth=new Bu
36、tton(North); Button bsouth=new Button(South); Button beast=new Button(East); Button bwest=new Button(West); public void init() this.setLayout(new GridLayout(2,3);/設(shè)置布局為2*3的GridLayout,add(bnorth); add(bsouth); add(beast); add(bwest); add(txt);/按默認(rèn)順序依次加入組件 ,4.4 卡片式布局管理器(CardLayout) 提供了一種基于卡片式布局,象撲克牌一樣
37、,每次只能顯示最上面的一張. CardLayout cardlayout1=new CardLayout(); .setLayout(CardLayout ); add(“one”,container);/加入第一張卡片 add(“two”, container); /加入第二張卡片 add(“three”, container); /加入第三張卡片 first(container);/顯示第一張卡片 next(container);/顯示下一張卡片 previous(container);/顯示上一張卡片,import java.applet.*; import java.awt.*; pu
38、blic class guiexample3 extends Applet public void init() this.setLayout(null); Label l1=new Label(用戶名:); l1.setBounds(new Rectangle(45,38,67,34); add(l1);,4.5 null布局管理器 由用戶來(lái)管理組件的布局,即用setBounds(new Rectangle()方法手動(dòng)地設(shè)置組件位置.,TextField txt1=new TextField(8); txt1.setBounds(new Rectangle(115,39,210,33); a
39、dd(txt1); Label l2=new Label(密碼 :); l2.setBounds(new Rectangle(43,86,66,26); add(l2); TextField txt2=new TextField(8); txt2.setEchoChar(*);,txt2.setBounds(new Rectangle(115,84,210,33); add(txt2); Button b1=new Button(登錄); b1.setBounds(new Rectangle(78,150,86,30); add(b1); Button b2=new Button(取消); b
40、2.setBounds(new Rectangle(193,150,86,30); add(b2); ,5Java的圖形設(shè)計(jì) 5.1 圖形的坐標(biāo)系統(tǒng) (1)Graphics對(duì)象 Java語(yǔ)言的類庫(kù)中提供了豐富的繪圖方法,其中大部分對(duì)圖形、文本、圖像的操作方法都定義在Graphics類中。Graphics類又是java.awt程序包的一部分,進(jìn)行圖形、文本、圖像的處理時(shí),要在Java源文件的頭部先寫上: import java.awt.Graphics; (2)paint()方法 paint( )方法中得到了一個(gè)Graphics對(duì)象的引用,這是系統(tǒng)直接將生成好的Graphics對(duì)象通過(guò)參數(shù)形式傳
41、遞給paint( )方法。因此,我們只要在這個(gè)對(duì)象上進(jìn)行圖形、文本及圖像的繪制操作,就可以在屏幕上看到所顯示的結(jié)果。,(3)坐標(biāo) Java的坐標(biāo)原點(diǎn)(0,0)在屏幕的左上角,水平向右為X軸的正方向,豎直向下為Y軸的正方向,每個(gè)坐標(biāo)點(diǎn)的值表示屏幕上的一個(gè)象素點(diǎn)的位置,因此,所有坐標(biāo)點(diǎn)的值都取整數(shù)。,X,Y,(0,0),5.2 顏色 Color (1)常用顏色 (2)創(chuàng)建顏色 Java中每一種顏色都看成是由紅(R)、綠(G)、藍(lán)(B)三原色組合而成的。因此Color類的構(gòu)造方法采用如下格式: Color(int r, int g, int b),(3)設(shè)置當(dāng)前顏色setColor(Color c)
42、 (4)其他方法 (5)設(shè)置applet背景和前景 setBackground(Color c) setForeground(Color c) 它們都被定義在java.awt.Component類中,因此該方法能被其子類(包括Applet類及Applet類的子類)自動(dòng)繼承,import java.awt.Graphics; import java.awt.Color; public class Colors extends java.applet.Applet public void paint(Graphics g) int red,green,blue; for (int i=10;i40
43、0;i+=40) red=(int)Math.floor(Math.random()*256); green=(int)Math.floor(Math.random()*256); blue=(int)Math.floor(Math.random()*256); g.setColor(new Color(red,green,blue); g.fillRect(i,20,30,30); ,5.2 字型Font (1)創(chuàng)建 Font(String name, int style, int size) name:字體名TimesRoman ,”Courier” Style:字體風(fēng)格 Font.BOL
44、D(表示粗體)、Font.ITALIC(表示 斜體)、Font.PLAIN(表示普通體)可以進(jìn)行相加運(yùn)算來(lái)生成復(fù)合style size:尺寸大小 Toolkit systk = Toolkit.getDefaultToolkit( ); String fonts = systk.getFontList( ); 返回系統(tǒng)目前可用的字體列表,然后就可決定到底選用哪種字體。,(2)設(shè)置字型 setFont( Font)方法 (3)顯示drawString(String str, int x, int y) str即是要顯示的字符串,x,y指明字符串顯示的起始位置坐標(biāo) (4)其他方法,import j
45、ava.awt.Graphics; import java.awt.Font; public class Fonts extends java.applet.Applet public void paint(Graphics g) Font ftp20 = new Font(TimesRoman,Font.PLAIN,20); Font fai15 = new Font(Arial,Font.ITALIC,15); Font fcb24 = new Font(Courier,Font.BOLD,24); Font fsib30 = new Font(宋體,Font.ITALIC+Font.BO
46、LD,30);,g.setFont(ftp20); g.drawString(Font name TimesRoman , style plain , size 20,10,20); g.setFont(fai15); g.drawString(Font name Arial , style italic , size 15,10,50); g.setFont(fcb24); g.drawString(Font name Courier , style bold , size 24,10,80); g.setFont(fsib30); g.drawString(字體名 宋體,風(fēng)格 斜體粗體,尺
47、寸 30,10,120); ,5.3 繪制圖形 (1)畫線 drawLine(int x1,int y1,int x2,int y2) x1,y1表示線段的一個(gè)坐標(biāo)點(diǎn),x2,y2表示線段的另一個(gè)坐標(biāo)點(diǎn),(2)矩形 1.普通矩形 drawRect(int x, int y, int width, int height) /邊框型風(fēng)格 fillRect(int x, int y, int width, int height) /填充型風(fēng)格 其中頭兩個(gè)參數(shù)分別表示矩形左上角的x坐標(biāo)和y坐標(biāo),后兩個(gè)參數(shù)分別表示矩形的寬度和高度。 2圓角矩形 圓角矩形,也就是矩形的四個(gè)頂角呈圓弧狀,每個(gè)圓弧其實(shí)是由四分
48、之一的橢圓弧所構(gòu)成。畫圓角矩形的兩個(gè)方法的調(diào)用格式如下: drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight),它們除了具有和普通矩形含義相同的前四個(gè)參數(shù)外,還多了兩個(gè)用來(lái)描述圓角性質(zhì)的參數(shù)。其中arcWidth代表了圓角弧的橫向直徑;arcHeight代表了圓角弧的縱向直徑。 3立體矩形 (三維矩形 ) draw3DRect(in
49、t x, int y, int width, int height, boolean raised) fill3DRect(int x, int y, int width, int height, boolean raised) raised便是定義該立體矩形是具有凸出(值為true)還是凹下(值為false)的效果,(3)多邊形 多邊形的畫法通常是給出一組坐標(biāo)點(diǎn),再用直線段將這些點(diǎn)依次連接起來(lái)。,drawPolygon(int xPoints,int yPoints,int nPoints) fillPolygon(int xPoints,int yPoints,int nPoints) 其
50、中xPoints參數(shù)是一個(gè)整數(shù)數(shù)組,用以存放多邊形坐標(biāo)點(diǎn)的X坐標(biāo)值,yPoints參數(shù)存放相應(yīng)的一組Y坐標(biāo)值,nPoints則表示共有幾個(gè)坐標(biāo)點(diǎn)。 drawPolygon(Polygon p) fillPolygon(Polygon p) Polygon(int xPoints,int yPoints,int nPoints) Polygon( ) Polygon類中提供了一系列特有的方法,可以方便的進(jìn)行與多邊形相關(guān)的操作,象其中的addPoint( )方法可將多邊形的坐標(biāo)點(diǎn)動(dòng)態(tài)地增加到Polygon對(duì)象中。,(4)橢圓 drawOval(int x, int y, int width, in
51、t height) /邊框型風(fēng)格 fillOval(int x, int y, int width, int height) /填充型風(fēng)格 x和y不是橢圓的圓心坐標(biāo),而是該橢圓外接矩形的左上角。,(5)畫弧 弧是橢圓的一部分,因而畫弧的方法就相當(dāng)于先畫一個(gè)橢圓,而后取該橢圓中所需要的一部分。它們的調(diào)用格式如下: drawArc(int x, int y, int width, int height,int startAngle, int arcAngle) /邊框型風(fēng)格 fillArc(int x, int y, int width, int height,int startAngle, in
52、t arcAngle) /填充型風(fēng)格,import java.applet.*; public class no72 extends java.applet.Applet public void paint(Graphics g) g.setColor(Color.blue); g.drawLine(30,30,70,70); g.drawLine(60,50,60,50); g.setColor(Color.red); g.drawRect(80,40,60,40); g.fillRect(160,20,60,40); g.setColor(Color.green); g.drawRound
53、Rect(20,100,80,120,20,20);,g.fillRoundRect(120,100,80,140,40,30); g.drawRoundRect(220,100,80,140,60,40); g.setColor(Color.yellow); g.drawOval(330,20,60,60); g.fillOval(430,20,80,60); g.setColor(Color.black); g.drawArc(10,320,100,60,35,65); g.drawArc(110,320,100,60,35,-140); g.fillArc(210,320,100,60,
54、35,65); g.fillArc(310,320,100,60,35,-140); ,import java.awt.*; import java.applet.Applet; public class no73 extends java.applet.Applet public void paint(Graphics g)Poly1_x=30,63,115,72,67; /int Poly1_y=40,20,95,74,106; /int Poly1_pts=Poly1_x.length; /int Poly2_x=180,213,265,222,217; /int Poly2_y=40,
55、20,95,74,106; /int Poly2_pts=Poly2_x.length; /g.drawPolygon(Poly1_x,Poly1_y, Poly1_pts); /g.fillPolygon(Poly2_x,Poly2_y, Poly2_pts);,int Poly1_x=30,63,115,72,67; int Poly1_y=40,20,95,74,106; int Poly1_pts=Poly1_x.length; Polygon poly1= new Polygon(Poly1_x,Poly1_y, Poly1_pts); Polygon poly2= new Poly
56、gon(); poly2.addPoint(180,40); poly2.addPoint(213,20); poly2.addPoint(265,95); poly2.addPoint(222,74); poly2.addPoint(217,106); g.drawPolygon(poly1); g.fillPolygon(poly2); ,(6)復(fù)制與清除圖形 copyArea(int x, int y, int width, int height, int dx, int dy) 前四個(gè)參數(shù)我們應(yīng)該是相當(dāng)熟悉了,它定義了要被復(fù)制的屏幕的矩形區(qū)域。最后兩個(gè)參數(shù)則表示新區(qū)域與原始屏幕區(qū)域的偏
57、移距離:若dx,dy為正值,則表示新區(qū)域相對(duì)于原區(qū)域的右方及下方所偏移的像素值;反之,它們?nèi)∝?fù)值則分別表示相對(duì)左方及上方的偏移量。 若要清除屏幕的某一矩形區(qū)域所畫的內(nèi)容,就要選用clearRect( )方法,它用當(dāng)前的背景顏色來(lái)填充整個(gè)矩形區(qū)域。其調(diào)用格式為: clearRect(int x, int y, int width, int height) 可以看出這四個(gè)參數(shù)定義了所要清除的矩形區(qū)域。,import java.awt.*; import java.applet.Applet; public class Copyarea extends java.applet.Applet publ
58、ic void paint(Graphics g) g.drawOval(80,80,59,59); g.copyArea(80,80,60,60,80,0); g.copyArea(80,80,60,60,-80,0); g.copyArea(80,80,60,60,80,0); g.copyArea(80,80,60,60,0,80); g.copyArea(80,80,60,60,0,-80); ,Java 2D java.awt:java.awt包含了一些新增的2D API 類別和接口。其中 Graphics2D繼承自 java.awt.Graphics ,是描繪2D圖形的物件(object)。在Graphics2D中新增了許多狀態(tài)屬性,像是Stroke、 Paint、Clip、 Transform等。 java.awt.geom:包含可以勾勒任何形狀的 GeneralPath類別。它可以由許多不同種類
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年游戲策劃師崗位面試題集與答案參考
- 裝配式鋼結(jié)構(gòu)生產(chǎn)線項(xiàng)目商業(yè)計(jì)劃書
- 幕墻鋼結(jié)構(gòu)施工信息共享平臺(tái)方案
- 鋼結(jié)構(gòu)幕墻連接件選型方案
- 2026年游戲公司策劃工程師面試知識(shí)及題庫(kù)
- 2026年通信設(shè)備維修技師面試要點(diǎn)及答案解析
- 餐飲業(yè)服務(wù)規(guī)范與服務(wù)質(zhì)量提升手冊(cè)
- 企業(yè)危機(jī)管理與應(yīng)對(duì)策略(標(biāo)準(zhǔn)版)
- 2025年客戶服務(wù)標(biāo)準(zhǔn)與服務(wù)流程手冊(cè)
- 2025年醫(yī)療廢物處理技術(shù)指南
- 無(wú)糾紛自愿離婚協(xié)議書
- 四川省高等教育自學(xué)考試畢業(yè)生登記表【模板】
- vpap iv st說(shuō)明總體操作界面
- 2023人事年度工作計(jì)劃七篇
- LY/T 1692-2007轉(zhuǎn)基因森林植物及其產(chǎn)品安全性評(píng)價(jià)技術(shù)規(guī)程
- GB/T 20145-2006燈和燈系統(tǒng)的光生物安全性
- 長(zhǎng)興中學(xué)提前招生試卷
- 安全事故案例-圖片課件
- 螺紋的基礎(chǔ)知識(shí)
- 蜂窩煤成型機(jī)課程設(shè)計(jì)說(shuō)明書
- 生物統(tǒng)計(jì)學(xué)(課堂PPT)
評(píng)論
0/150
提交評(píng)論