Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.Color;
- import javax.swing.JApplet;
- import javax.swing.JRadioButton;
- import javax.swing.ButtonGroup;
- import javax.swing.JLabel;
- import javax.swing.JTextArea;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class trick extends JApplet implements ActionListener{
- public static int line = 1;
- public static int char_per_line = 5;
- private TextField[] txtF = new TextField[16];
- private JTextArea input = new JTextArea(line, char_per_line);
- private JTextArea input2 = new JTextArea(line, char_per_line);
- private JTextArea error = new JTextArea(5, 60);
- public static String finals;
- public void init(){
- Container display = getContentPane();
- display.setBackground(Color.ORANGE);
- display.setLayout(new FlowLayout());
- JLabel intro = new JLabel("CHEEZEE'S BINARY REGISTER\n");
- intro.setForeground(Color.RED);
- display.add(intro);
- ButtonGroup RadioGroup = new ButtonGroup();
- ButtonGroup RadioGroup2 = new ButtonGroup();
- JRadioButton add = new JRadioButton("ADD");
- RadioGroup.add(add);
- add.addActionListener(this);
- JRadioButton sub = new JRadioButton("SUBTRACT");
- RadioGroup.add(sub);
- sub.addActionListener(this);
- JRadioButton Do = new JRadioButton("DO");
- RadioGroup2.add(Do);
- Do.addActionListener(this);
- JRadioButton clear = new JRadioButton("CLEAR");
- RadioGroup2.add(clear);
- clear.addActionListener(this);
- JLabel Enter = new JLabel("Enter a binary number");
- display.add(Enter);
- display.add(input);
- display.add(add);
- display.add(sub);
- JLabel Enter2 = new JLabel("Enter another binary number");
- display.add(Enter2);
- display.add(input2);
- display.add(Do);
- display.add(clear);
- display.add(error);
- for (int y = 0; y < 16; y++){
- txtF[y] = new TextField();
- }
- display.add(txtF[0]);
- display.add(txtF[1]);
- display.add(txtF[2]);
- display.add(txtF[3]);
- display.add(txtF[4]);
- display.add(txtF[5]);
- display.add(txtF[6]);
- display.add(txtF[7]);
- display.add(txtF[8]);
- display.add(txtF[9]);
- display.add(txtF[10]);
- display.add(txtF[11]);
- display.add(txtF[12]);
- display.add(txtF[13]);
- display.add(txtF[14]);
- display.add(txtF[15]);
- }
- public void actionPerformed(ActionEvent e){
- String click = e.getActionCommand();
- if (click.equals("ADD")){
- String one = input.getText();
- long chan = Long.parseLong(one);
- long rem;
- while (chan > 0){
- rem = chan % 10;
- chan = chan/10;
- if (rem != 0 && rem != 1){
- error.setText("Please make sure you enter a binary number");
- }
- }
- int dec = Integer.parseInt(one,2);
- String two = input2.getText();
- long chan2 = Long.parseLong(two);
- long rem2;
- while (chan > 0){
- rem2 = chan2 % 10;
- chan2 = chan2/10;
- if (rem2 != 0 && rem2 != 1){
- error.setText("Please make sure you enter a binary number");
- }
- }
- int dec2 = Integer.parseInt(two,2);
- int ans = dec + dec2;
- if (ans > 32768){
- error.setText("Overflow the convert only has a limit of 16 bits");
- }
- String runz = "";
- while (ans > 0){
- int y = ans%2;
- runz = runz + y + "";
- ans = ans/2;
- }
- finals = new StringBuffer(runz).reverse().toString();
- for (int u = 0; u < 16; u++){
- txtF[u].setText("");
- }
- }
- else if(click.equals("SUBTRACT")){
- String one = input.getText();
- long chan = Long.parseLong(one);
- long rem;
- while (chan > 0){
- rem = chan % 10;
- chan = chan/10;
- if (rem != 0 && rem != 1){
- error.setText("Please make sure you enter a binary number");
- }
- }
- int dec = Integer.parseInt(one,2);
- String two = input2.getText();
- long chan2 = Long.parseLong(two);
- long rem2;
- while (chan > 0){
- rem2 = chan2 % 10;
- chan2 = chan2/10;
- if (rem2 != 0 && rem2 != 1){
- error.setText("Please make sure you enter a binary number");
- }
- }
- int dec2 = Integer.parseInt(two,2);
- int ans = dec - dec2;
- if(ans <0){
- error.setText("Underflow make sure you are not producing a neagtive number!");}
- if (ans > 32768){
- error.setText("Overflow the convert only has a limit of 16 bits");}
- String runz = "";
- while (ans > 0){
- int y = ans%2;
- runz = runz + y + "";
- ans = ans/2;
- }
- finals = new StringBuffer(runz).reverse().toString();
- for (int u = 0; u < 16; u++){
- txtF[u].setText("");
- }
- }
- else if (click.equals("DO")){
- int size = finals.length();
- for (int y = 0;y < size;y++ ){
- String single = Character.toString(finals.charAt(y));
- txtF[y].setText(single);
- }
- }
- else if (click.equals("CLEAR")){
- input.setText("");
- input2.setText("");
- for (int r = 0; r < 16; r++){
- txtF[r].setText("");
- }
- error.setText("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement