home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / apps / chess / PGNViewFrame.java < prev    next >
Encoding:
Java Source  |  1997-11-13  |  10.3 KB  |  404 lines

  1. //******************************************************************************
  2. // PGNViewFrame.java:    
  3. //
  4. //******************************************************************************
  5. import java.awt.*;
  6. import com.ms.dxmedia.*;
  7. import GameFrame;
  8. import java.io.*;
  9. import ChessModel;
  10. import java.util.Vector;
  11. import Game;
  12.  
  13.  
  14. public class PGNViewFrame extends Frame 
  15. {
  16.   // AWT components for the GUI
  17.   protected Button m_nextButton = new Button("Next Move");
  18.   protected Button m_prevButton = new Button("Prev Move");
  19.   protected    MenuItem m_fileOpen = new MenuItem("Open...");
  20.   protected Label m_moveLabel = new Label("Move 0 of 0");
  21.   protected Checkbox m_auto = new Checkbox("AutoPlay");
  22.  
  23.   public CheckboxMenuItem m_fileGame = new CheckboxMenuItem("Show Games");
  24.   protected GameFrame m_gameFrame = new GameFrame(this);
  25.  
  26.  
  27.   AutoPlayer m_autoThread = new AutoPlayer(this);
  28.   // DXM stuff
  29.   protected ChessCanvas m_canvas = null;
  30.   protected ChessModel m_model = null;
  31.  
  32.   // the current game
  33.   protected Game m_currentGame = null;
  34.  
  35.   // the index of which move we are on in tha game
  36.   protected int m_curMoveIdx = 0;
  37.  
  38.  
  39.   
  40.   public static void main(String args[])
  41.   {
  42.     // create an instance of the frame
  43.     PGNViewFrame frame = new PGNViewFrame();
  44.     frame.show();
  45.     frame.hide();
  46.     frame.resize(frame.insets().left + frame.insets().right  + 300,
  47.          frame.insets().top  + frame.insets().bottom + 340);
  48.     frame.show();
  49.     
  50.     // create and start the DXM animation
  51.     frame.startme();
  52.  
  53.     if (args.length > 0)
  54.       frame.OpenFile(args[0]);
  55.   }
  56.  
  57.  
  58.   public PGNViewFrame()
  59.   {
  60.     super ("Chess PGN Viewer");
  61.     System.err.println("ARG");
  62.     DisableButtons();
  63.     System.err.println("ARG");
  64.   }
  65.  
  66.   public void startme() {
  67.  
  68.     System.err.println("ARK");
  69.     setBackground(Color.lightGray);
  70.     setForeground(Color.black);
  71.  
  72.     //setup the frame's menubar
  73.  
  74.     MenuBar bar = new MenuBar();
  75.  
  76.     Menu fileMenu = new Menu("File");        
  77.     
  78.     System.err.println("ARK");
  79.     fileMenu.add(m_fileOpen);
  80.     fileMenu.add(new MenuItem("-"));
  81.  
  82.     System.err.println("ARK");
  83.     fileMenu.add(m_fileGame);
  84.     //m_fileGame.setState(false);
  85.     System.err.println("ARK");
  86.  
  87.     bar.add(fileMenu);
  88.     setMenuBar(bar);
  89.         
  90.         
  91.     //setup the move buttons
  92.     GridBagLayout layout = new GridBagLayout();
  93.     setLayout(layout);
  94.     GridBagConstraints gbc = new GridBagConstraints();
  95.     System.err.println("ARK");
  96.     gbc.weightx = 0;
  97.     gbc.fill = GridBagConstraints.HORIZONTAL;
  98.     gbc.ipadx = 10;
  99.     layout.setConstraints(m_prevButton, gbc);
  100.     add(m_prevButton);
  101.     System.err.println("ARK");
  102.  
  103.     layout.setConstraints(m_nextButton, gbc);
  104.     add(m_nextButton);
  105.     System.err.println("ARK");
  106.     // add an autoPlay checkbox
  107.     gbc.fill = GridBagConstraints.NONE;
  108.     gbc.ipadx = 0;
  109.     gbc.insets = new Insets(0,10,0,0);    
  110.     gbc.anchor = GridBagConstraints.EAST;
  111.     layout.setConstraints(m_auto,gbc);
  112.     add(m_auto);
  113.  
  114.     //add the label giving move information
  115.     gbc.fill = GridBagConstraints.HORIZONTAL;
  116.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  117.     gbc.weightx = 1;
  118.     gbc.insets = new Insets(0,0,0,10);
  119.         
  120.     m_moveLabel.setAlignment(Label.RIGHT);
  121.     layout.setConstraints(m_moveLabel, gbc);
  122.     add(m_moveLabel);
  123.  
  124.     // add the canvas for displaying the DXM Animation
  125.     gbc.insets = new Insets(0,0,0,0);    
  126.     gbc.fill = GridBagConstraints.BOTH;
  127.     gbc.anchor = GridBagConstraints.NORTHWEST;
  128.     gbc.weighty = 1;
  129.     gbc.weightx = 1;
  130.     System.err.println("ART");
  131.     m_model = new ChessModel();
  132.     m_canvas = new ChessCanvas(m_model);
  133.     System.err.println("HM!");
  134.     m_canvas.resize(350,300);
  135.     System.err.println("MPH!");
  136.     layout.setConstraints(m_canvas, gbc);
  137.     add(m_canvas);
  138.     pack();
  139.  
  140.     m_autoThread.start();
  141.     
  142.   }
  143.  
  144.   public boolean handleEvent(Event evt)
  145.   {
  146.     switch (evt.id)
  147.       {
  148.     // Application shutdown (e.g. user chooses Close from the system menu).
  149.       case Event.WINDOW_DESTROY:
  150.     dispose();
  151.     System.exit(0);
  152.     return true;
  153.  
  154.       default:
  155.     return super.handleEvent(evt);
  156.       }             
  157.   }
  158.  
  159.   public boolean action(Event  evt, Object  what) {
  160.     boolean result = false;
  161.  
  162.     // sychronize with the canvas to make sure all the code we execute here
  163.     // is completed before the next frame of the animation is generated.
  164.     synchronized(m_canvas){
  165.       // callbacks for the menu items and move buttons.
  166.       if (evt.target == m_nextButton) {
  167.     Next();
  168.     result = true;
  169.       }    
  170.       else if (evt.target == m_prevButton) {
  171.     Prev();
  172.     result = true;
  173.       }
  174.       else if (evt.target == m_fileGame) {
  175.     if (m_fileGame.getState()){
  176.       m_gameFrame.show();
  177.     } else {
  178.       m_gameFrame.hide();
  179.     }
  180.     result = true;
  181.       }
  182.       else  if (evt.target == m_fileOpen) {
  183.     OpenFileGUI();
  184.     result = true;
  185.       }
  186.       else  if (evt.target == m_auto) {
  187.     //          System.err.println("WE GO");
  188.     AutoPlay();
  189.     result = true;
  190.       }
  191.     } 
  192.     
  193.     return result;
  194.   }
  195.  
  196.   protected void Next() {
  197.     // synchronize again just in case we're called from outside of action() by
  198.     // the AutoPlayer
  199.     synchronized (m_canvas) {
  200.     
  201.       // if we're at the end of the game, do nothing
  202.       if (m_curMoveIdx >= m_currentGame.m_moves.size())
  203.         return;
  204.       if (!m_nextButton.isEnabled())
  205.         return;
  206.       
  207.       //            System.err.println("moving NEXT");
  208.       // otherwise animate the next move
  209.       MoveForward((Move)m_currentGame.m_moves.elementAt(m_curMoveIdx));
  210.       m_curMoveIdx++;
  211.       ResetButtons();
  212.     }
  213.   }
  214.  
  215.   protected void Prev() {
  216.     // if we're at the beginning of the game do nothing
  217.     if (m_curMoveIdx <= 0)
  218.       return;
  219.  
  220.     // otherwise animate the current move backwards
  221.     m_curMoveIdx--;
  222.     MoveBackward((Move)m_currentGame.m_moves.elementAt(m_curMoveIdx));
  223.     ResetButtons();
  224.   }
  225.  
  226.   public void ResetGame() {
  227.     // reset the game to initial state. For simplicity, we'll just create a new
  228.     // model and canvas
  229.     remove(m_canvas);
  230.  
  231.     m_model = new ChessModel();
  232.     m_canvas = new ChessCanvas(m_model);
  233.     m_canvas.resize(300,300);
  234.     GridBagConstraints gbc = new GridBagConstraints();
  235.  
  236.     gbc.gridwidth = GridBagConstraints.REMAINDER;
  237.  
  238.     gbc.fill = GridBagConstraints.BOTH;
  239.     gbc.anchor = GridBagConstraints.NORTHWEST;
  240.     gbc.weighty = 1;
  241.     gbc.weightx = 1;
  242.  
  243.     ((GridBagLayout)getLayout()).setConstraints(m_canvas, gbc);
  244.     add(m_canvas);
  245.     pack();
  246.  
  247.     m_curMoveIdx = 0;
  248.   } 
  249.  
  250.   void DisableButtons() {
  251.     // disable the buttons so the user can't push buttons that are not appropriate 
  252.     m_prevButton.disable();
  253.     m_nextButton.disable();
  254.     m_fileGame.disable();
  255.     m_gameFrame.hide();
  256.     m_auto.disable();
  257.     if (m_auto.getState()){
  258.       m_autoThread.suspend();
  259.       //             System.err.println("Suspended autoplayer via open");
  260.       m_auto.setState(false);
  261.     }
  262.   }
  263.  
  264.   void EnableButtons() {
  265.     m_auto.enable();
  266.     m_fileGame.enable();
  267.  
  268.   }
  269.  
  270.   void ResetButtons() {
  271.     // enable/disable the next & prev move buttons based on if we can move forward or back
  272.     // also update the move label in the corner
  273.     m_moveLabel.setText("Move " + (m_curMoveIdx + 1)/2 + " of " + (m_currentGame.m_moves.size()+1)/2);
  274.  
  275.     if (m_curMoveIdx > 0)
  276.       m_prevButton.enable();
  277.     else
  278.       m_prevButton.disable();
  279.  
  280.     if (m_curMoveIdx < m_currentGame.m_moves.size())
  281.       m_nextButton.enable();
  282.     else
  283.       m_nextButton.disable();
  284.   }
  285.  
  286.   // load a new file via a FileOpen dialog
  287.   void OpenFileGUI() {
  288.     FileDialog dlg = new FileDialog(this, "Open PGN file...", FileDialog.LOAD);
  289.  
  290.     dlg.show();
  291.  
  292.     if (dlg.getFile() != null){
  293.       OpenFile(dlg.getFile());
  294.     }
  295.         
  296.   }
  297.  
  298.   // open a filename
  299.   void OpenFile(String filename) {
  300.         System.out.println(filename);
  301.     DisableButtons();
  302.     try {
  303.       // parse the game into a format easy to animate. (Game classes)
  304.       Parser parser = new Parser();
  305.       parser.Reset();
  306.       m_gameFrame.SetGames(parser.ParseFile(filename));
  307.  
  308.     } catch (Exception e)        {
  309.       System.out.println("ERROR PARSING GAME!!!");
  310.       ResetGame();
  311.       m_gameFrame.SetGames(null);
  312.       return;
  313.     }
  314.  
  315.     ResetGame();
  316.     EnableButtons();
  317.     ResetButtons();
  318.   }
  319.  
  320.   void SetGame(Game game) {
  321.     m_currentGame = game;
  322.   }
  323.  
  324.   // animate a Move structure
  325.   void MoveForward(Move move) {
  326.     if (move.result != null) {
  327.       m_model.m_resultLabel.switchText(move.result);
  328.       return;
  329.     }
  330.     // if we are promoting a piece, tell the model to promote it
  331.     if (move.promote != -1)
  332.       m_model.PromotePiece(move.xFrom,move.yFrom, move.promote);
  333.     
  334.     // if we cature a piece, tell the model to capture it
  335.     if (move.xCapture != -1) 
  336.       m_model.CapturePiece(move.xCapture, move.yCapture, move.player);
  337.     
  338.     // tell the model to animate the move
  339.     m_model.MovePiece(move.xFrom, move.yFrom, move.xTo, move.yTo);
  340.  
  341.     // if we're castling, tell the model to move the second piece
  342.     if (move.xFrom2 != -1)
  343.       m_model.MovePiece(move.xFrom2, move.yFrom2, move.xTo2, move.yTo2);
  344.  
  345.  
  346.   }
  347.  
  348.   // Animate a move structure backward. undo a MoveForward.
  349.   void MoveBackward(Move move) {
  350.     
  351.     if (move.result != null) {
  352.       m_model.m_resultLabel.switchText(null);
  353.       return;
  354.     }
  355.     // unpromote if nessecary
  356.     if (move.promote != -1)
  357.       m_model.PromotePiece(move.xTo,move.yTo, Piece.PAWN);
  358.     
  359.     // move the piece back
  360.     m_model.MovePiece(move.xTo, move.yTo, move.xFrom, move.yFrom);
  361.  
  362.     // uncastle
  363.     if (move.xFrom2 != -1)
  364.       m_model.MovePiece(move.xTo2, move.yTo2, move.xFrom2, move.yFrom2);
  365.  
  366.     // revive any captured piece
  367.     if (move.xCapture != -1) 
  368.       m_model.RevivePiece(move.xCapture, move.yCapture, move.player);
  369.     
  370.   }
  371.  
  372.   // stuff for Autoplay button  
  373.   protected void AutoPlay() {
  374.     if (m_auto.getState()) {
  375.       m_autoThread.resume();
  376.     } else {
  377.       m_autoThread.suspend();
  378.     }
  379.   }
  380. }
  381.  
  382.  
  383. class AutoPlayer extends Thread {
  384.  
  385.   protected PGNViewFrame m_frame = null;
  386.  
  387.   AutoPlayer (PGNViewFrame frame) {
  388.     m_frame = frame;
  389.   }
  390.  
  391.   public void run() {
  392.     suspend();
  393.  
  394.     while (true) {
  395.       try {
  396.         m_frame.Next();
  397.         Thread.sleep(4000);
  398.       } catch (InterruptedException e){
  399.         // do nothing
  400.       }
  401.     }
  402.   }
  403. }
  404.