?
Solved

Two applets not communicating

Posted on 2003-03-28
9
Medium Priority
?
289 Views
Last Modified: 2013-11-23
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
Comment
Question by:cs00003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8226469
You con't communicate between two applets if they're in different pages. Put them in the same page.
0
 

Author Comment

by:cs00003
ID: 8226617
   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
 
LVL 86

Expert Comment

by:CEHJ
ID: 8226693
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:cs00003
ID: 8379828
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
 

Author Comment

by:cs00003
ID: 8379853
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 8380080
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
 

Expert Comment

by:CleanupPing
ID: 9058434
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9735166
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question