Link to home
Start Free TrialLog in
Avatar of susanbirnbaum
susanbirnbaum

asked on

I need everything in this applet.....

Hello Experts,

[I'm sorry a similar question has been posted before like this one from one of my colleagues but I'm getting desperate to sort this problem out quickly - Sorry again!]

Can you please help?  I have 2 programs that I need you to check.  What I need this applet to do is to have a scrolling text above and underneath some textfields, checkboxes, lists, etc.  

Both programs have each compiled successfully.
 
Now there is some difficulty as to how to place the scrolling text applet program in the BoxDemo applet program so that I have an applet that has scrolling text at the top and checkboxes, lists, etc. at the bottom.

Please, please help us.  You are our only hope right now!  

[Here are the 2 programs].  

(1)The BoxDemo Program:-

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class BoxDemo extends Applet implements ItemListener,ActionListener
{

//checkbox
String msg=" ";
Checkbox Win98, winNT, solaris,mac;

//checkboxgroup
Checkbox one,two,three,four;
CheckboxGroup cbg;

//choicelist
Choice fruit;

//list
List vegetables;

//TextField
TextField name,pass;


public void init()
{

//checkbox
Win98=new Checkbox("Win98");

winNT=new Checkbox("winNT");

solaris=new Checkbox("solaris");

mac=new Checkbox("mac");

//checkboxgroup
cbg=new CheckboxGroup();
one=new Checkbox("one", cbg,true);

two=new Checkbox("two", cbg,false);
three=new Checkbox("three", cbg,false);
four=new Checkbox("four", cbg,false);

//choicelist
fruit=new Choice();
fruit.add("apple");
fruit.add("banana");
fruit.add("grapes");
fruit.add("melon");

//list
vegetables=new List(4,true);
vegetables.add("onion");
vegetables.add("potatoes");
vegetables.add("marrow");
vegetables.add("carrot");

//textfield
Label namep=new Label("Name: ", Label.RIGHT);
Label passp=new Label("Password: ", Label.RIGHT);
name=new TextField(12);
pass=new TextField(8);
pass.setEchoChar('*');

//checkbox
add(Win98);
add(winNT);
add(solaris);
add(mac);

//checkboxgroup
add(one);
add(two);
add(three);
add(four);

//choicelist
add(fruit);

//list
add(vegetables);

//textfield
add(namep);
add(name);
add(passp);
add(pass);

//checkbox
Win98.addItemListener(this);
winNT.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);

//checkboxgroup
one.addItemListener(this);
two.addItemListener(this);
three.addItemListener(this);
four.addItemListener(this);

//choicelist
fruit.addItemListener(this);

//list
vegetables.addActionListener(this);

//textfield
name.addActionListener(this);
pass.addActionListener(this);

}

public void itemStateChanged(ItemEvent ie)
{
repaint();
}

public void actionPerformed(ActionEvent ae)
{
repaint();
}

public void paint(Graphics g)
{

//checkbox
msg="Current Status: ";
g.drawString(msg,100,200);
msg=" Win98: " +Win98.getState();
g.drawString(msg,6,100);
msg=" winNT: " +winNT.getState();
g.drawString(msg,6,120);
msg=" Solaris: "+solaris.getState();
g.drawString(msg,6,140);
msg=" mac: "+mac.getState();
g.drawString(msg,6,160);

//checkboxgroup
msg="Number selection: ";
msg+=cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,0,180);

//choicelist
msg="Fruit Selection: ";
msg+=fruit.getSelectedItem();
g.drawString(msg,40,200);

//list
int idx[];
msg="Vegetable selection: ";
idx=vegetables.getSelectedIndexes();
for(int i=0; i<idx.length;i++)
msg+=vegetables.getItem(idx[i])+" ";
g.drawString(msg,60,220);

//textfield
g.drawString("Name: "+ name.getText(),80,240);
g.drawString("Selected text in name: " + name.getSelectedText(),70,260);
g.drawString("Password: "+ pass.getText(),80,280);
}
}


(2) The Scroll Text Program:
import java.awt.*;
import java.applet.*;

public class Welcome extends Applet implements Runnable {
 
 Font f = new Font("Arial Black", Font.BOLD, 24);
 Color col = new Color(204,153,0);
 Color back = new Color(249,249,249);
 String wel = new String("WELCOME");
 Thread runner;
 int x;
 int stringSize;
 
 public void init() {
    setForeground(col);
    setBackground(back);
     setFont(f);
     FontMetrics fm=getFontMetrics(f);
     stringSize=fm.stringWidth(wel);
 }

 public void start() {
    if (runner == null) {
       runner = new Thread(this);
       runner.start();
    }
 }

 public void stop() {
    if (runner != null) {
       runner = null;
    }
 }

 public void run() {
    Thread thisThread = Thread.currentThread();
    x = 10-stringSize;
    int cont = getSize().width;
    while (runner == thisThread) {
       x = x + 2;
       repaint();
       try {
          Thread.sleep(20);
       } catch (InterruptedException e) { }
       if (x > cont) x = -stringSize;
    }
 }

 public void paint(Graphics g) {
   
    g.drawString(wel, x, 25);
 }
}  


Avatar of rrz
rrz
Flag of United States of America image

Why not use one applet with a layout manager.
You could use "GridLayout". Add your programs to two panels
and then add the panels to the layout.
I support that. Also I would not use drawString to display the 'win98: true' kind of statements. Instead use labels, and use labelName.setText() in the itemStateChanged methods to update those displays rather than using repaint technique. There's too much flickering otherwise.
Avatar of susanbirnbaum
susanbirnbaum

ASKER

Thanks rrk and ahosang for the advice,

At the moment I do not know how the GridLayout works.  I'd be grateful if you could give me an idea of how to do this in respect of the 2 programs above.

Sorry for the inconvenience.
Susan
I apologize. I meant thank you for the advice rrz (not rrk) and ahosang.
There are a number of layout managers.You have to decide
how you want to layout your applet. Maybe the one you want
is the "BorderLayout". You could put your scroll text in
the North section, and put the rest in the center or South
section. Leave the West and East sections empty.
The easiest way  would be to use the default layout manager
("FlowLayout"). Just make your scoller almost the width of
the applet. Try something like this.
FlowLayout lm = new FlowLayout(FlowLayout.LEFT);
public void init(){ setLayout(lm); add(scroller);
add(a);add(b); and so on.
in the run method:
ASKER CERTIFIED SOLUTION
Avatar of ahosang
ahosang
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange