import java.*;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lesson9_Menus extends JFrame implements ActionListener{
private Container win;
JFrame f;
Container subWin;
JLabel inputLabel, outputLabel;
JTextField inputField, outputField;
JButton calcButton, bd_calcButton, bh_calcButton, db_calcButton, dh_calcButton, hb_calcButton, hd_calcButton;
private String binIn, hexIn;
private int decimal, number;
JMenuBar menubar;
JMenu binaryToMenu, decimalToMenu, hexToMenu;
JMenuItem binToDecItem, binToHexItem, decToBinItem, decToHexItem, hexToDecItem, hexToBinItem;
public Lesson9_Menus(){
super("Conversion from Decimal to Binary / Hexadecimal");
win = getContentPane();
win.setLayout(null);
menubar= new JMenuBar();
binaryToMenu = new JMenu("Binary To");
decimalToMenu = new JMenu("Decimal To");
hexToMenu = new JMenu("Hex To");
binaryToMenu.setMnemonic('B');
decimalToMenu.setMnemonic('B');
hexToMenu.setMnemonic('B');
binToDecItem = new JMenuItem("Binary to Decimal");
binToHexItem = new JMenuItem("Binary to Hex");
decToBinItem = new JMenuItem("Decimal to Binary");
decToHexItem = new JMenuItem("Decimal to Hex");
hexToDecItem = new JMenuItem("Hex to Decimal");
hexToBinItem = new JMenuItem("Hex to Binary");
binToDecItem.addActionListener(this);
binToHexItem.addActionListener(this);
decToBinItem.addActionListener(this);
decToHexItem.addActionListener(this);
hexToBinItem.addActionListener(this);
hexToDecItem.addActionListener(this);
binaryToMenu.add(binToDecItem);
binaryToMenu.add(binToHexItem);
decimalToMenu.add(decToBinItem);
decimalToMenu.add(decToHexItem);
hexToMenu.add(hexToBinItem);
hexToMenu.add(hexToDecItem);
menubar.add(binaryToMenu);
menubar.add(decimalToMenu);
menubar.add(hexToMenu);
setJMenuBar(menubar);
number = 0;
setSize(600,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] arg){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Lesson9_Menus();
}
});
}
public void actionPerfomed(ActionEvent event){
if(event.getSource() == binToDecItem)
react_binToDecItem();
else if(event.getSource() == binToHexItem)
react_binToHexItem();
else if(event.getSource() == decToBinItem)
react_decToBinItem();
else if(event.getSource() == decToHexItem)
react_decToHexItem();
else if(event.getSource() == hexToBinItem)
react_hexToBinItem();
else if(event.getSource() == hexToDecItem)
react_hexToDecItem();
}
public void react_binToDecItem(){
JFrame f = new JFrame("Binary to decimal");
subWin = f.getContentPane();
subWin.setLayout(null);
subWin.setPreferredSize(new Dimension(400,200));
inputLabel = new JLabel("Enter binary number");
inputLabel.setSize(new Dimension(175,20));
inputLabel.setLocation(10,50);
subWin.add(inputLabel);
inputField = new JTextField("");
inputField.setSize(175,20);
inputField.setLocation(200,50);
subWin.add(inputField);
outputLabel = new JLabel("Decimal Number");
outputLabel.setSize(new Dimension(175,20));
outputLabel.setLocation(10,150);
subWin.add(outputLabel);
outputField = new JTextField("");
outputField.setSize(new Dimension(175,20));
outputField.setLocation(200,150);
subWin.add(outputField);
bd_calcButton = new JButton("Calculate");
bd_calcButton.setSize(150,30);
bd_calcButton.setLocation(100,80);
subWin.add(bd_calcButton);
bd_calcButton.addActionListener(new ButtonHandler());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public void react_binToHexItem(){
JFrame f = new JFrame("Binary to hexadecimal");
subWin = f.getContentPane();
subWin.setLayout(null);
subWin.setPreferredSize(new Dimension(400,200));
inputLabel = new JLabel("Enter binary number");
inputLabel.setSize(new Dimension(175,20));
inputLabel.setLocation(10,50);
subWin.add(inputLabel);
inputField = new JTextField("");
inputField.setSize(175,20);
inputField.setLocation(200,50);
subWin.add(inputField);
outputLabel = new JLabel("Hexadecimal Number");
outputLabel.setSize(new Dimension(175,20));
outputLabel.setLocation(10,150);
subWin.add(outputLabel);
outputField = new JTextField("");
outputField.setSize(new Dimension(175,20));
outputField.setLocation(200,150);
subWin.add(outputField);
bh_calcButton = new JButton("Calculate");
bh_calcButton.setSize(150,30);
bh_calcButton.setLocation(100,80);
subWin.add(bh_calcButton);
bh_calcButton.addActionListener(new ButtonHandler());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public void react_hexToDecItem(){
JFrame f = new JFrame("Hex to Decimal");
subWin = f.getContentPane();
subWin.setLayout(null);
subWin.setPreferredSize(new Dimension(400,200));
inputLabel = new JLabel("Enter hexadecimal number");
inputLabel.setSize(new Dimension(175,20));
inputLabel.setLocation(10,50);
subWin.add(inputLabel);
inputField = new JTextField("");
inputField.setSize(175,20);
inputField.setLocation(200,50);
subWin.add(inputField);
outputLabel = new JLabel("Decimal Number");
outputLabel.setSize(new Dimension(175,20));
outputLabel.setLocation(10,150);
subWin.add(outputLabel);
outputField = new JTextField("");
outputField.setSize(new Dimension(175,20));
outputField.setLocation(200,150);
subWin.add(outputField);
hd_calcButton = new JButton("Calculate");
hd_calcButton.setSize(150,30);
hd_calcButton.setLocation(100,80);
subWin.add(hd_calcButton);
hd_calcButton.addActionListener(new ButtonHandler());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public void react_hexToBinItem(){
JFrame f = new JFrame("Hex to Binary");
subWin = f.getContentPane();
subWin.setLayout(null);
subWin.setPreferredSize(new Dimension(400,200));
inputLabel = new JLabel("Enter hexadecimal number");
inputLabel.setSize(new Dimension(175,20));
inputLabel.setLocation(10,50);
subWin.add(inputLabel);
inputField = new JTextField("");
inputField.setSize(175,20);
inputField.setLocation(200,50);
subWin.add(inputField);
outputLabel = new JLabel("Binary Number");
outputLabel.setSize(new Dimension(175,20));
outputLabel.setLocation(10,150);
subWin.add(outputLabel);
outputField = new JTextField("");
outputField.setSize(new Dimension(175,20));
outputField.setLocation(200,150);
subWin.add(outputField);
hb_calcButton = new JButton("Calculate");
hb_calcButton.setSize(150,30);
hb_calcButton.setLocation(100,80);
subWin.add(hb_calcButton);
hb_calcButton.addActionListener(new ButtonHandler());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public void react_decToBinItem(){
JFrame f = new JFrame("Decimal to Binary");
subWin = f.getContentPane();
subWin.setLayout(null);
subWin.setPreferredSize(new Dimension(400,200));
inputLabel = new JLabel("Enter decimal number");
inputLabel.setSize(new Dimension(175,20));
inputLabel.setLocation(10,50);
subWin.add(inputLabel);
inputField = new JTextField("");
inputField.setSize(175,20);
inputField.setLocation(200,50);
subWin.add(inputField);
outputLabel = new JLabel("Binary Number");
outputLabel.setSize(new Dimension(175,20));
outputLabel.setLocation(10,150);
subWin.add(outputLabel);
outputField = new JTextField("");
outputField.setSize(new Dimension(175,20));
outputField.setLocation(200,150);
subWin.add(outputField);
db_calcButton = new JButton("Calculate");
db_calcButton.setSize(150,30);
db_calcButton.setLocation(100,80);
subWin.add(db_calcButton);
db_calcButton.addActionListener(new ButtonHandler());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public void react_decToHexItem(){
JFrame f = new JFrame("Decimal to Hexadecimal");
subWin = f.getContentPane();
subWin.setLayout(null);
subWin.setPreferredSize(new Dimension(400,200));
inputLabel = new JLabel("Enter decimal number");
inputLabel.setSize(new Dimension(175,20));
inputLabel.setLocation(10,50);
subWin.add(inputLabel);
inputField = new JTextField("");
inputField.setSize(175,20);
inputField.setLocation(200,50);
subWin.add(inputField);
outputLabel = new JLabel("Hexadecimal Number");
outputLabel.setSize(new Dimension(175,20));
outputLabel.setLocation(10,150);
subWin.add(outputLabel);
outputField = new JTextField("");
outputField.setSize(new Dimension(175,20));
outputField.setLocation(200,150);
subWin.add(outputField);
dh_calcButton = new JButton("Calculate");
dh_calcButton.setSize(150,30);
dh_calcButton.setLocation(100,80);
subWin.add(dh_calcButton);
dh_calcButton.addActionListener(new ButtonHandler());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == bd_calcButton){
String binIn = inputField.getText();
decimal = 0;
int base = 1;
for(int i = binIn.length() -1; i >= 0; i--){
char letter = binIn.charAt(i);
if(letter == '1'){
decimal += base;
base*= 2;
}
}
outputField.setText("" + decimal);
}
if(e.getSource() == bh_calcButton){
String binIn = inputField.getText();
decimal = 0;
int base = 1;
for(int i = binIn.length() - 1; i>= 0; i--){
char letter = binIn.charAt(i);
if(letter == '1'){
decimal += base;
base*=2;
}
}
int rem = 0;
String result = "";
for(int hexbase = 4096; hexbase >0; hexbase/=16){
int hexval = decimal/hexbase;
rem = decimal/hexbase;
if(hexval >=10){
switch(hexval){
case 10 : result += ('A'); break;
case 11 : result += ('B'); break;
case 12 : result += ('C'); break;
case 13 : result += ('D'); break;
case 14 : result += ('E'); break;
case 15 : result += ('F'); break;
}
}
else
result += hexval;
if(hexval >= 0)
decimal = rem;
}
outputField.setText(result);
}
if(e.getSource() == hd_calcButton){
hexIn = inputField.getText().toUpperCase();
decimal = 0;
int base = 1;
for(int i = hexIn.length() - 1; i >= 0; i--){
int pos = 0;
char letter = hexIn.charAt(i);
if(Character.isDigit(letter))
pos = letter - 48;
else
pos = letter - 55;
decimal += pos * base;
base*= 16;
}
outputField.setText("" + decimal);
}
if(e.getSource() == hb_calcButton){
hexIn = inputField.getText().toUpperCase();
int bin1;
decimal = 0;
int base = 1;
for(int i = hexIn.length() - 1; i >= 0; i--){
int pos = 0;
char letter = hexIn.charAt(i);
if(Character.isDigit(letter))
pos = letter - 48;
else
pos = letter - 55;
decimal += pos * base;
base *= 16;
}
int number = decimal;
String result = "";
for(int twobase = 128; twobase >= 0; twobase /= 2){
bin1 = number /twobase;
result += ("" + bin1);
if(number >= twobase)
number -= twobase;
}
outputField.setText(result);
}
if(e.getSource() == db_calcButton){
int number = Integer.parseInt(inputField.getText());
int bin1;
String result = "";
for(int twobase = 128; twobase > 0; twobase/=2){
bin1 = number /twobase;
result += ("" + bin1);
if(number >= twobase)
number -= twobase;
}
outputField.setText(result);
}
if(e.getSource() == dh_calcButton){
int number = Integer.parseInt(inputField.getText());
int rem = 0;
String result = "";
for(int hexbase = 4096; hexbase > 0; hexbase /= 16){
int hexval = number / hexbase;
rem = number % hexbase;
if(hexval >= 10){
switch(hexval){
case 10 : result += ('A'); break;
case 11 : result += ('B'); break;
case 12 : result += ('C'); break;
case 13 : result += ('D'); break;
case 14 : result += ('E'); break;
case 15 : result += ('F'); break;
}
}
else
result += hexval;
if(hexval >= 0)
number = rem;
}
outputField.setText(result);
}
}
}
}