Package swarm.viewer

Source Code of swarm.viewer.AddPanel

package swarm.viewer;



import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import swarm.sim.Environment;
import swarm.sim.Resource;
import swarm.sim.Simulation;

import bees.Bee;
import insects.Ant;
import insects.AntHill;
import insects.AntQueen;
import insects.Beetle;
import insects.DeadInsect;
import insects.Wasp;
import cannibals.Cannibal;
import boids.Boid;


public class AddPanel implements ActionListener
{
  private JFrame window;
  private LayoutManager layout;
 
  private JLabel title;
  private JTextField numInsects;
  private JButton AntButton;
  private JButton AntQueenButton;
  private JButton WaspButton;
  private JButton BeetleButton;
  private JButton CannibalButton;
  private JButton BoidButton;
  private JButton BeeButton;
  private JButton ResourceButton;
  private JButton DeadInsectButton;
 
 
  //static Environment terrain;
  static Simulation sim;
 
  public AddPanel(Simulation sim)
  {
   
    this.sim=sim;
   
    window = new JFrame();
    window.setSize(150,550);
    window.setResizable(false);
    //window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    layout = new FlowLayout();
    ((FlowLayout)layout).setHgap(200);
    ((FlowLayout)layout).setVgap(20);
    window.setLayout(layout);
   
    title = new JLabel("Add this many Insects");
    window.add(title);
   
    numInsects = new JTextField("1                                  ");
    window.add(numInsects);
   
   
    AntButton = new JButton("AntHill");
    AntButton.addActionListener(this);
    window.add(AntButton);
   
    AntQueenButton = new JButton("Ant Queen");
    AntQueenButton.addActionListener(this);
    window.add(AntQueenButton);
   
    WaspButton = new JButton("Wasp");
    WaspButton.addActionListener(this);
    window.add(WaspButton);
   
    BeetleButton = new JButton("Beetle");
    BeetleButton.addActionListener(this);
    window.add(BeetleButton);
   
    CannibalButton = new JButton("Cannibal");
    CannibalButton.addActionListener(this);
    window.add(CannibalButton);
   
    BoidButton = new JButton("Boid");
    BoidButton.addActionListener(this);
    window.add(BoidButton);
   
    BeeButton = new JButton("Bee");
    BeeButton.addActionListener(this);
    window.add(BeeButton);
   
    ResourceButton = new JButton("Resource");
    ResourceButton.addActionListener(this);
    window.add(ResourceButton);
   
    DeadInsectButton = new JButton("Dead Insect");
    DeadInsectButton.addActionListener(this);
    window.add(DeadInsectButton);
   
    window.setVisible(true);
   

   
  }
 
  public void actionPerformed(ActionEvent e)
  {
    try
    {
      double num = Double.parseDouble(numInsects.getText());
      title.setText("Add this many Insects");
     
      if(e.getSource() == AntButton)
      {
        for(int i=0;i<num;i++)
        sim.add(new AntHill(Math.random()*3200,
             Math.random()*1800));
       
      }
      else if(e.getSource() == WaspButton)
      {
        double x = Math.random()*3200;
        double y = Math.random()*1800;
        for(int i=0;i<num;i++)
        sim.add(new Wasp(x,y));
      }
      else if(e.getSource() == BeetleButton)
      {
        for(int i=0;i<num;i++)
        sim.add(new Beetle(Math.random()*3200,
             Math.random()*1800));
      }
      else if(e.getSource() == CannibalButton)
      {
        for(int i=0;i<num;i++)
        sim.add(new Cannibal(Math.random()*3200,
             Math.random()*1800));
      }
      else if(e.getSource() == BoidButton)
      {
        double x = Math.random()*3200;
        double y = Math.random()*1800;
        for(int i=0;i<num;i++)
        sim.add(new Boid(x,y));
      }
      else if(e.getSource() == BeeButton)
      {
        for(int i=0;i<num;i++)
        sim.add(new Bee(Math.random()*3200,
             Math.random()*1800));
      }
      else if(e.getSource() == ResourceButton)
      {
        for(int i=0;i<num;i++)
        sim.add(new Resource(Math.random()*3200,
             Math.random()*1800, 20, 5));
      }
      else if(e.getSource() == DeadInsectButton)
      {
       
        for(int i=0;i<num;i++)
        sim.add(new DeadInsect(Math.random()*3200,
             Math.random()*1800, 5, 5));
      }
      else if(e.getSource() == AntQueenButton)
      {
        for(int i=0;i<num;i++)
          sim.add(new AntQueen(Math.random()*3200,
               Math.random()*1800));
      }
    }
   
    catch(NumberFormatException ex)
    {
      title.setText("result: invalid entry");
    }
  }
 
  public static void main(String[] args)
  {
    new AddPanel(sim);
  }

}
TOP

Related Classes of swarm.viewer.AddPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.