Package com.mlarktar.spacewar.commands

Source Code of com.mlarktar.spacewar.commands.HowToPlay$TakeAllTheSpaceLayout

/*
*  This file is part of jSpaceWar.
*
*  jSpaceWar is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  jSpaceWar is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with jSpaceWar.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.mlarktar.spacewar.commands;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.Timer;

import com.mlarktar.spacewar.Labels;
import com.mlarktar.spacewar.Space;
import com.mlarktar.spacewar.sprites.JEditorPanelWrapper;

/**
* @author malar
*
* This class shows instructions on how to play, key bindings,
* energy cost of stuff, etc.
*/
public class HowToPlay extends SWCommand implements ActionListener {
  private Space space;
  private JEditorPane html;
  private String keys;
  private String instr;
  private String current;
  private Timer timer;
 
  public HowToPlay() {
    super();
    space = new Space();
    html = new JEditorPane();
    html.setEditable(false);
    html.setOpaque(false);
   
    space.addSpaceItem(new JEditorPanelWrapper(html));

    panel.setLayout(new TakeAllTheSpaceLayout());
    panel.add(space);
    panel.add(html);
    panel.validate();
   
    keys = "classpath:resources/" + Labels.getString("Howto.Keys");
    instr = "classpath:resources/" + Labels.getString("Howto.Inst");
       
    timer = new Timer(5000, this);
  }

  /* (non-Javadoc)
   * @see com.mlarktar.spacewar.SWCommand#init()
   */
  public void init() {
    space.fillWithStars();
    setPage(keys);
  }

  private void setPage(String page) {
    try {
      current = page;
      html.setPage(page);
    } catch (IOException e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
    }
  }
 
  /* (non-Javadoc)
   * @see com.mlarktar.spacewar.SWCommand#start()
   */
  public void start() {
    space.start();
    timer.start();
  }

  /* (non-Javadoc)
   * @see com.mlarktar.spacewar.SWCommand#stop()
   */
  public void stop() {
    space.dispose();
    timer.stop();
  }

  /** This Layout manager gives all Components the same size. It
   *  also makes all the components as big as the parent container
   *  To merge two behaviors into one for example.
   *
   *  In this case I wanted to merge the starfield with the HTML
   *  capabilities of JEditorPane.
   *
   * @author malar
   */
  class TakeAllTheSpaceLayout implements LayoutManager {
    /* (non-Javadoc)
     * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
     */
    public void removeLayoutComponent(Component comp) {
    }

    /* (non-Javadoc)
     * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
     */
    public void addLayoutComponent(String name, Component comp) {
    }
   
    /* (non-Javadoc)
     * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
     */
    public void layoutContainer(Container parent) {
      Component [] each = parent.getComponents();
      for (int i = 0; i < each.length; i++) {
        each[i].setBounds(parent.getBounds());
      }
    }

    /* (non-Javadoc)
     * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
     */
    public Dimension minimumLayoutSize(Container parent) {
      Insets i = parent.getInsets();
      return new Dimension(i.right - i.left, i.bottom - i.top);
    }

    /* (non-Javadoc)
     * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
     */
    public Dimension preferredLayoutSize(Container parent) {
      Insets i = parent.getInsets();
      return new Dimension(i.right - i.left, i.bottom - i.top);
    }
  }

  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    if (current == keys) {
      setPage(instr);
    } else {
      setPage(keys);
    }
  }

  /* (non-Javadoc)
   * @see com.mlarktar.spacewar.commands.SWCommand#pause()
   */
  public void pause() {
    space.stop();
    timer.stop();
  }
}
TOP

Related Classes of com.mlarktar.spacewar.commands.HowToPlay$TakeAllTheSpaceLayout

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.