-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecimalToBinaryGUI.java
102 lines (93 loc) · 2.21 KB
/
DecimalToBinaryGUI.java
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
101
102
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DecimalToBinaryGUI extends JFrame implements ActionListener{
JButton ok, clear;
JTextField field;
JTextArea inputField;
JLabel l1, l2;
static long decimal, c, a, b;
static String result=" ";
DecimalToBinaryGUI(){
super("Decimal To Binary Converter");
setSize(701, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane=new JPanel();
ok= new JButton("Calculate");
ok.addActionListener(this);
clear= new JButton("Clear");
clear.addActionListener(this);
inputField= new JTextArea(2, 15);
field= new JTextField(15);
l1= new JLabel("Decimal:");
l2= new JLabel("Binary:");
ok.setBackground(Color.GREEN);
clear.setBackground(Color.YELLOW);
inputField.setEditable(false);
pane.add(l1);
pane.add(field);
pane.add(l2);
pane.add(inputField);
pane.add(ok);
pane.add(clear);
add (pane);
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
Object source=evt.getSource();
if (source==ok){
calculator();
}
else if (source==clear){
clear();
inputField.setText("");
}
repaint();
}
void calculator() {
try{
decimal=Long.parseLong(field.getText());
}
catch(NullPointerException|NumberFormatException e){
JOptionPane.showMessageDialog(null, "Invalid Input, Please try again", "Answer", JOptionPane.PLAIN_MESSAGE);
}
if (decimal==1){
inputField.setText("1");
}
else if(decimal==0){
inputField.setText("0");
}
for(a=0; a<=2147483647; a++){
if((decimal!=0)&&(decimal!=1)){
c=decimal%2;
decimal=decimal/2;
result= result.concat(Long.toString(c));
}
else if(decimal==1){
decimal=0;
result= result.concat("1");
}
else if(decimal<=0){
StringBuilder ans = new StringBuilder();
ans.append(result);
ans.reverse();
result=ans.toString();
inputField.setText(result);
clear();
a=2147483647;
}
}
}
void clear(){
field.setText("");
decimal=0;
c=0;
a=0;
b=0;
result=" ";
System.out.flush();
}
public static void main(String[] args){
DecimalToBinaryGUI dtbgui=new DecimalToBinaryGUI();
}
}