• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Two applets not communicating

This is my first applet

import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import java.applet.*;
import java.applet.Applet;
import java.lang.Math.*;
import java.net.*;

public class main extends Applet implements ItemListener,ActionListener {

Font newfont;
public static int posans=0;
public static int negans=0;
public static int total=0;
public static int arcp=0;
public static int arcn=0;
Button votebtn = new Button("Vote");
CheckboxGroup vote = new CheckboxGroup();
Checkbox yes = new Checkbox("Yes",false,vote);
Checkbox no = new Checkbox("NO",false,vote);
Label LabelN = new Label();
Label LabelY = new Label();

public void init() {

newfont = new Font("Arial",Font.BOLD,20);
setBackground(new Color(31,20,239));
yes.addItemListener(this);
no.addItemListener(this);
votebtn.addActionListener(this);
add(no);
add(yes);
add(votebtn);
add(LabelN);
add(LabelY);
}

public void itemStateChanged(ItemEvent e){

if(no.getState() == true){

LabelN.setVisible(true);
LabelY.setVisible(false);
}
else{

LabelY.setVisible(true);
LabelN.setVisible(false);

}

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == votebtn)
{
if(yes.getState()== true){

posans++;
total++;
arcp=Math.round((posans/total)*360);
arcn=Math.round((negans/total)*360);
try {
      getAppletContext ().showDocument (new URL ("file://C:/WINNT/Temp/main2.html"));
    }
catch (MalformedURLException exception) {}


}
else
{

negans++;
total++;
arcp=Math.round((posans/total)*360);
arcn=Math.round((negans/total)*360);
try {
      getAppletContext ().showDocument (new URL ("file://c:/WINNT/Temp/main2.html"));
    }
catch (MalformedURLException exception) {}
}
}
}

public void paint(Graphics g){

g.setFont(newfont);
g.drawString("Is the professors`",65,30);
g.drawString("evaluation system reliable?",15,55);
yes.setLocation(120,90);
no.setLocation(120,120);
votebtn.setLocation(120,150);
LabelY.setVisible(false);
LabelN.setVisible(false);
LabelY.setLocation(50,190);
LabelN.setLocation(50,190);
LabelN.setAlignment(Label.CENTER);
LabelY.setAlignment(Label.CENTER);
LabelN.setSize(200,23);
LabelY.setSize(200,23);
LabelN.setText("You have choosed to answer No");
LabelY.setText("You have choosed to answer Yes");

}

}

and this is my second applet

import java.applet.*;
import java.awt.*;
import java.awt.Component;
import java.lang.Math;

public class main2 extends Applet {

Font newfont,newfont2;
int percent = 0;
String no;
String yes;
String tot;

public void init(){

percent = Math.round((main.posans/main.total)*100);
no = 100-percent + "%(" + main.negans + " votes)" ;
yes = percent+"%("+main.posans+" votes)";
tot = "Total Votes: "+main.total;
setBackground(new Color(31,20,239));
newfont=new Font("Arial",Font.BOLD,18);
newfont2=new Font("Arial",Font.PLAIN,16);

  }

public void paint(Graphics g){

  g.setColor(new Color(231,145,67));
  g.fillArc(65,120,150,150,0,main.arcp);
  g.fillRect(105,60,15,15);
  g.setColor(new Color(0,0,0));
  g.fillArc(65,120,150,150,360,-main.arcn);
  g.fillRect(105,90,15,15);
  g.setFont(newfont);
  g.setColor(new Color(0,0,0));
  g.drawArc(65,120,150,150,0,360);
  g.drawString("Voting Results ",85,30);
  g.setFont(newfont2);
  g.drawString("No",125,103);
  g.drawString("Yes",125,73);
  g.drawString(no,160,103);
  g.drawString(yes,160,73);
  g.drawString(tot,75,290);

                            }



}

this is my first html file

<applet code="main.class" width=400 height=400>
</applet>

this is my second html file

<applet code="main2.class" width=400 height=400>
</applet>

       For some strange reason when i compile them (javac main.java main2.java) and execute them the second applet isn`t loaded by the first(when i click on the button).Can anybody help me?


0
cs00003
Asked:
cs00003
1 Solution
 
CEHJCommented:
You con't communicate between two applets if they're in different pages. Put them in the same page.
0
 
cs00003Author Commented:
   But i want my second applet to be loaded only when the first one loads it:

getAppletContext ().showDocument (new URL ("file://C:/WINNT/Temp/main2.html"));

    How can i achieve it?Also i want to ask how can i use cookies in my applet?
0
 
CEHJCommented:
Actually i *might* be misleading you with that previous assertion, but i can't check at the moment.

>>Also i want to ask how can i use cookies in my applet?

AFAIK, you can't. Cookies are for server-side use.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cs00003Author Commented:
I need a programm that will do the folowing job:

when i insert a value to my integer variable n it will do

TextField tf1 = new  TextField();
TextField tf2 = new  TextField();
TextField tf3 = new  TextField();
            .
            .
            .
TextField tfn = new  TextField();            
 
0
 
cs00003Author Commented:
I need a programm that will do the folowing job:

when i insert a value to my integer variable n it will do

TextField tf1 = new  TextField();
TextField tf2 = new  TextField();
TextField tf3 = new  TextField();
            .
            .
            .
TextField tfn = new  TextField();            
 
0
 
CEHJCommented:
You seem to have a kind of 'rolling' question(s)! Just create an array of n text fields:

TextField[] textFields = new TextField[n];
for (int i = 0;i < n;i++) {
   textFields[i] = new TextField();
}
// give last one an initial value
textFields[n].setText("Init");
0
 
CleanupPingCommented:
cs00003:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now