Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

Problem With RSA Interface

Thread view: 
Soul Tech - 25 Mar 2008 07:48 GMT
Program complies and the interface shows up but there is no output
after pressing run button. How come this is happening?

Thanks.

package newpackage4;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;

public class NewClass5 extends JFrame{

  public NewClass5() {
       initiateComponents();
   }

   private JLabel TitleLabel;

   private JPanel innerPanel;

   private JLabel StringInputLabel;

   private JTextField InputStringTextField;

   private JButton RunButton;
   private JButton SaveButton;
   private JButton ResetButton;
   private JButton ExitButton;

   private JTextArea OutputWindow;

   private char[] plainText;

   private void initiateComponents() {
       getContentPane().setLayout(null);
       setTitle("Simplified Simulation: RSA Security Algorithm");
       setSize(new Dimension(1025, 703));
       setLocation(0,0);
       setResizable(false);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       getContentPane().setBackground(new Color(100,200,200));

       getContentPane().add(getInnerPanel());
       getContentPane().add(getTitleLabel());
       populateInnerPanel();
   }

   private JLabel getTitleLabel() {
       TitleLabel = new JLabel();
       TitleLabel.setFont(new Font("Arial",1,44));
       TitleLabel.setText("        RSA Algorithm");
       TitleLabel.setBackground(new Color(255,255,255));
       TitleLabel.setBorder(new EtchedBorder());
       TitleLabel.setBounds(300,40,500,60);
       return TitleLabel;
   }

    private JPanel getInnerPanel() {
       innerPanel = new JPanel();
       innerPanel.setLayout(null);
       innerPanel.setBackground(new Color(190,255,255));
       innerPanel.setBorder(new TitledBorder(
               new TitledBorder(null, "",
                   TitledBorder.DEFAULT_JUSTIFICATION,
                   TitledBorder.DEFAULT_POSITION,
                   new Font("Times New Roman", 1, 12))));
       innerPanel.setBounds(40,160,930,460);
       return innerPanel;
   }

   private void populateInnerPanel() {
       StringInputLabel = new JLabel();
       StringInputLabel.setFont(new Font("Bookman Old Style",0,13));
       StringInputLabel.setText("Enter in a word:");
       innerPanel.add(StringInputLabel);
       StringInputLabel.setBounds(65,20,110,20);

       InputStringTextField = new JTextField();
       InputStringTextField.setText(" ");
       innerPanel.add(InputStringTextField);
       InputStringTextField.setBounds(180,20,220,20);

       RunButton = new JButton();
       RunButton.setFont(new Font("Bookman Old Style",1,13));
       RunButton.setText("Run");
       innerPanel.add(RunButton);
       RunButton.setBounds(65,420,70,20);

       SaveButton = new JButton();
       SaveButton.setFont(new Font("Bookman Old Style",1,13));
       SaveButton.setText("Save");
       innerPanel.add(SaveButton);
       SaveButton.setBounds(305,420,70,20);

       ResetButton = new JButton();
       ResetButton.setFont(new Font("Bookman Old Style",1,13));
       ResetButton.setText("Reset");
       innerPanel.add(ResetButton);
       ResetButton.setBounds(555,420,80,20);

       ExitButton = new JButton();
       ExitButton.setFont(new Font("Bookman Old Style",1,13));
       ExitButton.setText("Exit");
       innerPanel.add(ExitButton);
       ExitButton.setBounds(794,420,70,20);

       OutputWindow = new JTextArea();
       Font equalSpacedFont = new Font("Monospaced",Font.PLAIN,14);
       OutputWindow.setFont(equalSpacedFont);
       OutputWindow.setEditable(false);
       JScrollPane scrollPane = new JScrollPane(OutputWindow);
       scrollPane.setBounds(65,70,800,330);
       innerPanel.add(scrollPane);

   }

   public void actionPerformed(ActionEvent event) {
       String arg = event.getActionCommand();

           if(arg.equals("Run"))
           {
               try
               {
                   int plainTextLength = plainText.length;
                   String inputString = InputStringTextField.getText();
                   int inputStringLength = inputString.length();
                   char[] result = new char[inputStringLength];
                   //char currentSource = inputString.charAt(i);
               }
               catch (Exception e) {

               }

               int [] cipherText = new int[plainText.length];

               System.out.print("\nPlaintext:\t ");
               for(int i = 0; i < plainText.length; i++)
               {
                 int tmp = (int)plainText[i]-64;
                 cipherText[i] = (tmp*tmp*tmp)%33;
                 System.out.print("\t"+plainText[i]);
               }
               System.out.print("\nCiphertext:\t");
               for(int i = 0; i < cipherText.length; i++)
               {
                   System.out.print("\t"+cipherText[i]);
               }
           }
   }

   public static void main(String args[]) {
       SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
               JFrame.setDefaultLookAndFeelDecorated(true);
               NewClass5 nc5 = new NewClass5();
               nc5.setVisible(true);
           }
       });

     }
}
Matt Humphrey - 25 Mar 2008 13:17 GMT
> Program complies and the interface shows up but there is no output
> after pressing run button. How come this is happening?
>
> Thanks.

<code snipped>

I don't see where you add the action listener to the RunButton.  By
convention, instance variables should start with lower case to help
distinguish them from class names.  Also, you're swallowing exceptions.

Matt Humphrey http://www.iviz.com/
Lew - 25 Mar 2008 15:00 GMT
"Soul Tech" <carla.pinate@gmail.com> wrote in message
> news:c81d32df-0486-47f4-ac03-0e76277b4c52@n77g2000hse.googlegroups.com...
>> Program complies and the interface shows up but there is no output
>> after pressing run button. How come this is happening?

> I don't see where you add the action listener to the RunButton.  By
> convention, instance variables should start with lower case to help
> distinguish them from class names.  Also, you're swallowing exceptions.

Cross-posted so both groups to which the OP multi-posted can see this answer.

Signature

Lew

RedGrittyBrick - 25 Mar 2008 16:58 GMT
> Program complies and the interface shows up but there is no output
> after pressing run button. How come this is happening?

I think Matt has spotted the cause.

Here's some comments on style in case you're interested (if not, please
ignore).

> package newpackage4;
>
[quoted text clipped - 5 lines]
>
> public class NewClass5 extends JFrame{

Nowadays I encapsulate JFrame rather than extend it. I'm not sure why :-)

In cases where my imagination fails, I'd name the class Class5, instead
on NewClass5 to avoid things like "new NewClass5()". If you refactor
this, would you call the old version OldNewClass5 and the new one
NewNewClass5?

>    public NewClass5() {
>         initiateComponents();
>     }

Unless initiateComponents() gets called from somewhere else I don't see
the value of moving the construction code out of the constructor.

>     private JLabel TitleLabel;
>
>     private JPanel innerPanel;

I note that innerPanel is accessible to all methods in NewClass5.

>     private JLabel StringInputLabel;
>
[quoted text clipped - 11 lines]
>     private void initiateComponents() {
>         getContentPane().setLayout(null);

Eeek!

>         setTitle("Simplified Simulation: RSA Security Algorithm");
>         setSize(new Dimension(1025, 703));
[quoted text clipped - 6 lines]
>         getContentPane().add(getTitleLabel());
>         populateInnerPanel();

I hate chains of method calls. I'd call populateInnerPanel right after I
call initiateComponents(). That way the large scale structure of the
program isn't hidden.

The "getContentPane()." prefixes can be omitted.
If they were needed, I'd do
  Container c = getContentPane();
  c.setLayout(...);
  ...
  c.add(...);
  c.add(...);
I find less dense code easier to read.

>     }
>
>     private JLabel getTitleLabel() {
>         TitleLabel = new JLabel();

Variable names should start with a lowercase letter.

I use short names for short scope. I might have
   JLabel l = new JLabel();
   ...
   return l;

>         TitleLabel.setFont(new Font("Arial",1,44));
>         TitleLabel.setText("        RSA Algorithm");
>         TitleLabel.setBackground(new Color(255,255,255));
>         TitleLabel.setBorder(new EtchedBorder());
>         TitleLabel.setBounds(300,40,500,60);
>         return TitleLabel;

Since the scope of TitleLabel is NewClass5, you don't need to return it
from a private method. You could have a void method. I'd instead create
a local label and return a reference to that. This is because I like to
develop the habit of making chunks of code relatively independent of
context. I think of this as being related to ensuring good encapsulation.

>     }
>
[quoted text clipped - 22 lines]
>         innerPanel.add(InputStringTextField);
>         InputStringTextField.setBounds(180,20,220,20);

I hope you have a visual GUI editor producing these coordinates. I'd
much rather use a layout manager.

>         RunButton = new JButton();
>         RunButton.setFont(new Font("Bookman Old Style",1,13));
[quoted text clipped - 4 lines]
>         SaveButton = new JButton();
>         SaveButton.setFont(new Font("Bookman Old Style",1,13));

I'd have long ago re-factored 'new Font("Bookman Old Style",1,13) into a
local variable and I'd probably make the font-name string a constant
declared at the top of the source file.

>         SaveButton.setText("Save");
>         innerPanel.add(SaveButton);
[quoted text clipped - 11 lines]
>         innerPanel.add(ExitButton);
>         ExitButton.setBounds(794,420,70,20);

I'd replace the prior 24 lines with
          p.add(makeJButton(RUN));
          p.add(makeJButton(SAVE));
          p.add(makeJButton(RESET));
          p.add(makeJButton(EXIT));

I might even replace them with something using an enum, like
          for (ButtonName b: ButtonName.values())
              p.add(makeJButton(b));

>         OutputWindow = new JTextArea();
>         Font equalSpacedFont = new Font("Monospaced",Font.PLAIN,14);
>         OutputWindow.setFont(equalSpacedFont);

You define a variable for a font you use once, you don't for one you use
four times. I'd probably do the opposite.

>         OutputWindow.setEditable(false);
>         JScrollPane scrollPane = new JScrollPane(OutputWindow);
[quoted text clipped - 7 lines]
>
>             if(arg.equals("Run"))

To avoid typos causing problems, I use Enums for values like "Run". One
day I must get into the habit of using Action.

>             {
>                 try
>                 {
>                     int plainTextLength = plainText.length;

I can't see where this variable is used - doesn't your IDE warn you
about this?

>                     String inputString = InputStringTextField.getText();

>                     int inputStringLength = inputString.length();
>                     char[] result = new char[inputStringLength];

You saved three characters ".()" at the expense of an extra line. I'd
not make that trade-off.
     char[] result = new char[inputString.length()];

>                     //char currentSource = inputString.charAt(i);
>                 }
>                 catch (Exception e) {
>
>                 }

Eek, an ignored exception!

I'd at least do System.err.println(e.getMessage());

I'd preferably log it and work out how the program should handle this. I
usually pop up a JDialog and maybe either exit the program or let the
user choose.

>                 int [] cipherText = new int[plainText.length];
>
[quoted text clipped - 11 lines]
>                 }
>             }

All the above is blocking the EDT. It may not matter for small amounts
of text but it's not a good habit.

I'd separate the 'encryption' into separate methods (and/or classes)
from the GUI code.

I'd also do something if arg doesn't equal "Run". Like emit an error
message about an unexpected command. It's an old "defensive programming"
habit.

>     }
>
[quoted text clipped - 10 lines]
>       }
> }

There seems to be a total absence of Javadocs and comments. OK for code
put up for discussion but probably not OK for anything to be used in
production.

Signature

RGB



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.