1. Trang chủ
  2. » Luận Văn - Báo Cáo

LẬP TRÌNH ĐỒ HỌA VỚI AWT

136 627 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Lập trình đồ họa với AWT
Tác giả Cao Duc Thong
Trường học Thanglong University
Thể loại bài luận
Định dạng
Số trang 136
Dung lượng 747,45 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

LẬP TRÌNH ĐỒ HỌA VỚI AWT

Trang 1

L p p trình h h a a v i i AWT

CAO Duc Thong – Thanglong University

thongcd@thanglong.edu.vn

Trang 2

2

Trang 3

Trình qu n lý trình bày (Layout Manager)

a (Graphics), phông ch (Font), ki n (Event)

Trang 4

4

Th vi nAWT T

Image Insets Polygon Rectangle Toolkit MenuComponent

XXXLayout CheckboxGroup Component

MenuBar MenuItem

Menu Checkbox MenuItem

Trang 5

Applet Checkbox

Choice Container Label List Scrollbar TextComponent

Panel Window ScrollPane

TextArea TextField

Frame Dialog

FileDialog

Trang 6

Ví Component: button, checkbox, scrollbar…

t ph ng th c n a Component

getBounds(): tr ph m vi a Component (Rectangle)

getFont(): tr font hi n i a Component

getForeGround(): tr màu a Component

getHeight(): tr chi u cao a Component (pixel, ki u int)

Trang 7

getSize(): tr kích th c a Component (Dimenstion)

getWidth(): tr chi u ng a Component (int)

getX(), getY(): tr a hi n i

isEnable(): boolean

paint(Graphics): ch u trách nhi m hi n th component

setVisible(boolean): hi n th component

Trang 9

Frame a là Component a là Container

o Frame Frame() Frame(String title)

Trang 10

UseLessFrame frame = new UseLessFrame();

} }

Trang 13

t ngu n sinh ra ki n có th có nhi u p nghe

Trang 14

class EventTest extends Frame implements ActionListener {

Label lab = new Label("Enter a number");

TextField tf1 = new TextField(5);

TextField tf2 = new TextField(5);

Button btnResult = new Button("Double is");

Button ext = new Button("exit");

public EventTest(String title) {

Trang 15

public static void main(String args[]) {

EventTest t = new EventTest("Event handling");

t.setSize(300, 200);

t.setVisible(true);

} }

Trang 17

java.awt.AWTEvent

AdjustmentEvent ComponentEvent ItemEvent

FocusEvent InputEvent ContainerEvent

KeyEvent MouseEvent

TextEvent WindowEvent

Trang 19

v ContainerListenere

n FocusListenert

L

i ItemListeners

e

n MouseListenere

r MouseMotionListener

TextListener WindowListener

Trang 20

addActionListener(A ctionListener)

addItemListener(Ite mListener)

Trang 22

b.addActionListener(new ButtonHandler());

f.add(b, BorderLayout.CENTER);

f.pack();

f.setVisible(true);

Trang 23

X lý ý cács ki n n

Trang 24

public static void main(String[] args){

TestButton test = new TestButton();

test.init();

} }

class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

System.out.println("Button's command is: "+

e.getActionCommand());

} }

Trang 25

Button's command is: ButtonPressed

Button's command is: ButtonPressed

Button's command is: ButtonPressed

Trang 26

25

Trang 27

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

public void windowOpened(WindowEvent e){}

}

Trang 29

public class GUIFrame extends Frame {

public GUIFrame(String title){

}

Trang 30

setLocation((d.width - getWidth())/2, (d.height

-getHeight())/2);

} super.setVisible(visible);

}

public static void main(String[] args){

GUIFrame frame = new GUIFrame("GUI Frame");

frame.setSize(400,300);

frame.setVisible(true);

} }

Trang 32

tr Label.LEFT, Label.RIGHT, Label.CENTER

Trang 33

Label l1 = new Label("Label");

Label l2 = new Label("I am a label");

l2.setFont(new Font("Timesroman", Font.BOLD, 18));

Label l3 = new Label();

Trang 34

public static void main(String[] args) {

LabelTest lt = new LabelTest();

}

}

Trang 35

getSelectedText() getSelectionStart() getSelectionEnd() getText(), setText() select(int, int)

setCaretPosition(int) setEditable(boolean) setSelectionStart(int)

Trang 36

TextField(int columns) TextField(String s)

TextField(String s, int columns) addActionListener(ActionListener) echoCharIsSet()

setEchoChar(char)

setText()

setColumn(int)

Trang 37

isEditable(): xác nh xem có ch Editable không

Trang 38

TextField tf1 = new TextField();

TextField tf2 = new TextField(25);

tf2.setText("Type stuff here");

tf2.setFont(new Font("Timesroman",Font.BOLD,18)); TextField tf3 = new TextField("I am disabled",15);

Trang 39

Cácthànhph nAWT T

Trang 40

TextField tf7 = new TextField("Caret here >< ");

TextField tf8 = new TextField("username",8);

TextField tf9 = new TextField("password",8);

tf9.setEchoChar('*');

ComponentTestFrame frame = new

ComponentTestFrame("TextField Test");

frame.add(tf1); frame.add(tf2); frame.add(tf3);

frame.add(tf4); frame.add(tf5); frame.add(tf6);

frame.add(tf7); frame.add(tf8); frame.add(tf9);

frame.setVisible(true);

tf7.setCaretPosition(14);

} public static void main(String[] args) {

TextFieldTest test = new TextFieldTest();

} }

Trang 42

TextArea(int rows, int columns) TextArea(String text)

TextArea(String text, int rows, int columns)

Trang 43

get/set row/column setEditable/isEditable append(String)

insert(String s, int i): chèn chu i vào t trí replaceRange(String, int, int): thay th n n m gi a tríint và int cho tr c

Trang 44

TextArea ta1 = new TextArea(10,20);

TextArea ta2 = new TextArea("Text Area\n with

TextArea ta4 = new TextArea("This textarea is not enable",

4,25,TextArea.SCROLLBARS_NONE);

Trang 45

public static void main(String[] args) {

TextAreaTest test = new TextAreaTest();

} }

Trang 47

Button(String text) addActionListener(ActionListener) String getLabel()

setLabel(String) removeActionListener(ActionListener)

Trang 48

Button b1 = new Button("Button");

Button b2 = new Button();

b2.setLabel("Press me!");

b2.setFont(new Font("Timesroman", Font.BOLD, 16));

Button b3 = new Button("Can't press me");

Trang 49

public static void main(String[] args) {

ButtonTest lt = new ButtonTest();

}

}

Trang 50

Có th ch n nhi u option a Checkbox RadioButton ng gi ng nh Checkbox nh ng ch cho phép ch n

o RadioButton)

t ph ng th c Checkbox()

Checkbox(String) Checkbox(String, boolean)

Trang 51

set/getLabel() set/getState() removeItemListener(ItemListener)

Trang 52

Checkbox cb1 = new Checkbox("Java",false);

Checkbox cb2 = new Checkbox("C++",false);

cb2.setEnabled(false);

Checkbox cb3 = new Checkbox("HTML",true);

Checkbox cb4 = new Checkbox();

Trang 53

public static void main(String[] args) {

CheckboxTest test = new CheckboxTest();

} }

Trang 54

CheckboxGroup group = new CheckboxGroup();

Checkbox cb1 = new Checkbox("Java",false,group);

Checkbox cb2 = new Checkbox("C++",false,group);

cb2.setEnabled(false);

Checkbox cb3 = new Checkbox("HTML",true,group);

Checkbox cb4 = new Checkbox("",true,group);

Trang 55

CheckboxGroupTest test = new CheckboxGroupTest();

} }

Trang 56

add(String) addItem(String) addItemListener(ItemListener) getItem(int)

getItemCount() getSelectedIndex() insert(String, int)

Trang 57

select(int) select(String)

Trang 59

ChoiceTest test = new ChoiceTest();

} }

Trang 62

public class CanvasTest extends Canvas {

public void paint(Graphics g){

setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 16));

g.drawString("Canvas", 15, 25);

}

Trang 63

public static void main(String[] args) {

CanvasTest c1 = new CanvasTest();

Trang 65

o Menu Menu fileMenu = new Menu(“File”);

n Menu vào MenuBar menuBar.add(fileMenu);

o MenuItem MenuItem fileOpen = new MenuItem(“Open”);

Trang 67

isEnabled() setEnabled()

Trang 68

MenuItem fileOpen = new MenuItem("Open");

fileOpen.setShortcut(new MenuShortcut(KeyEvent.VK_O));

MenuItem fileSave = new MenuItem("Save");

fileSave.setShortcut(new MenuShortcut(KeyEvent.VK_S));

fileSave.setEnabled(false);

MenuItem fileSaveAs = new MenuItem("Save As");

Trang 69

fileSaveAs.setEnabled(false);

MenuItem fileExit = new MenuItem("Exit");

fileExit.setShortcut(new MenuShortcut(KeyEvent.VK_X));

MenuItem editCut = new MenuItem("Cut");

editCut.setShortcut(new MenuShortcut(KeyEvent.VK_X));

MenuItem editCopy = new MenuItem("Copy");

editCopy.setShortcut(new MenuShortcut(KeyEvent.VK_C));

MenuItem editPaste = new MenuItem("Paste");

editPaste.setShortcut(new MenuShortcut(KeyEvent.VK_P));

Menu file = new Menu("File");

Trang 70

public static void main(String[] args) {

MenuTest test = new MenuTest();

}

Trang 71

Cácthànhph nAWT T

Trang 73

MenuItem paste = new MenuItem("Paste");

Trang 74

MenuItem delete = new MenuItem("Delete");

PopupMenu popupMenu = new PopupMenu("Clipboard");

PopupMenuTest test = new PopupMenuTest();

} }

Trang 76

Dùng Panel nhóm các thành ph n GUI i i nhau

import java.awt.*;

public class PanelTest {

public PanelTest() {

super();

Label lb1 = new Label("URL: ");

TextField tf1 = new TextField("",20);

Button b1 = new Button("Go");

Panel p1 = new Panel();

p1.setBackground(Color.CYAN);

p1.add(lb1); p1.add(tf1); p1.add(b1);

CheckboxGroup group = new CheckboxGroup();

Trang 77

Cácthànhph nAWT T

Panel

Checkbox cb1 = new Checkbox("Java",group,true);

Checkbox cb2 = new Checkbox("C++",group,false);

Checkbox cb3 = new Checkbox("HTML",group,false); Checkbox cb4 = new Checkbox("ADA",group,false); Panel p2 = new Panel();

p2.setBackground(Color.BLUE);

p2.add(cb1); p2.add(cb2); p2.add(cb3); p2.add(cb4);

ComponentTestFrame frame = new

ComponentTestFrame("PanelTest");

frame.add(p1); frame.add(p2);

frame.setVisible(true);

} public static void main(String[] args) {

PanelTest test = new PanelTest();

} }

Trang 78

Cácthànhph nAWT T

74

Trang 80

t Dialog có th là modal ho c non-modal Modal dialog

Non-modal dialog

Trang 81

Dialog(Frame) Dialog(Frame, String)

Trang 82

Dialog d1 = new Dialog(frame,"Modal Dialog Test",true);

d1.add(new Label("Modal Dialog Test"));

Trang 83

public void windowClosing(WindowEvent arg0) {

((Dialog)arg0.getSource()).setVisible(false);

} public void windowClosed(WindowEvent arg0) { }

Trang 84

public static void main(String[] args) {

DialogTest test = new DialogTest();

} }

Trang 86

82

Qu nlý ý trình h bày y

Layout manager: qu n lý cách trình bày a các GUI

components trong t Container

Trang 87

Các hàm kh i o

FlowLayout() FlowLayout(int alignment)Alignment có th là FlowLayout.CENTER, LEFT, RIGHT FlowLayout(int, int, int)

Trang 88

public class FlowLayoutTest extends Frame implements WindowListener {

public FlowLayoutTest(String title){

dispose();

System.exit(0);

}

Trang 89

public void windowActivated(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

public void windowOpened(WindowEvent e){}

public static void main(String[] args) { FlowLayoutTest ft = new FlowLayoutTest("FlowLayout Test");

} }

Trang 91

Là layout c nh cho Frame, Window, Dialog

Các thành ph n c p p trên 5 khu c khác nhau

NORTH – t nh a container.

EAST – t phía bên ph i a container.x`

SOUTH – t phía i a container

WEST – t phía bên trái a container.

CENTER – t gi a a container.

thêm t thành ph n vào vùng ‘North’

Button b1=new Button(“North Button”); // khai báo thành ph n

setLayout(new BorderLayout()); // thi t p layout

add(b1,BorderLayout.NORTH); // thêm thành ph n vào layout

Kh i o: BorderLayout(), BorderLayout(int, int)

Trang 94

Các hàm kh i o:

GridLayout(int), GridLayout(int, int), GridLayout(int, int, int,int)

Trang 95

public class GridLayoutTest extends Frame implements WindowListener {

public GridLayoutTest(String title){

Trang 96

public void windowActivated(WindowEvent e){}

public void windowClosed(WindowEvent e){}

public void windowIconified(WindowEvent e){}

public void windowDeiconified(WindowEvent e){}

public void windowDeactivated(WindowEvent e){}

public void windowOpened(WindowEvent e){}

public static void main(String[] args) {

GridLayoutTest ft = new GridLayoutTest(“GridLayout Test");

} }

Trang 99

95

WindowEvent

Cài t giao ti p WindowListener

Xem ví Frame

X lý ý cács ki n n

Trang 101

c phát sinh i Button, MenuItem, TextField, List

p nghe cài t giao ti p ActionListener hay cài t ph ng th c actionPerformed(ActionEvent)

t bi n & ph ng th c a ActionEvent int ALT_MASK: phím ALT có c nh n ? int CTRL_MASK: phím CTRL có c nh n ? int SHIFT_MASK: phím SHIFT có c nh n ? int getModifiers(): có th tr ALT_MASK, CTRL_MASK…

String getActionCommand(): tr command n i i ActionEvent

Trang 102

class ActionListenerTest extends GUIFrame implements ActionListener {

Panel controlPanel, whoDoneItPanel, commandPanel;

Label whoDoneItLabel, commandLabel;

TextField whoDoneItTextField, commandTextField, textField;

Trang 103

whoDoneItPanel.setBackground(Color.pink);

whoDoneItLabel = new Label("Who done it",

whoDoneItTextField = new TextField("A TextField"); //whoDoneItTextField.addActionListener(this);

whoDoneItTextField.setEditable(false);

whoDoneItPanel.add(whoDoneItLabel);

whoDoneItPanel.add(whoDoneItTextField);

add(whoDoneItPanel,BorderLayout.NORTH);

Trang 104

X lý ý cács ki n n

99

Trang 105

X lý ý cács ki n n

ActionEvent

//create controlPanel controlPanel = new Panel();

controlPanel.add(new Label("A TextField", Label.RIGHT)); textField = new TextField(15);

controlPanel.add(new Label("A List",Label.RIGHT));

list = new List(5,false);

list.add("Breakfast");

list.add("Lunch");

list.add("Diner");

Trang 107

commandLabel = new Label("Action Command");

Trang 109

} public static void main(String[] args){

ActionListenerTest test = new ActionListenerTest();

} }

Trang 111

p nghe ItemEvent n cài t giao ti p ItemListener

Ph ng th c n cài t: itemStateChanged(ItemEvent)

Ph ng th c a ItemEvent

int getStateChange(): có th nh n ItemEvent.SELECTED ho cItemEvent.DESELECTED

Object getItem(): item ã thay i tr ng thái (Checkbox,

Trang 112

c o ra i TextComponent (TextField, TextArea)

p nghe cài t giao ti p TextListener

Ph ng th c n cài t textValueChanged(TextEvent)

i (thêm, xóa text)

Trang 115

super("Mouse Event Test");

canvas = new Canvas();

Trang 116

new MouseTest();

} public void mouseClicked(MouseEvent e){

String text = "Event: Clicked Button ";

switch(e.getModifiers()){

case InputEvent.BUTTON1_MASK:

text += 1;

Trang 118

event.setText(text);

} public void mouseEntered(MouseEvent e){

event.setText("Event: Entered");

} public void mouseExited(MouseEvent e){

event.setText("Event: Exited");

}

Trang 119

X lý ý cács ki n n

110

Trang 120

event.setText("Event: Released");

} public void mouseMoved(MouseEvent e){

location.setText("Location: (" + e.getX()+"," + e.getY()+")");

} public void mouseDragged(MouseEvent e){

Point p = e.getPoint();

event.setText("Event: Dragged");

location.setText("Location: (" + p.getX()+"," + p.getY()+")");

}

Trang 122

void keyTyped(KeyEvent)

Trang 123

Image image =

Toolkit.getDefaultToolkit().getImage("image.jpg");

Trang 125

public static void main(String[] args) {

ImageTest image = new ImageTest();

GUIFrame frame = new GUIFrame("ImageTest");

frame.add(image);

frame.pack();

frame.setVisible(true);

} }

Trang 127

CÂU U H I I

Trang 128

th c 200, 200 màu Khi a chu t vào Canvas

ch ng trình hi n dòng ch “In canvas”, khi a chu t ra

ngoài hi n dòng ch “Not in canvas”

Vi t ch ng trình cho phép ng tròn i t trí

t mà ng i dùng nh n chu t trên màn hình

Vi t t ch ng trình i menu “Draw” có 3 menu item:

line, circle, square: khi ng i dùng kích vào ng menu

item, 50 line ho c circle ho c square c ra

Trang 130

trí a qu bóng thay i theo Yêu u o t p

Ball riêng bi t (M ng bài toán cho tr ng p ng i

Trang 131

121

ki n và i ng gây ra ki n

Tham m kh o o

Ngày đăng: 25/04/2013, 20:49

HÌNH ẢNH LIÊN QUAN

Là ti ng a có th hin th c trên màn hình và - LẬP TRÌNH ĐỒ HỌA VỚI AWT
ti ng a có th hin th c trên màn hình và (Trang 6)
t mà n gi dùng nh n ch ut trên màn hình - LẬP TRÌNH ĐỒ HỌA VỚI AWT
t mà n gi dùng nh n ch ut trên màn hình (Trang 128)
Vi tch ng trình mô trò chi dò mìn. Trên màn hình - LẬP TRÌNH ĐỒ HỌA VỚI AWT
i tch ng trình mô trò chi dò mìn. Trên màn hình (Trang 130)

TỪ KHÓA LIÊN QUAN

w