Lecture Programming languages (2/e): Chapter 16b - Tucker, Noonan

In this section, we illustrate three problems that utilize event-driven programming. The first is the design of a simple GUI interface. The second is a Java applet, and the third involves the design of an interactive game using the Model-View-Controller (MVC) design pattern. | Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries the bitterest is this, to know so much and to have control over nothing. Herodotus (484-432 BC) Contents Event-Driven Control Event Handling Three Examples Other Applications Designing a Java Applet Can convert previous application An applet runs inside a web browser Differences Extend JApplet Lacks a main method Method init replaces constructor import .*; import .*; import .*; public class SkelApplet extends JApplet { // Global "state" of the interaction private int lastX = 0; private int lastY = 0; . public void init( ) { JPanel panel = new JPanel( ); . // Create a button and add it to the Panel. JButton clearButton = new JButton("Clear"); (); (); (clearButton); ( new ClearButtonHandler()); . getContentPane( ).add(panel); } Tic-Tac-Toe State of the board Whose turn: X or O Whether game is over private int player = -1; // Current player: EX, or OH private int[ ] board = new int[9]; public abstract class CellIcon implements Icon { private Color color = ; private int size; public CellIcon(int size) { = size; } public int size( ) { return size; } public Color getColor( ) { return color; } public void setColor(Color c) { color = c; } public int getIconWidth( ) { return size; } public int getIconHeight( ) { return size; } } private int[ ][ ] lines = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //across {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //down {0, 4, 8}, {2, 4, 6} //diagonal }; public boolean won( ) { for (int i = 0; i < ; i++) if (board[lines[i][0]] == player && board[lines[i][1]] == player && board[lines[i][2]] == player) return true; return false; } // won public class CellXIcon extends CellIcon { public CellXIcon(int size) { super(size); } public void . | Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries the bitterest is this, to know so much and to have control over nothing. Herodotus (484-432 BC) Contents Event-Driven Control Event Handling Three Examples Other Applications Designing a Java Applet Can convert previous application An applet runs inside a web browser Differences Extend JApplet Lacks a main method Method init replaces constructor import .*; import .*; import .*; public class SkelApplet extends JApplet { // Global "state" of the interaction private int lastX = 0; private int lastY = 0; . public void init( ) { JPanel panel = new JPanel( ); . // Create a button and add it to the Panel. JButton clearButton = new JButton("Clear"); (); (); (clearButton); ( new ClearButtonHandler()); . .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.