here is the following code
in java applet..

//this is one applet which has to be opened when button is clicked...
import java.awt.*;
import java.applet.*;

import java.awt.event.*;

public class Order_info extends Applet implements ItemListener
{
private Label l,l1,l2;
private TextField t=new TextField();
private Choice list;
String s,selection;


public void init()
{
this.setLayout(new BorderLayout(10,10)); 
Panel p1=new Panel(new GridLayout(3,5,7,4));
l2=new Label("  ORDER    INFORMATION  ");
l2.setFont(new Font("Courier",Font.BOLD,48));
p1.add(l2);
add(p1,"North");

Panel p=new Panel(new GridLayout(3,3,4,4));
 l=new Label("ITEM");

p.add(l);

 list = new Choice();
list.addItem("LCD TV");
list.addItem("FRIDGE");
list.addItem("AIRCONDITIONER");
list.addItemListener(this);
p.add(list);


l1=new Label("PRICE");
p.add(l1);
p.add(t);
add(p,"Center");

}
public void itemStateChanged(ItemEvent e)
{selection=list.getSelectedItem();

if(selection.equals("LCD TV"))
{
s="15000";
t.setText(s);
}
else if(selection.equals("FRIDGE"))

{s="20000";
t.setText(s);
}
else if(selection.equals("AIRCONDITIONER"))
{
s="25000";
t.setText(s);
}
}
public void paint(Graphics g)
{

g.drawRect(0,0,this.getSize().width-1,this.getSize().height-1);


}


}

//here is another applet where button  b6 is clicked..


import java.io.*;
import java.math.*;
import java.security.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public final class MsgDigestApp extends java.applet.Applet implements ActionListener
{
private  Button b0,b1,b2,b3,b4,b5,b6;
private TextArea message=new TextArea("ENTER PURCHASE INFORMATION");
private TextArea message1=new TextArea("ENTER ORDER INFORMATION");
private TextArea a=new TextArea();
private TextArea a1=new TextArea();
private TextField digest=new TextField();
private TextField digest1=new TextField();
private TextField digest2=new TextField();
private TextField t=new TextField();
private TextField n=new TextField();
private TextField phi=new TextField();
private TextField pub=new TextField();
private TextField dec_msg=new TextField();
private TextField en_msg=new TextField();
private MessageDigest sha;
private Label l,l1,s1,s2,s3,s4,s5,s6;
private Label la1,la2,la3,la4,la5,la6,la7;


public void init()
{
this.setLayout(new BorderLayout(10,10)); 

Panel p1=new Panel();
l=new Label("purchase info");
p1.add(l);
p1.add(message);
//l1=new Label("Order info");
//p1.add(l1);
b6=new Button("ORDER INFO");// this button is clicked to open another applet
b6.addActionListener(this);
p1.add(b6);
//p1.add(message1);
p1.add(message1);
add(p1,"North");

Panel p2 = new Panel(new GridLayout(4,2, 5, 5));
s1=new Label("hash1");
p2.add(s1);
p2.add(digest);
s2=new Label("hash2");
p2.add(s2);
p2.add(digest1);
s3=new Label("concat");
p2.add(s3);
p2.add(t);
s4=new Label("hash resultant");
p2.add(s4);
p2.add(digest2);
add(p2,"South");



Panel p=new Panel(new GridLayout(2,1,4,4));
b0=new Button("Clear text");
b0.addActionListener(this);
p.add(b0);
b1=new Button("Hash");
b1.addActionListener(this);
p.add(b1);
add(p,"West");
setAlgo("sha-1");
Panel p5=new Panel(new GridLayout(10,1,7,7));
la1=new Label("enter p");
p5.add(la1);
p5.add(a);
la2=new Label("enter q");
p5.add(la2);
p5.add(a1);
s5=new Label("Value(n)");
p5.add(s5);
p5.add(n);
la3=new Label("phi");
p5.add(la3);
p5.add(phi);
la4=new Label("Public Key: e");
p5.add(la4);
p5.add(pub);

s6=new Label("Encrypted msg");
p5.add(s6);
p5.add(en_msg);
la7=new Label("Decrypted msg");
p5.add(la7);
p5.add(dec_msg);
add(p5,"Center");
Panel p6=new Panel(new GridLayout(2,1,4,4));
b2=new Button("Cal N");
b2.addActionListener(this);
p6.add(b2);
b3=new Button("Encrypt");
b3.addActionListener(this);
p6.add(b3);
b4=new Button("Decrypt");
b4.addActionListener(this);
p6.add(b4);
add(p6,"East");


}

 public void itemStateChanged(ItemEvent evt)
   {  if (evt.getStateChange() == ItemEvent.SELECTED) 
         setAlgo((String)evt.getItem());
   }

   private void setAlgo(String alg)
   {  try
      {  sha = MessageDigest.getInstance(alg);
} catch(NoSuchAlgorithmException e)
      {  digest.setText("" + e);
digest1.setText("" + e);
digest2.setText(""+e);
      }
   }
  


public void actionPerformed(ActionEvent evt)
{
String arg=evt.getActionCommand();
 
if(arg.equals("ORDER INFO"))//to open new applet..what should be the right code
{
getAppletContext().showDocument(order);
}
else if(arg.equals("Hash"))
{
String m=message.getText();
String m1=message1.getText();
String m2=t.getText();

computeDigest(m.getBytes());
computeDigest1(m1.getBytes());
con();
computeDigest2(m2.getBytes());

}
else if(arg.equals("Cal N"))
{
cal_n();
choose_e();
}
else if(arg.equals("Encrypt"))
{
en();
}
else if(arg.equals("Decrypt"))
{
dec();
}
else if(arg.equals("Clear text"))
{
message.setText("");
digest.setText("");
message1.setText("");
digest1.setText("");
t.setText("");
digest2.setText("");
n.setText("");
phi.setText("");
pub.setText("");
a.setText("");
a1.setText("");
en_msg.setText("");
dec_msg.setText("");
}
 }

private void computeDigest(byte[] b) 
{
sha.reset();
sha.update(b);
byte[] hash1=sha.digest();
String d="";
String e="";
int usbyte=0;
for(int i=0;i<hash1.length;i++)
{
usbyte=hash1[i]&0xFF;
if(usbyte<16)
d+="0"+Integer.toHexString(usbyte);
else
d+=Integer.toHexString(usbyte);
}
e=d.toUpperCase();
digest.setText(""+e);

}
private void computeDigest1(byte[] b) 
{

sha.reset();
sha.update(b);
byte[] hash2=sha.digest();
String d="";
String f="";
int usbyte=0;
for(int i=0;i<hash2.length;i++)
{
usbyte=hash2[i]&0xFF;
if(usbyte<16)
d+="0"+Integer.toHexString(usbyte);
else
d+=Integer.toHexString(usbyte);
}
f=d.toUpperCase();
digest1.setText(""+f);

}
private void computeDigest2(byte[] b) 
{

sha.reset();
sha.update(b);
byte[] hash1=sha.digest();
String d="";
String e="";
int usbyte=0;
for(int i=0;i<hash1.length;i++)
{
usbyte=hash1[i]&0xFF;
if(usbyte<16)
d+="0"+Integer.toHexString(usbyte);
else
d+=Integer.toHexString(usbyte);
}
e=d.toUpperCase();
digest2.setText(""+e);
}

private void con()
{
String str,x,y;
x=digest.getText();
y=digest1.getText();
str=x+y;
t.setText(""+str);
}


private void cal_n()
{
String mul1,mul2,mul0,mul;
int x,y,x1,y1,mul3,mul4;
mul1=a.getText();
mul2=a1.getText();
x=Integer.parseInt(mul1);
y=Integer.parseInt(mul2);
mul3=x*y;
mul=String.valueOf(mul3);
n.setText(""+mul);
x1=Integer.parseInt(mul1)-1;
y1=Integer.parseInt(mul2)-1;
mul4=x1*y1;
mul0=String.valueOf(mul4);
phi.setText(""+mul0);
}
private void choose_e()
{
String n1=phi.getText();
BigInteger n2=new BigInteger(n1);
String n3=pub.getText();
BigInteger n4=new BigInteger(n3);
while( n4.gcd( n2 ).compareTo( BigInteger.ONE ) != 0 )
{
n4 = n4.add( BigInteger.ONE );
pub.setText(n4.toString());
}

}
private void en()
{
String n1=n.getText();
 BigInteger n2= new BigInteger(n1);
String n3=pub.getText();
 BigInteger n4= new BigInteger(n3);
String  hexmsg=digest2.getText();
 BigInteger v= new BigInteger(hexmsg,16);

BigInteger c = v.modPow( n4, n2 );

String c2=String.valueOf(v);
en_msg.setText(""+c2);
}
private void dec()
{String n1=n.getText();
 BigInteger n2= new BigInteger(n1);
String c1=en_msg.getText();
BigInteger c2=new BigInteger(c1);
String n3=pub.getText();
 BigInteger n4= new BigInteger(n3);
String n5=phi.getText();
BigInteger n6= new BigInteger(n5);
BigInteger d =n4.modInverse( n6 );
BigInteger m=c2.modPow(d,n2);  
dec_msg.setText(""+m.toString(16));
}

 
public void paint(Graphics g)
{
setBackground(Color.black);
g.setColor(Color.blue);
g.drawRect(0,0,this.getSize().width-1, this.getSize().height-1) ; 

}
}

Recommended Answers

All 14 Replies

Please wrap your posted code in code tags. See the [ CODE ] icon above the input box.

Where do you want this applet window to open?
Normally applets are displayed in the context of an HTML page in a browser.
Do you want the browser to read another HTML page with the other applet?
See the AppletContext showDocument method to do that.

Otherwise explain where you want the second window to be displayed.

sorry..further i'll surely post code in code icon...
actually i tried with this show document but its not working..
can i get a new applet window when clicking on the "order info" button in the old applet

its not working..

Please explain. Do you get errors? See the java console for any output.
If no errors, what does happen?
Is the showDocument method being called?
Is the URL correct?

its shows the error in the line
URl order=new URL("----")
and showDocument is not being called

its shows the error

Sorry, I can't look over your shoulder and see what that error message was. You will have to post it here. Also describe where it was shown and what the "its" is.

if you are dealing with button, then you need to implement ActionListener and in actionPerformed method you make visible another window by making its object (e.g. e2.setVisible(true))and make current windowinvisible if u dont want to be visible by
e1.setVisible(false)

Can anyone help me to call an applet from another applet.When i click an ok button i want a new applet window to be displayed.But this applet window is created in another file.

If an applet is being viewed in an HTML page loaded in a browser, you can use the showDocument() method to ask the browser to load a new HTML page with a new applet.
If you want your own window, you could create a new frame and add the applet to the frame's GUI. That applet would not be supported by the browser and you would have to call the necessary methods to start the applet's execution and support any of the applet context methods it uses.

Thanku sir for your help.
Im not loading applet in an HTML page.Instead im trying it with appletviewer command.Currently im having two applet files.The first one just displays a button .On clicking that button i want my second applet file's content to be displayed.The second file contains an animation.

To execute the code in an applet, the applet should be loaded into a program like appletviewer or a browser that provides specific support for many applet methods. I guess that appletviewer might not support the showDocument method. The problem would be to start another instance of a program that can support an applet like a browser or appletviewer. You can start other programs by using the Runtime and Process classes. But using them could require that you give the applet permission to use those classes.

This seems like a silly way to write the application. Why not make the code that does the animation a separate class that both applets can use by creating an instance of the class and calling its methods.

Thanku sir for your valuable..

Hey can u help me with this code..?? Its a simple game of moving darts I made on applet..
I want to restart the game from starting when i click replay but this ain't happening, please check the code:
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class game extends Applet implements Runnable, ActionListener
{
Thread t=null;
boolean flag;
int BoardY=0,chngdir=1,hitstop=0,arrX=0,chances=2,arr=3,pts=0,arrspeed;
String pname;
Button Continue,Shoot,Replay,Slow,Medium,Fast;
TextField Name;
Image img;

 public void init()
 {

     BoardY=0;
     chances=3;
     arr=3;
     pts=0;
     arrspeed=1;

     Label namep = new Label("Name: ", Label.RIGHT);
     add(namep);

     Name = new TextField(12);
     add(Name);
     Name.addActionListener(this);

     Label Speed = new Label("Arrow Speed");
     add(Speed);

     Slow = new Button("Slow");
     add(Slow);
     Slow.addActionListener(this);

     Medium = new Button("Medium");
     add(Medium);
     Medium.addActionListener(this);

     Fast = new Button("Fast");
     add(Fast);
     Fast.addActionListener(this);

     Shoot = new Button("Shoot");
     add(Shoot);
     Shoot.addActionListener(this);
     Shoot.setEnabled(false);

     Continue = new Button("Continue");
     add(Continue);
     Continue.addActionListener(this);

     Replay = new Button("Replay");
     add(Replay);
     Replay.addActionListener(this);
     Replay.setEnabled(false);

     //img = getImage(getDocumentBase(), getParameter("img"));
     setBackground(Color.orange);
     t = new Thread(this);
     flag = false;
     t.start();

     }



 public void actionPerformed(ActionEvent ae)
 {
 String str = ae.getActionCommand();

if(str.equals("Shoot"))                      //IF SHOOT IS CLICKED
 {
     Continue.setEnabled(true);
     Shoot.setEnabled(false);
     hitstop=1;
 }
 if(str.equals("Replay"))                  //IF REPLAY IS CLICKED
 {  
     BoardY=0;
     chances=3;
     arr=3;
     pts=0;
     arrspeed=1;
        repaint();
    Shoot.setEnabled(true);
    Slow.setEnabled(true);
    Medium.setEnabled(true);
    Fast.setEnabled(true);
    Name.setEnabled(true);
    Replay.setEnabled(false);
    Continue.setEnabled(true);
    init();
 }
 if(str.equals("Continue"))                 //IF CONTINUE IS CLICKED
 {
    repaint();
    Continue.setEnabled(false);
    chances=arr-1;
    arr--;
    if(arr>0)

    Shoot.setEnabled(true);

    else
    {Replay.setEnabled(true);
    stop();
    }
 }
 if(str.equals("Slow"))                   // SLOW IS SELECTED
 {
     Continue.setEnabled(true);
     Name.setEnabled(true);
     Shoot.setEnabled(true);
     Slow.setEnabled(false);
     arrspeed= 10;
 }
 if(str.equals("Medium"))                //IF MEDIUM IS SELECTED
 {
     Name.setEnabled(true);
     Shoot.setEnabled(true);
     Continue.setEnabled(true);
     Medium.setEnabled(false);
     arrspeed= 15;

 }
 if(str.equals("Fast"))                     //IF FAST IS SELECTED
 {

     Name.setEnabled(true);
     Shoot.setEnabled(true);
     Continue.setEnabled(true);
     Fast.setEnabled(false);
     arrspeed= 20;
 }

 repaint();
 }

public void run()
{

   /*BoardY=0;
   chances=3;
   arr=3;
   pts=0;
   arrspeed=1;*/
   for(chances=3,arr=3;chances>0;)
   {

    BoardY=0;hitstop=0;chngdir=1;arrX=0;chances=0;
                                                                                       //Loop for every chance
   for(;chngdir!=0;)
   {

   try
    {
         repaint();
         Thread.sleep(100);




     //For controlling the movement of the board up and down.
      if(chngdir == 1)
      {
        BoardY+=8;
        if(BoardY>=380)
        chngdir = -1;
      }
      if(chngdir==-1)
      {
        BoardY-=8;
        if(BoardY<=20)
        chngdir = 1;
      }

      //For movement of the arrow
      if (hitstop==1)
      {
        if(arrX<395) //if arrow hasnt reached the position of the board increase the x coordinate of arrow(arrX).

        arrX+=arrspeed;


        else // else increase the points accordingly.
        {


        chngdir=0;
        if(BoardY>=150&&BoardY<=230)
        {
             if(BoardY<=170||BoardY>=210)
                pts+=20;
                else if(BoardY<=185||BoardY>=195)
                    pts+=35;
                    else pts+=50;

        }
        Thread.sleep(3000);
       }
      }
       if(flag)
        break;
    }
    catch(InterruptedException e){}
   }


 }

 }



 public void paint(Graphics g)
 {


    g.drawString(("Arrows Left: "+arr),10,430);
    g.drawString(("Score: " + pts),200,430);
    int x[] = {30+arrX,35+arrX,30+arrX};
    int y[] = {245,250,255};
    g.fillPolygon(x,y,3);
    g.fillRect(15+arrX,249,20,2);
    g.setColor(Color.yellow);                    //Board coordinates
    g.fillRect(425,20+BoardY,25,20);
    g.setColor(Color.green);
    g.fillRect(425,40+BoardY,25,15);
    g.setColor(Color.red);
    g.fillRect(425,55+BoardY,25,10);
    g.setColor(Color.green);
    g.fillRect(425,65+BoardY,25,15);
    g.setColor(Color.yellow);
    g.fillRect(425,80+BoardY,25,20);
    g.setColor(Color.black);
    g.drawLine(425,20+BoardY,425,100+BoardY);        //Border
    g.drawString("20",450,35+BoardY);                 //Pts-score
    g.drawString("35",450,53+BoardY);
    g.drawString("50",450,65+BoardY);
    g.drawString("35",450,78+BoardY);
    g.drawString("20",450,95+BoardY);
   pname = Name.getText();
    if(arr==0&&pts!=0)
      g.drawString("Congrats " +pname+" !!! You've scored "+pts+" Points",150,250);
    if(arr==0&&pts==0)
        g.drawString("Better luck next time " +pname+" !!! You've scored 0 Points",100,250);

 }

public void stop()
{
t=null;
flag = true;
}

}

Just check the replay parameters... 66 to 81 lines... I have to submit this by monday...

@Nimzy Please start your own thread with your questions and your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.