Retour à la page de téléchargement Retour à la page de téléchargement


//	Control panel for Artificial Termites.
//
//	Author	Matthew Caryl
//	Created	29.10.96
//
//  Although under copywrite to the author (Matthew Caryl) this code can be copied and modified for non-commercial
//  purposes as long as any derivatives contain this condition.
//
// Last modification: 2000-04-05 adding wheat and cheese Xavier Outhier
// Last modification: 2000-04-06 adding forbidden Xavier Outhier

package ArtificialTermites;

import java.awt.*;

final class ControlFrame extends Frame {
    World   owningWorld;
    WorldApplet   worldApplet;
    Button  ok;
    Button  cancel;
    Choice  wood;
    Choice  wheat;
    Choice  cheese;
    Choice  termite;
    Choice  width;
    Choice  height;

	int woodPercentage;
	int wheatPercentage;
	int cheesePercentage;
	int termitePercentage;
	int worldWidth;
	int worldHeight;                
    
    public ControlFrame (World world, WorldApplet worldGUI) {
        super("Termite Control");
        
        owningWorld = world;
        worldApplet = worldGUI;
        
        // Add an OK button to the base of the frame
        cancel = new Button("Cancel");
        ok = new Button("OK");
        Panel okPanel = new Panel();
        okPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
        okPanel.add(cancel);
        okPanel.add(ok);
        this.add("South", okPanel);
        
        // Add an signature to the top of the frame
        Panel sigPanel = new Panel();
        sigPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
        sigPanel.add(new Label("Matthew Caryl 1996, Xavier Outhier & http://www.multimania.com/d2set/ 2000"));
        this.add("North", sigPanel);
        
        // Add option menu for wood percentages starting with current value
        wood = new Choice();
        wood.addItem(Integer.toString(owningWorld.woodPercentage()));
        wood.addItem("---");
        for (int i = 5; i < 50; i += 5)
            wood.addItem(Integer.toString(i));
        
        // Add option menu for wheat percentages starting with current value
        wheat = new Choice();
        wheat.addItem(Integer.toString(owningWorld.wheatPercentage()));
        wheat.addItem("---");
        for (int i = 5; i < 50; i += 5)
            wheat.addItem(Integer.toString(i));
        
        // Add option menu for cheese percentages starting with current value
        cheese = new Choice();
        cheese.addItem(Integer.toString(owningWorld.cheesePercentage()));
        cheese.addItem("---");
        for (int i = 5; i < 50; i += 5)
            cheese.addItem(Integer.toString(i));
        
        // Add option menu for termite percentages starting with current value
        termite = new Choice();
        termite.addItem(Integer.toString(owningWorld.termitePercentage()));
        termite.addItem("---");
        for (int i = 1; i < 10; i++)
            termite.addItem(Integer.toString(i));
        
        // Add option menu for world width starting with current value
        width = new Choice();
        width.addItem(Integer.toString(owningWorld.width()));
        width.addItem("---");
        for (int i = 1; i < 1024; i *= 2)
            width.addItem(Integer.toString(i));
        
        // Add option menu for world height starting with current value
        height = new Choice();
        height.addItem(Integer.toString(owningWorld.height()));
        height.addItem("---");
        for (int i = 1; i < 1024; i *= 2)
            height.addItem(Integer.toString(i));
        
        // Add a grid to contain all the controls
        Panel controlPanel = new Panel();
        controlPanel.setLayout(new GridLayout(5, 2, 0, 5)); // 5 rows, 2 columns, 0 horizontal, 5 verticle
        controlPanel.add(new Label("Wood percentage:"));    controlPanel.add(wood);
        controlPanel.add(new Label("Wheat percentage:"));    controlPanel.add(wheat);
        controlPanel.add(new Label("Cheese percentage:"));    controlPanel.add(cheese);
        controlPanel.add(new Label("Termite percentage:")); controlPanel.add(termite);
        controlPanel.add(new Label("World width:"));        controlPanel.add(width);
        controlPanel.add(new Label("World height:"));       controlPanel.add(height);
        Panel alignPanel = new Panel();
        alignPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        alignPanel.add("Center", controlPanel);
        this.add("Center", alignPanel);
        
        // Resize the window and display
        this.pack();
        this.show();
        }
    
    public boolean action(Event e, Object arg) {
        if (e.target == ok || e.target == cancel) {
            // capture new control values for ok
            if (e.target == ok) {
                try {
                    woodPercentage = Integer.parseInt(wood.getSelectedItem());
                    }
                catch (NumberFormatException e2) {
                    woodPercentage = owningWorld.woodPercentage();
                    }
                try {
                    wheatPercentage = Integer.parseInt(wheat.getSelectedItem());
                    }
                catch (NumberFormatException e2) {
                    wheatPercentage = owningWorld.wheatPercentage();
                    }
                try {
                    cheesePercentage = Integer.parseInt(cheese.getSelectedItem());
                    }
                catch (NumberFormatException e2) {
                    cheesePercentage = owningWorld.cheesePercentage();
                    }
                try {
                    termitePercentage = Integer.parseInt(termite.getSelectedItem());
                    }
                catch (NumberFormatException e2) {
                    termitePercentage = owningWorld.termitePercentage();
                    }
                try {
                    worldWidth = Integer.parseInt(width.getSelectedItem());
                    }
                catch (NumberFormatException e2) {
                    worldWidth = owningWorld.width();
                    }
                try {
                    worldHeight = Integer.parseInt(height.getSelectedItem());
                    }
                catch (NumberFormatException e2) {
                    worldHeight = owningWorld.height();
                    }
/*
				owningWorld.initialiseWorld(worldWidth, worldHeight, 
				                            woodPercentage, wheatPercentage, cheesePercentage,
										    termitePercentage, owningWorld.getWorldApplet());

*/
                }
            // restart world and dispose of controls

            worldApplet.start(worldWidth, worldHeight, 
                              woodPercentage, wheatPercentage, cheesePercentage,
							  termitePercentage);
//            worldApplet.start();
            this.hide();
            this.dispose();
            return true;
            }
        else
            return false;
        }
    }

Retour à la page de téléchargement Retour à la page de téléchargement