zurück zur Übersicht

SpringerMain.java

 
/** 
 * SpringerMain.java
 *
 * @author Martin Klöckner, Daniel Höpfl
 */

package SpringerPack;

import SpringerPack.Boards.*;

import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.border.TitledBorder;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.Canvas;
import java.awt.GridBagConstraints;
import java.awt.Point;
import java.awt.Image;
import java.awt.event.*;
import javax.swing.event.*;

/**
 * This is the main class of the Springer-project
 */

public class SpringerMain extends javax.swing.JApplet {

      /**
       * empty constructor
       */
   public SpringerMain() {
   }

      /**
       * Get info on the applet parameters
       */
   public String[][] getParameterInfo() {
      return null;
   }

      /**
       * Get applet information
       */
   public String getAppletInfo() {
      return "OOP-Project, Springer";
   }

      /**
       * Initialize the applet
       *
       * @see SpringerMain#initComponents
       **/
   public void init() {
      try {
         initComponents();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }

   JPanel mOutputPanel = new JPanel();
   IOutput mOutput = null;
   IAnimatedOutput mAnimatedOutput = null;
   int mStartX = 0;
   int mStartY = 0;

   JLabel mBoardSizeInfo = new JLabel("Size of Board:");
   JLabel mBoardSizeLabel = new JLabel();
   JSlider mBoardSizeSlider = new JSlider();

   JLabel mProgressInfo1 = new JLabel("Search Deepth:");
   JProgressBar mProgressDeepth = new JProgressBar();
   JLabel mProgressInfo2 = new JLabel("Search Progress:");
   JProgressBar mProgress = new JProgressBar();
   JButton mStartStopButton = new JButton("Start");
   
   // members for Animation
   private JButton mAnimationForwardButton = new JButton( " > " );
   private JButton mAnimationBackwardButton = new JButton( " < ");
   private JCheckBox mAnimationAutoStep = new JCheckBox( "Auto Stepping" );
   private JSlider mAnimationSpeedSlider = new JSlider();
   private Animate mAnimation = null;
   private Image mFigure;
   private JTextArea mAnimationExplanation  = new JTextArea();
   private JCheckBox mAnimationCheck = new JCheckBox( "Animation Panel" );
   
   
   JComboBox mThemeMenu = null;

   JList mResultList = new JList();

   private int mSize;

   public void initComponents() throws Exception {
         // the following code sets the frame's initial state
      setSize(new java.awt.Dimension(605,600));

      mSize = 5;

         // create Output-Area
      mOutputPanel.setBorder(new TitledBorder("Output"));
      mOutputPanel.setLayout(new FlowLayout());
      mOutputPanel.setSize(new java.awt.Dimension(267,267));

      mOutput = new SimpleBoard();
      
      mOutput.setBoardSize(mSize);
      mOutput.setSize(new java.awt.Dimension(257,257));
      mOutput.setImage(createImage(257,257));
      mOutputPanel.add(mOutput);

         // create Settings-Area
      JPanel settingsPanel = new JPanel();
      settingsPanel.setBorder(new TitledBorder("Settings"));
      settingsPanel.setLayout(new GridLayout(2,1));

      JPanel settingsUpperPanel = new JPanel();
      settingsUpperPanel.setLayout(new GridBagLayout());

      settingsUpperPanel.add(mBoardSizeInfo);
      mBoardSizeLabel.setText(new Integer(mSize).toString());
      settingsUpperPanel.add(mBoardSizeLabel);
      mBoardSizeSlider.setSnapToTicks(true);
      mBoardSizeSlider.setMajorTickSpacing(11);
      mBoardSizeSlider.setMinorTickSpacing(1);
      mBoardSizeSlider.setMinimum(5);
      mBoardSizeSlider.setMaximum(16);
      mBoardSizeSlider.setValue(mSize);
      mBoardSizeSlider.setPaintTicks(true);
      mBoardSizeSlider.setOrientation(JSlider.HORIZONTAL);
      settingsUpperPanel.add(mBoardSizeSlider);
      settingsPanel.add(settingsUpperPanel);

      JPanel settingsMidPanel = new JPanel();
      settingsMidPanel.setLayout(new GridBagLayout());

      JLabel ThemeInfo = new JLabel("Theme:");
      settingsMidPanel.add(ThemeInfo);
      mThemeMenu = new JComboBox();
      mThemeMenu.addItem("Simple Board"); 
      mThemeMenu.addItem("Simple Board (red edition)");
      mThemeMenu.addItem("Sloped Board");
      mThemeMenu.addItem("3D Board"); 
      mThemeMenu.addItem("Simple Animated Board");
      settingsMidPanel.add(mThemeMenu);
      settingsPanel.add(settingsMidPanel);

         // create mAnimation-Area
      
      JPanel animationHiddenPanel1 = new JPanel();
      animationHiddenPanel1.setLayout(new FlowLayout());
      animationHiddenPanel1.add(mStartStopButton);
      
      JPanel animationPanel = new JPanel();
      animationPanel.setBorder( new TitledBorder( "Animation Control" ) );
      animationPanel.setLayout( new GridLayout(4,1) );
      
      JPanel animationControlPanel = new JPanel();
      animationControlPanel.setLayout( new FlowLayout() );
      animationControlPanel.add( mAnimationCheck );
      
      JPanel animationUpperPanel = new JPanel();
      animationUpperPanel.setLayout( new FlowLayout() );

      mAnimationExplanation.setText( "Sorry, this Theme doesn't\n" +
                                    "provide Animation of Results\n" );
      
      // mAnimationExplanation.setLineWrap(true);
      mAnimationExplanation.setEditable(false);
      mAnimationExplanation.setSelectedTextColor(animationControlPanel.getForeground());
      mAnimationExplanation.setSelectionColor(animationControlPanel.getBackground());
      mAnimationExplanation.setForeground(animationControlPanel.getForeground());
      mAnimationExplanation.setBackground(animationControlPanel.getBackground());
      
      animationUpperPanel.add( mAnimationBackwardButton );
      animationUpperPanel.add( mAnimationForwardButton );
      animationUpperPanel.add( mAnimationExplanation );
      
      JPanel animationCenterPanel = new JPanel();
      animationCenterPanel.setLayout( new FlowLayout() );
      
      mAnimationAutoStep = new JCheckBox( "Auto Stepping" );
      mAnimationSpeedSlider = new JSlider();
      mAnimationSpeedSlider.setSnapToTicks(true);
      mAnimationSpeedSlider.setMajorTickSpacing(11);
      mAnimationSpeedSlider.setMinorTickSpacing(1);
      mAnimationSpeedSlider.setMinimum(1);
      mAnimationSpeedSlider.setMaximum(15);
      mAnimationSpeedSlider.setValue(7);
      mAnimationSpeedSlider.setPaintTicks(true);
      mAnimationSpeedSlider.setOrientation(JSlider.HORIZONTAL);
      
      animationCenterPanel.add( mAnimationAutoStep );
      animationCenterPanel.add( mAnimationSpeedSlider );
      
      animationPanel.add( animationHiddenPanel1 );
      animationPanel.add( animationControlPanel );
      animationPanel.add( animationUpperPanel );
      animationPanel.add( animationCenterPanel );

         //  create Progress-Area ( progress and deepth info )
      JPanel progressPanel = new JPanel();
      progressPanel.setBorder(new TitledBorder("Progress"));
      progressPanel.setLayout(new GridLayout(2,1));

      JPanel progressUpperPanel = new JPanel();
      progressUpperPanel.setLayout(new GridLayout(1,2));

      mProgressInfo1.setEnabled(false);
      progressUpperPanel.add(mProgressInfo1);

      mProgressDeepth.setBorderPainted(true);
      mProgressDeepth.setStringPainted(false);
      mProgressDeepth.setEnabled(false);
      mProgressDeepth.setOrientation(JProgressBar.HORIZONTAL);
      progressUpperPanel.add(mProgressDeepth);

      JPanel progressLowerPanel = new JPanel();
      progressLowerPanel.setLayout(new GridLayout(1,2));

      mProgressInfo2.setEnabled(false);
      progressLowerPanel.add(mProgressInfo2);

      mProgress.setBorderPainted(true);
      mProgress.setStringPainted(false);
      mProgress.setEnabled(false);
      mProgress.setOrientation(JProgressBar.HORIZONTAL);
      progressLowerPanel.add(mProgress);

      progressPanel.add(progressUpperPanel);
      progressPanel.add(progressLowerPanel);
         // end of Progress-Area ( progress and deepth info )

         // create Information-Area
      JPanel controlPanel = new JPanel();
      controlPanel.setBorder(new TitledBorder("Information"));
      controlPanel.setLayout(new BorderLayout());

      JTextArea copyrightExplanation = new JTextArea();
      copyrightExplanation.setText(
         "OOP-Project WS 2000/2001 \n" +
         "Authors: Daniel Höpfl, Martin Klöckner\n" +
         "Springer Problem\n" +
         "Developed with vi\n" );
      copyrightExplanation.setLineWrap(true);
      copyrightExplanation.setEditable(false);
      copyrightExplanation.setSelectedTextColor(controlPanel.getForeground());
      copyrightExplanation.setSelectionColor(controlPanel.getBackground());
      copyrightExplanation.setForeground(controlPanel.getForeground());
      copyrightExplanation.setBackground(controlPanel.getBackground());

      controlPanel.add(copyrightExplanation, BorderLayout.CENTER);

         // create Result-Area
      JPanel resultPanel = new JPanel();
      resultPanel.setBorder(new TitledBorder("Result"));
      resultPanel.setLayout(new BorderLayout());
      mResultList.setListData(new Vector());
      JScrollPane scrollpane = new JScrollPane(mResultList);
      resultPanel.add(scrollpane, BorderLayout.CENTER);
      
         // Place areas into root-Panel
      Container root = getContentPane();

      GridBagLayout bag = new GridBagLayout();
      root.setLayout(bag);

      GridBagConstraints constraints = new GridBagConstraints();
   
      JPanel leftRootPanel = new JPanel();
      GridBagLayout leftRootPanelBag = new GridBagLayout();
      leftRootPanel.setLayout(leftRootPanelBag);

      constraints.fill = GridBagConstraints.BOTH;
      constraints.gridx = 0;
      constraints.gridy = 0;
      constraints.gridheight = 69;
      constraints.weighty = 1;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      leftRootPanelBag.setConstraints(mOutputPanel, constraints);
      leftRootPanel.add(mOutputPanel);

      constraints.gridy = 69;
      constraints.gridheight = 10;
      constraints.weighty = 100;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      leftRootPanelBag.setConstraints(controlPanel, constraints);
      leftRootPanel.add(controlPanel);

   
      // configuration of the outputPanel
      constraints.fill = GridBagConstraints.BOTH;
      constraints.gridx = 0;
      constraints.gridy = 0;
      constraints.gridheight = 70;
      constraints.weighty = 31;
      constraints.gridwidth = 1;
      bag.setConstraints(leftRootPanel, constraints);
      root.add(leftRootPanel);

      // configuration of the settingsPanel
      constraints.gridx = 1;
      constraints.gridy = 0;
      constraints.gridheight = 15;
      constraints.weighty = 10;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      bag.setConstraints(settingsPanel, constraints);
      root.add(settingsPanel);

      // configuration of the progressPanel
      constraints.gridx = 1;
      constraints.gridy = 15;
      constraints.weighty = 1;
      constraints.gridheight = 5;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      bag.setConstraints(progressPanel, constraints);
      root.add(progressPanel);

      // configuration of the AnimationControlPanel
      constraints.gridx = 1;
      constraints.gridy = 20;
      constraints.weighty = 20;
      constraints.gridheight = 50;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      bag.setConstraints(animationPanel, constraints);
      root.add(animationPanel);

   
      // configuration of the resultPanel
      constraints.gridx = 0;
      constraints.gridy = 70;
      constraints.weightx = 1;
      constraints.weighty = 100;
      constraints.gridheight = 20;
      constraints.gridwidth = GridBagConstraints.REMAINDER;
      bag.setConstraints(resultPanel, constraints);
      root.add(resultPanel);

      // load SpringerFigure
      mFigure = getImage( getCodeBase(), "Springer.gif" );
      
      if ( mOutput.isAnimated() )
      {
         mAnimatedOutput = (IAnimatedOutput)(mOutput);
         enableAnimationControls();
      } else
      {
         mAnimatedOutput = null;
         disableAnimationControls();
      }
   
         // implementing Event-Handling 

         // set up responding functions
      mStartStopButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            StartStopPressed();
         }});

      mBoardSizeSlider.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent e) {
            BoardSizeChanged();
         }});

      mThemeMenu.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            ThemeChanged();
         }});

      mOutput.addMouseListener(new java.awt.event.MouseAdapter() {
         public void mouseReleased(java.awt.event.MouseEvent e) {
            outputMouseReleased(e);
         }
      });

      mResultList.addListSelectionListener(new ListSelectionListener() {
         public void valueChanged(ListSelectionEvent e) {
            ResultSelected();
         }});
         
      mAnimationSpeedSlider.addChangeListener( new ChangeListener() {
         public void stateChanged( ChangeEvent e ) {
            animationSpeedChanged();
         }} );
         
      mAnimationForwardButton.addMouseListener( new java.awt.event.MouseAdapter() {
         public void mouseReleased( java.awt.event.MouseEvent e ) {
            animationStepForward();
         }} );
         
      mAnimationBackwardButton.addMouseListener( new java.awt.event.MouseAdapter() {
         public void mouseReleased( java.awt.event.MouseEvent e ) {
            animationStepBackward();
         }} );   
      mAnimationCheck.addMouseListener( new java.awt.event.MouseAdapter() {
         public void mouseReleased( java.awt.event.MouseEvent e ) {
            animationStartStop();
         }} );   
      mAnimationAutoStep.addMouseListener( new java.awt.event.MouseAdapter() {
         public void mouseReleased( java.awt.event.MouseEvent e ) {
            animationSetAutoStep();
         }} );   
   }

   /**
    * handles the click on the result list
    */
   public void ResultSelected() {
      String theResult = null;

      theResult = (String) mResultList.getSelectedValue();

      if(theResult != null)
      {
         System.out.println( "the result you've selected is not null!" );
         theResult += " ";

         mBoard = new Board(mSize);

         int i = 0;
         int step = 1;
         while(i < theResult.length()-1)
         {
            int x = (new String("ABCDEFGHIJKLMNOPQRSTUVXYZ")).indexOf(theResult.substring(i, i+1));
            i++;

            String yStr = new String();
            do {
               yStr = yStr + theResult.substring(i, i+1);
               i++;
            } while(!theResult.substring(i, i+1).equals(" "));

               // skip space
            i++;

            int y = mSize - (new Integer(yStr)).intValue();

            mBoard.setXY(x, y, step);
            step++;
         }
         this.getContentPane().validate();
         mOutput.setBoard( mBoard );
         mOutput.repaint();
      }
      if( mAnimation != null )
      {
         mAnimation.setResult( theResult );
         mAnimation.repaint();
      }
   }

   /**
    * handles the click on the board
    */
   public void outputMouseReleased(java.awt.event.MouseEvent e) {
      if(mSpringer == null)
      {
         Point Pos = mOutput.calcStartField(e.getPoint());
         if(Pos != null)
         {
            mStartX = Pos.x;
            mStartY = Pos.y;
            mOutput.setStart(mStartX, mStartY);
            if(mSpringer == null)
            {
               mResultList.setListData(new Vector());
               mBoard = null;
               mOutput.setBoard(null);
            }
         }
      }
   }

   /**
    * handles the click on the theme-combo-box
    */
    public void ThemeChanged() {
      IOutput Themes[] = {
         new SimpleBoard(),
         new SimpleBoardRed(),
         new SlopedBoard(),
         new Virtual3DBoard(),
         new SimpleAnimatedBoard() };

      int index = mThemeMenu.getSelectedIndex();

      if(index >= 0 &&
         index < Themes.length)
      {
         IOutput theOldOutput = mOutput;

         mOutputPanel.add(Themes[index]);

         mOutput = Themes[index];

         mOutput.setBoardSize(mSize);
         mOutput.setSize(new java.awt.Dimension(257,257));
         mOutput.setImage(createImage(257,257));
         mOutput.setStart(mStartX, mStartY);
         mOutput.setBoard(mBoard);

         if(mSpringer != null)
            mSpringer.setIOutput(mOutput);

         mOutputPanel.remove( theOldOutput );

            // reinstall MouseListener
         mOutput.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent e) {
               outputMouseReleased(e);
            }
         });

         this.getContentPane().validate();
         
         if ( Themes[index].isAnimated() )
         {
            System.out.println( "System animates ..." );
            mAnimatedOutput = (IAnimatedOutput)(mOutput);
            enableAnimationControls();
         } else
         {
            System.out.println( "System not animates ... " );
            mAnimatedOutput = null;
            disableAnimationControls();
         }
         
         mOutput.repaint();
      }
   }

   private Springer mSpringer = null;
   private Board mBoard = null;

   /**
    * handles the click on the Start and Stop-button
    */
   public void StartStopPressed() {
      if(mSpringer != null)
      {
         // Stop-Button was pressed
         // stops the thread, i think
         mSpringer.Stop();
      }
      else
      {
         // Start-Button was pressed

         this.mStartStopButton.setText("Stop");

         mBoardSizeInfo.setEnabled(false);
         mBoardSizeLabel.setEnabled(false);
         mBoardSizeSlider.setEnabled(false);

         mProgressInfo1.setEnabled(true);
         mProgressDeepth.setEnabled(true);
         mProgressDeepth.setValue(0);
         mProgressInfo2.setEnabled(true);
         mProgress.setEnabled(true);
         mProgress.setValue(0);

         mResultList.setEnabled(false);

//         mProgress.setValue(mSize * mSize);
//         mProgressDeepth.setValue(mSize);

         mBoard = new Board(mSize);

         mSpringer = new Springer(this, 
                           mSize,
                           mProgress,
                           mProgressDeepth,
                           mOutput,
                           mResultList,
                           mBoard);

         mSpringer.setStart(mStartX, mStartY);
         
         this.getContentPane().validate();
         
         mSpringer.start();
      }
   }

   /** 
    * stops the calculation when after all result are found
    */
   public void ChildFinished() {
      this.mStartStopButton.setText("Start");

      mBoardSizeInfo.setEnabled(true);
      mBoardSizeLabel.setEnabled(true);
      mBoardSizeSlider.setEnabled(true);

      mProgressInfo1.setEnabled(false);
      mProgressDeepth.setEnabled(false);
      mProgressDeepth.setValue(0);
      mProgressInfo2.setEnabled(false);
      mProgress.setEnabled(false);
      mProgress.setValue(0);

      mResultList.setEnabled(true);
      if(mResultList.getFirstVisibleIndex() >= 0)
         mResultList.setSelectedIndex(0);

      mSpringer = null;
   }

   /**
    * handles the click of the Board-Size-slider
    */
   public void BoardSizeChanged() {
      if(mBoardSizeSlider.getValue() != mSize)
      {
         mSize = mBoardSizeSlider.getValue();

         mBoardSizeLabel.setText(new Integer(mSize).toString());
         mBoard = null;
         mOutput.setBoard(null);
         mOutput.setBoardSize(mSize);

         if(mStartX >= mSize)
            mStartX = mSize-1;
         if(mStartY >= mSize)
            mStartY = mSize-1;

         mOutput.setStart(mStartX, mStartY);

         mResultList.setListData(new Vector());
      }
   }
   /**
    * handles the click on the Animation-Speed-slider
    */
   public void animationSpeedChanged()
   {
      if( mAnimation != null )
      {
         mAnimation.setAnimationSpeed( mAnimationSpeedSlider.getValue() );
      }
   }
   
   /**
    * handles the click on the checkbox of Autostepping
    */
   public void animationSetAutoStep()
   {
      if( mAnimation != null )
      {
         if ( mAnimationAutoStep.isSelected() )
            mAnimation.setAutoStep( true );
         else
            mAnimation.setAutoStep( false );
      }
   }
   
   /**
    * handles the click on the animation-forward-button
    */
   public void animationStepForward()
   {
      
      if( mAnimation != null )
      {
         mAnimation.stepForward();
      }
   }
   
   /**
    * handles the click on the animation-backward-button
    */
   public void animationStepBackward()
   {
      if( mAnimation != null )
      {
         mAnimation.stepBackward();
      }
   }

   /**
    * inserts the Animation-Canvas and removes the Output-Canvas
    */
   public void createAnimation( String inResult )
   {
      if( mAnimation == null )
      {
         mAnimation = new Animate( mAnimatedOutput, inResult, mFigure );
         
         IOutput theOldOutput = mOutput;
         if ( mAnimation == null )
         {
            System.out.println( "mAnimation sollte hier aber nicht null sein!" );
         }
         
         // mAnimation.setBoardSize(mSize);
         mAnimation.setSize( new java.awt.Dimension(257,257) );

         mOutputPanel.add( mAnimation );
         mOutputPanel.remove( mOutput );

         this.getContentPane().validate();
         
         mAnimation.setImage( createImage(257,257) );
      }
   }
   
   /**
    * removes the Animation-Canvas and inserts the Output-Canvas 
    */
    public void destroyAnimation()
    {
       if( mAnimation != null )
      {
         mOutputPanel.add( mOutput );
         mOutputPanel.remove( mAnimation );
         mAnimation = null;
         this.getContentPane().validate();
      }
   }   
   
   /**
    * Animation-buttons are enabled
    */
   private void enableAnimationControls()
   {
      // enable all Components in the AnimationPanel
      
      if ( !mOutput.isAnimated() )
      {
         return;
      } else
      {
         System.out.println ( "enabling Animation..." );
         mAnimationExplanation.setVisible( false );
         mAnimationCheck.setVisible( true );
         mAnimationBackwardButton.setVisible( true );
         mAnimationForwardButton.setVisible( true );
         mAnimationAutoStep.setVisible( true );
         mAnimationSpeedSlider.setVisible( true );
         this.getContentPane().validate();
      }
   }
   
   /**
    * Animation-Buttons are disabled
    */
   private void disableAnimationControls()
   {
      // disable all Components in the Animation Panel
      System.out.println( "disabling Animation..." );
      mAnimationCheck.setVisible( false );
      mAnimationBackwardButton.setVisible( false);
      mAnimationForwardButton.setVisible( false );
      mAnimationAutoStep.setVisible( false);
      mAnimationSpeedSlider.setVisible( false);
      mAnimationExplanation.setVisible( true );
      
      this.getContentPane().validate();
      
   }
   
   /**
    * handling the checkbox AnimationPanel
    */
   private void animationStartStop()
   {
      if ( mOutput.isAnimated() )
      {
         if ( mAnimationCheck.isSelected() && mOutput.isAnimated() )
         {
            System.out.println( "Set Animation Panel on!" );
            
            //enableAnimationPanel();
            String theResult = (String)mResultList.getSelectedValue();
            if ( theResult != null && !theResult.equals( "" ) )
               createAnimation( theResult );
         }
         else
         {
            System.out.println( "Set Animation Panel off!" );
            destroyAnimation();
            ThemeChanged();
         }
      } else
      {
         mAnimationCheck.setSelected( false );
      }
         
   }
}

zurück zur Übersicht