import java.awt.*;
import java.applet.*;
import more;
import box;

class window extends Frame
{
    box msgBox;
    Button button;
    Image Picture, SexPic2, StatusPic2, NewPic2, NewPic4;
    String Name2, Sex2, Age2, Mom2, Dad2, PicName2, PicCom2, Status2, Comment2, MoreComment1, NewName2, NewCom2, NewName4, NewCom4, Pregnant2, Developed2;
    MediaTracker Tracker;
    int test=0;
    Component parent;
    static int x=1;
    String WindowTitle = "More pictures of ";
    String Title2, Author2, Update2, Boxtitle2;

    public window(String Title, String Author, String Update, String Boxtitle, Component par, String Name, String Sex, String Age, String Mom, String Dad, Image Pic, String PicName, String PicCom, String Status, String Comment, Image StatusPic, String NewName, Image NewPic, String NewCom, String NewName3, Image NewPic3, String NewCom3, Image SexPic, String Pregnant, String Developed)
    {
        Title2 = Title;
        Author2 = Author;
        Update2 = Update;
        Boxtitle2 = Boxtitle;
        parent = par;
        Name2 = Name;
        Sex2 = Sex;
        Age2 = Age;
        Mom2 = Mom;
        Dad2 = Dad;
        PicName2 = PicName;
        PicCom2 = PicCom;
        Status2 = Status;
        Comment2 = Comment;
        MoreComment1 = Comment;
        NewName2 = NewName;
        NewPic2 = NewPic;
        NewCom2 = NewCom;
        NewName4 = NewName3;
        NewPic4 = NewPic3;
        NewCom4 = NewCom3;
        SexPic2 = SexPic;
        Picture = Pic;
        Developed2 = Developed;
        Pregnant2 = Pregnant;

        if (Status2.equals("deceased") || Status2.equals("unborn")) StatusPic2 = StatusPic;

        Tracker = new MediaTracker(this);
        Tracker.addImage(Picture, 0);
        Tracker.addImage(SexPic, 1);
        if (Status2.equals("deceased")) Tracker.addImage(StatusPic2, 2);
        try
        {
            Tracker.waitForAll();
        }
        catch(InterruptedException e)
        {
            return;
        }
    }

    public boolean action(Event evt, Object arg)
    {
        if(arg.equals("OK"))
        {
            hide();
            dispose();
            return true;
        }

        if(arg.equals("More Pics"))
        {
            WindowTitle = WindowTitle + Name2;
            more win = new more(this, NewName2, NewPic2, NewCom2, NewName4, NewPic4, NewCom4);
            win.setBackground(Color.white);
            win.setTitle(WindowTitle);
            win.resize(400,320);
            win.move(50,50);
            win.show();
        }

        if(arg.equals("About"))     //MessageBox
        {
            msgBox = new box(this, Title2, Author2, Update2);
            msgBox.setBackground(Color.white);
            msgBox.setTitle(Boxtitle2);
            msgBox.resize(300,200);
            msgBox.move(200,200);
            msgBox.show();
        }

        return false;
    }

    public void paint(Graphics g)
    {
        g.drawImage(Picture, 200, 0, this);
        g.drawImage(SexPic2, 62, 0, this);
        if (Status2.equals("deceased") || Status2.equals("unborn")) g.drawImage(StatusPic2, 85, 0, this);

        Panel p1 = new Panel();
        p1.setLayout(new GridLayout(10,2));

        p1.add(new Label("Name: "));
        p1.add(new Label(Name2));

        p1.add(new Label("Status: "));
        p1.add(new Label(Status2));

        p1.add(new Label("Age: "));
        p1.add(new Label(Age2));

        p1.add(new Label("Sex: "));
        p1.add(new Label(Sex2));

        p1.add(new Label("Fully Devel.:"));
        p1.add(new Label(Developed2));

        p1.add(new Label("Pregnant:"));
        p1.add(new Label(Pregnant2));

        p1.add(new Label("Mother: "));
        p1.add(new Label(Mom2));

        p1.add(new Label("Father: "));
        p1.add(new Label(Dad2));

        Panel p = new Panel();
        p.setLayout(new GridLayout(3,1));
        p.add(new Button("About"));
        p.add(new Button("OK"));
        if (NewName2 == null && NewName4 == null);
        else
        {
            p.add(new Button("More Pics"));
        }


        setLayout(null);  //By setting the Layout to null, it is possible to specify the
                          //coordinates of a panel (--> reshape).
        add(p);
        add(p1);
        p.reshape(320,295,70,75);
        p1.reshape(5,60,190,200);
        int partCounter=0;
        int partCounter2=0;
        String[] part, part2;
        String endText ="";
        String endText2 ="";

        if (test==0)
        {
            if(PicCom2 == null);
            else        //Here I am cutting the commentary strings to the length of the
                        //TextArea. To do this I had to get the char at the TextArea-length-
                        //position in the String and to test whether it is a space or not. If
                        //it is a space I can cut at this position. If not, I am looking at the
                        //position right left to it.

            {
                int ComLeng = PicCom2.length();  //Length of commentary string.
                String akut = PicCom2;
                if (ComLeng > 30)
                {
                    int parts = (ComLeng / 30);
                    part = new String[parts];    //Parts in which the string has got to be cut.
                    for (int ph = 0; ph < parts; ph++)
                    {
                        for (int pp = 29; pp > -1;pp--)
                        {
                            if (akut.length()>29)
                            {
                                char te[] = new char[1];    //Getting of the char and transforming it
                                                            //into a string.
                                te[0] = akut.charAt(pp);
                                String pos_str = new String(te);
                                if( pos_str.equals(" "))
                                {
                                    part[ph] = akut.substring(0 , pp);
                                    String temp = akut.substring(pp+1); //Cutting the String.
                                    akut = temp;
                                    pp=-1;
                                    partCounter++;
                                }
                            }
                        }
                    }

                    TextArea PictureCom = new TextArea(30, 5);
                    for (int c=0; c < partCounter; c++)
                    {
                        endText = endText + part[c] + '\n';
                    }
                    PictureCom.setText(endText + akut);
                    PictureCom.setEditable(false);
                    add(PictureCom);
                    PictureCom.reshape(200,165,200,100);


                }
                else
                {
                    TextField PictureCom = new TextField(30);
                    PictureCom.setText(PicCom2);
                    PictureCom.setEditable(false);
                    add(PictureCom);
                    PictureCom.reshape(200,165,195,20);
                }
        }test=124;   //This If-clause was necessary because the Applet loaded the window and
                     //especially the TextArea more often than once.

        int ComLeng2 = Comment2.length();
        String akut2 = Comment2;
        if (ComLeng2 > 55)
        {
            int parts2 = (ComLeng2 / 55);
            part2 = new String[parts2];
            for (int ph2 = 0; ph2 < parts2; ph2++)
            {
                for (int pp2 = 54; pp2 > -1;pp2--)
                {
                    if (akut2.length()>54)
                    {
                        char te2[] = new char[1];
                        te2[0] = akut2.charAt(pp2);
                        String pos_str2 = new String(te2);
                        if( pos_str2.equals(" "))
                        {
                            part2[ph2] = akut2.substring(0 , pp2);
                            String temp2 = akut2.substring(pp2+1);
                            akut2 = temp2;
                            pp2=-1;
                            partCounter2++;
                        }
                    }
                }
            }
            TextArea Com = new TextArea(60, 5);
            for (int c2=0; c2 < partCounter2; c2++)
            {
                endText2 = endText2 + part2[c2] + '\n';
            }
            Com.setText(endText2 + akut2);
            Com.setEditable(false);
            add(Com);
            Com.reshape(5,267,300,130);
        }
        else
        {
            TextArea Com = new TextArea(60, 5);
            Com.setText(Comment2);
            Com.setEditable(false);
            add(Com);
            Com.reshape(5,267,300,130);
        }
        }
     show();
    }

    public void show()
    {
        super.show();
        x=2;                //I've used the x-clause (look at crea.java) because the Applet
                            //opened the window twice. This clause also disables the mouseDown
                            //method of crea.java.
    }

    public void hide()
    {
        super.hide();
        x=1;
    }

    public boolean handleEvent(Event e)
    {
        if (e.id == Event.WINDOW_DESTROY
        || (e.id == Event.KEY_PRESS && e.key == 27))
        {
            hide();
            dispose();
            return true;
        }
        return super.handleEvent(e);
    }

}
