projekt  
  einleitung
  applet
  quellcode
über  
  info
  team
quellcode
 
JAVADOC
SOURCECODE
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;

public class MassPanel extends JPanel
{
  // *the textfields of a MASS
  private static JTextField xCoorText= new JTextField("0",6);
  private static JTextField yCoorText= new JTextField("0",6);
  private static JTextField radiusText= new JTextField("0",6);
  private static JTextField massText= new JTextField("0",6);
  // v= sqrt(Mass.xv^2+Mass.yv^2)/deltaT
  private static JTextField vText= new JTextField("0",6);
  // *the additional textfields of the CRAFT
  private static JTextField fText= new JTextField("0",6);
  private static JTextField rfText= new JTextField("0",6);
  private static JTextField yawTText= new JTextField("0",6);
  private static JComboBox massCombo=null;
  private static JPanel westPanel;
  private static JPanel centerPanel;
  private static JPanel eastPanel;
  private static JLabel craftF = new JLabel("F:");
  private static JLabel craftrF = new JLabel("rF:");
  private static JLabel craftyawT = new JLabel("yawT:");
  private static JLabel craftFN = new JLabel("N");
  private static JLabel craftrFN = new JLabel("N");
  private static JLabel craftyawTs = new JLabel("s");
  // *the textfild for orbit radius
  private static JTextField orbitText= new JTextField("100",6);
  public static double r_orbit= 100;

  //what to do next?
  public static int newActionToExecute=-1;
  public static final int MASS= 23;
  public static final int CRAFT= 2323;
  public static final int SELECTION= 232323;
  public static final int ORBIT= 5;

  MassPanel()
  {
    this.setLayout(new BorderLayout());
      JPanel containmentA= new JPanel(new BorderLayout());
        JPanel selectPanel= new JPanel(new BorderLayout());
          JButton directSelButton= new JButton("select");
        selectPanel.add(directSelButton, BorderLayout.WEST);
          massCombo= new JComboBox(Physics.masses);
        selectPanel.add(massCombo, BorderLayout.EAST);
      containmentA.add(selectPanel,BorderLayout.NORTH);
        JPanel orbit2Panel= new JPanel();
        orbit2Panel.setLayout(new BoxLayout(orbit2Panel, BoxLayout.X_AXIS));
          JButton orbitSetButton= new JButton("orbit");
        orbit2Panel.add(orbitSetButton);
        orbit2Panel.add(orbitText);
        orbit2Panel.add(new JLabel("m"));
      containmentA.add(orbit2Panel,BorderLayout.SOUTH);
    this.add(containmentA,BorderLayout.NORTH);
      JPanel containmentB= new JPanel(new BorderLayout());
        JPanel buttonPanel= new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
          JButton newMassButton= new JButton("new Mass");
        buttonPanel.add(newMassButton);
          JButton newCraftButton= new JButton("new Craft");
        buttonPanel.add(newCraftButton);
      containmentB.add(buttonPanel,BorderLayout.NORTH);
        JPanel showPanel= new JPanel(new BorderLayout());
        showPanel.setBorder(BorderFactory.createTitledBorder("Selected Mass"));
          westPanel= new JPanel(new GridLayout(0,1));
          westPanel.add(new JLabel("x:"));
          westPanel.add(new JLabel("y:"));
          westPanel.add(new JLabel("r:"));
          westPanel.add(new JLabel("m:"));
          westPanel.add(new JLabel("v:"));
          westPanel.add(craftF);
          westPanel.add(craftrF);
          westPanel.add(craftyawT);
        showPanel.add(westPanel, BorderLayout.WEST);
          centerPanel= new JPanel(new GridLayout(0,1));
          centerPanel.add(xCoorText);
          centerPanel.add(yCoorText);
          centerPanel.add(radiusText);
          centerPanel.add(massText);
          centerPanel.add(vText);
          centerPanel.add(fText);
          centerPanel.add(rfText);
          centerPanel.add(yawTText);
        showPanel.add(centerPanel, BorderLayout.CENTER);
          eastPanel= new JPanel(new GridLayout(0,1));
          eastPanel.add(new JLabel("m"));
          eastPanel.add(new JLabel("m"));
          eastPanel.add(new JLabel("m"));
          eastPanel.add(new JLabel("kg"));
          eastPanel.add(new JLabel("m/s"));
          eastPanel.add(craftFN);
          eastPanel.add(craftrFN);
          eastPanel.add(craftyawTs);
        showPanel.add(eastPanel, BorderLayout.EAST);
      containmentB.add(showPanel,BorderLayout.SOUTH);
    this.add(containmentB,BorderLayout.SOUTH);

    craftF.setVisible(false);
    craftrF.setVisible(false);
    craftyawT.setVisible(false);
    fText.setVisible(false);
    rfText.setVisible(false);
    yawTText.setVisible(false);
    craftFN.setVisible(false);
    craftrFN.setVisible(false);
    craftyawTs.setVisible(false);

    // add renderer
    massCombo.setRenderer(new DefaultListCellRenderer() {
      public Component getListCellRendererComponent(
          JList list,
          Object value,
          int index,
          boolean isSelected,
          boolean cellHasFocus )
      {
        int massnum=666;
        if(index==-1) massnum=list.getSelectedIndex(); // render display-line:
          else massnum=index; //render drop-down-part
        if(massnum!=-1)
        {
          BufferedImage bi= new BufferedImage(30,10,BufferedImage.TYPE_INT_RGB);
          Graphics2D g2d= bi.createGraphics();
          g2d.setPaint( ((Mass)Physics.masses.get(massnum)).color );
          g2d.fillRect(0,0,30,10);
          Icon icon= new ImageIcon(bi);
          return new JLabel("mass "+massnum, icon, SwingConstants.RIGHT);
        }
        else { return new JLabel("none"); }
      }
    } );
    // add events
    massCombo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        JComboBox c= ((JComboBox)e.getSource());
        c.validate();
        c.repaint();
        int selected= c.getSelectedIndex();
        if(selected>-1)
        {
          Mass mass= (Mass)(Physics.masses.get(selected));
          xCoorText.setText(Main.double2str(mass.x));
          yCoorText.setText(Main.double2str(mass.y));
          radiusText.setText(Main.double2str(mass.r));
          massText.setText(Main.double2str(mass.m));
          double v= Math.sqrt( Math.pow(mass.xv,(double)2)
            +Math.pow(mass.yv,(double)2) ) / Physics.deltaT;
          // TODO: FIX NaN
          vText.setText(Main.double2str(v));
          if(mass instanceof Craft)
          {
            /*if(westPanel.getComponentCount()<6)
            {
            System.out.println("this is a craft -> add textfields...");
              westPanel.add(new JLabel("F:"));
              centerPanel.add(fText);
              eastPanel.add(new JLabel("N"));
              westPanel.add(new JLabel("rF:"));
              centerPanel.add(rfText);
              eastPanel.add(new JLabel("N"));
              westPanel.add(new JLabel("yawT"));
              centerPanel.add(yawTText);
              eastPanel.add(new JLabel("s"));
              westPanel.invalidate();
            } */
            //System.out.println("this is a craft -> add textfields...");

            craftF.setVisible(true);
            craftrF.setVisible(true);
            craftyawT.setVisible(true);
            fText.setVisible(true);
            rfText.setVisible(true);
            yawTText.setVisible(true);
            craftFN.setVisible(true);
            craftrFN.setVisible(true);
            craftyawTs.setVisible(true);
            fText.setText(Main.double2str(((Craft)mass).f));
            rfText.setText(Main.double2str(((Craft)mass).rf));
            yawTText.setText(Main.double2str(((Craft)mass).yawT));
          }
          else //if(westPanel.getComponentCount()>5)
          {
           /* for(int i=8; i>5; i--)
            {
              westPanel.remove(i);
              centerPanel.remove(i);
              eastPanel.remove(i);
              westPanel.invalidate();
            }*/
            craftF.setVisible(false);
            craftrF.setVisible(false);
            craftyawT.setVisible(false);
            fText.setVisible(false);
            rfText.setVisible(false);
            yawTText.setVisible(false);
            craftFN.setVisible(false);
            craftrFN.setVisible(false);
            craftyawTs.setVisible(false);
          }
          Screen.screen.update();
        }
      } });
    massCombo.setEnabled(true);
//    massCombo.setPreferredSize(new Dimension(100,10));
    directSelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        newActionToExecute= SELECTION;
        Main.main.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
      } });
    orbitSetButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        newActionToExecute= ORBIT;
        Main.main.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
      } });
    newMassButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        newActionToExecute= MASS;
        Main.main.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
      } });
    newCraftButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        newActionToExecute= CRAFT;
        Main.main.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
      } });

    orbitText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        r_orbit= Main.str2double(orbitText.getText());
        orbitText.setText(Main.double2str(r_orbit));
      } });

    xCoorText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Mass mass= (Mass)Physics.masses.get(active);
          double x= Main.str2double(xCoorText.getText());
          xCoorText.setText(Main.double2str(x));
          mass.x= x;
          Screen.update();
        }
      } });
    yCoorText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Mass mass= (Mass)Physics.masses.get(active);
          double y= Main.str2double(yCoorText.getText());
          yCoorText.setText(Main.double2str(y));
          mass.y= y;
          Screen.update();
        }
      } });
    radiusText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Mass mass= (Mass)Physics.masses.get(active);
          double r= Main.str2double(radiusText.getText());
          radiusText.setText(Main.double2str(r));
          mass.r= r;
          Screen.update();
        }
      } });
    massText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        System.out.println("WAS GEHT AB?");
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Mass mass= (Mass)Physics.masses.get(active);
          double m= Main.str2double(massText.getText());
          massText.setText(Main.double2str(m));
          mass.m= m;
          if(mass instanceof Craft) ((Craft)mass).update(Physics.deltaT);
        }
      } });
  // v= sqrt(Mass.xv^2+Mass.yv^2)/deltaT
//  private static JTextField vText= new JTextField("0",6);
    vText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Mass mass= (Mass)Physics.masses.get(active);
          double v= Main.str2double(vText.getText());
          // alpha= arctan(yv/xv)
          double alpha= Math.atan(mass.yv/mass.xv);
          // xvnew= cos(alpha)*v*deltaT; yvnew= sin(alpha)*v*deltaT
          mass.xv= Math.cos(alpha)*v*Physics.deltaT;
          mass.yv= Math.sin(alpha)*v*Physics.deltaT;
          Screen.update();
        }
      } });
    fText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Craft craft= (Craft)Physics.masses.get(active);
          double f= Main.str2double(fText.getText());
          fText.setText(Main.double2str(f));
          craft.f= f;
          craft.update(Physics.deltaT);
        }
      } });
    rfText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Craft craft= (Craft)Physics.masses.get(active);
          double rf= Main.str2double(rfText.getText());
          rfText.setText(Main.double2str(rf));
          craft.rf= rf;
          craft.update(Physics.deltaT);
        }
      } });
    yawTText.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        int active=MassPanel.getActiveMass();
        if(active!=-1)
        {
          Craft craft= (Craft)Physics.masses.get(active);
          double yawT= Main.str2double(yawTText.getText());
          yawTText.setText(Main.double2str(yawT));
          craft.yawT= yawT;
          craft.update(Physics.deltaT);
        }
      } });
  }
  /**
   * update all things of this panel to show given mass (0..)
   */
  public static void update(int number)
  {
    if(number==-1) massCombo.removeAllItems();
    massCombo.setSelectedIndex(number);
    ControlPanel.update();
  }

  /**
   * get the number of the active mass (0..)
   */
  public static int getActiveMass()
  {
    if(massCombo==null) return -1;
    return massCombo.getSelectedIndex();
  }

}