Keyboard Input: TypeWriterHere's a simple applet that uses the keyDown method to let you type some text.import java.applet.Applet; import java.awt.Event; import java.awt.Graphics; public class typewriter extends Applet { int numcols = 80; int numrows = 25; int row = 0; int col = 0; char page[][] = new char[numrows][]; public void init() { for (int i = 0; i < numrows; i++) { page[i] = new char[numcols]; } for (int i = 0; i < numrows; i++) { for (int j = 0; j < numcols; j++) { page[i][j] = '\0'; } } } public boolean keyDown(Event e, int key) { char c = (char) key; switch (key) { case Event.HOME: row = 0; col = 0; break; case Event.END: row = numrows-1; col = numcols-1; break; case Event.UP: if (row 0) row--; break; case Event.DOWN: if (row < numrows-1) row++; break; case Event.LEFT: if (col 0) col--; else if (col == 0 && row 0) { row--; col=numcols-1; } break; case Event.RIGHT: if (col < numcols-1) col++; else if (col == numcols-1 && row < numrows-1) { row++; col=0; } break; : if (c == '\n' || c == '\r') { row++; col = 0; } else if (row < numrows) { if (col = numcols) { col = 0; row++; } page[row][col] = c; col++; } else { // row = numrows col++; } } repaint(); return true; } public void paint(Graphics g) { for (int i=0; i < numrows; i++) { String tempString = new String(page[i]); g.drawString(tempString, 5, 15*(i+1)); } } }
|
Bhopal news
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100