1
2 import java.awt.*;
3 import java.awt.event.*;
4 /*
5 The idea behind this exorcise is aimed at teaching the crude fundamentals of
6 signed binary notation with a basic insight in mind as to what happens when
7 you don't designate correct containership for number representation
8 particularly important when writing math's classes or doing any programming
9 with regard to calculations of some sort. This is possibly the most critical
10 part of programming that people tend to forget and can be very costly at the
11 end of it especially if you are writing applications for a company
12
13 Written By MD Harrington Friday, 17 June 2005 */
14
15
16 class ConvertIntegerToBin extends Frame implements KeyListener
17 {
18
19 private TextField numb1, result, TChar ;
20 private Label LB1,LB2,LB3 ;
21 private IntegerToBinary myint; // The InetegertoBinary class that
22 // deals with our conversion
23 private Panel p1,p2,p3; // panels for for adding components to
24
25 public ConvertIntegerToBin()
26 {
27
28 super("Mark's 32 bit Signed Binary Converter");
29 this.addWindowListener(new WindowAdapter(){
30 // add windowlistener so we can close the window
31 public void windowClosing(WindowEvent e)
32 {close();}
33 });
34
35 // use grid Layout 4 rows 1 column vertical and horizontal spacing of 10
36 this.setLayout(new GridLayout(4,1,10,10));
37 numb1 = new TextField(20);
38 TChar = new TextField(3);
39 TChar.setForeground(new Color(255,0,0));
40 numb1.addKeyListener(this); // key listener listens for the enter key
41 result = new TextField(32);
42 // create 3 labels to indicate what we are doing here
43 LB1 = new Label("Enter an Integer");
44 LB2 = new Label("32 Bit Signed Representation");
45 LB3 = new Label("Character Representation");
46
47 // create 3 panels to hold our components such as labels text boxes etc
48 p1 = new Panel();
49 p2 = new Panel();
50 p3 = new Panel();
51
52 // set the layout for each panel such that we can space the Components neatly
53
54 p3.setLayout(new BorderLayout());
55 p1.setLayout(new BorderLayout());
56 p1.add("West", LB1);
57 p1.add("Center",numb1);
58 p2.add("Center",LB2);
59 p3.add("West",LB3);
60 p3.add("Center",TChar);
61
62 add("Center",p1);
63 add("Center",p3);
64 add("Center",p2);
65 add("Center",result);
66 // initialise the size of the window to width 300 height 145
67 this.setSize(300,145);
68 // don't want to be able to maximise Frame so set re sizeable to false
69 this.setResizable(false);
70
71 // you might like to play with this to get best startup position on the screen
72
73 this.setLocation(200,200);
74
75 this.show();
76
77 }
78
79
80 /* close method which is invoked when calling destroy
81 window method handled by WindowEvent Listener */
82
83 public void close()
84 {
85 hide();
86 dispose();
87 System.exit(1);
88 }
89
90 /* key event Listener handles all key input to text fields */
91
92 public void keyTyped(KeyEvent e)
93 {
94
95 if(e.getKeyChar() == KeyEvent.VK_ENTER)
96 {
97 myint = new IntegerToBinary(Integer.parseInt(numb1.getText()));
98 result.setText(myint.getBits());
99 char myChar = (char)Integer.parseInt(numb1.getText());
100 TChar.setText(myChar+"");
101 }
102
103 }// end keyevent
104
105
106 // not used
107
108 //We have to declare these methods since we are implementing an abstract class
109
110 public void keyPressed(KeyEvent e){}
111 public void keyReleased(KeyEvent e){}
112
113
114
115
116 public static void main(String[] args)
117 {
118 ConvertIntegerToBin object = new ConvertIntegerToBin();
119 }
120 }
|