Package de.axxeed.animosy.gui

Source Code of de.axxeed.animosy.gui.OptionsDialog

/**
*
*/
package de.axxeed.animosy.gui;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;

import de.axxeed.animosy.model.Constants;
import de.axxeed.animosy.model.Game;
import de.axxeed.animosy.model.Manager;
import de.axxeed.animosy.model.Options;

/**
* OptionsDialog.java
* Created 12.12.2007 11:13:23
* @author Markus J. Luzius
*
*/
public class OptionsDialog extends JDialog implements Constants {
  private static final long  serialVersionUID  = -1574694043263550395L;
  private static Logger log = Logger.getLogger(OptionsDialog.class);
 
  private JCheckBox cheat = new JCheckBox("Cheat mode (MrX always visible)");
  private JComboBox mrXAlgorithm = new JComboBox(MRX_ALGORITHMS);
  private JCheckBox startLoc = new JCheckBox("Original starting locations");
 
  // options set at game start
  JButton reset = new JButton("Default values");
    private JSpinner numberOfDetectives = new JSpinner( new SpinnerNumberModel( 5, 3, 7, 1 ) );
    JSlider level = new JSlider( 1, 3, 1 );
    private JSpinner numberTaxiTickets = new JSpinner( new SpinnerNumberModel( 10, 3, 23, 1 ) );
    private JSpinner numberBusTickets  = new JSpinner( new SpinnerNumberModel8, 3, 23, 1 ) );
    private JSpinner numberUGTickets   = new JSpinner( new SpinnerNumberModel3, 3, 23, 1 ) );
    private JSpinner numberOfMoves = new JSpinner( new SpinnerNumberModel23, 15, 50, 1 ) );

    private JLabel ticketsLabel = new JLabel("# of tickets:");
  private JLabel numberOfDetectivesLabel = new JLabel("# of Detectives: ");
  private JLabel levelLabel = new JLabel("Level: ");
  private JLabel easy = new JLabel("easy");
  private JLabel normal = new JLabel("normal");
  private JLabel hard = new JLabel("hard");
  private JLabel numberOfMovesLabel = new JLabel("# of moves: ");
  private JLabel taxiLabel = new JLabel("Taxi:");
  private JLabel busLabel = new JLabel("Bus:");
  private JLabel ugLabel = new JLabel("UG:");

  public OptionsDialog() {
   
    log.debug("new option dialog, root pane: "+this.getRootPane().getWidth());
    SpringLayout layout = new SpringLayout();
    this.setLayout(layout);
    this.setSize(440,350);
    this.setResizable(false);
    this.setTitle("AnImOSY game options");
    this.setModal(true);
    Point loc = PanelRepository.get(PanelRepository.MAIN_WINDOW).getLocation();
   
    loc.translate((PanelRepository.get(PanelRepository.MAIN_WINDOW).getWidth()-300)/2, 50);
    this.setLocation( loc );
   
    // springs for the buttons
    Spring buttonWidth = Spring.constant(100);
    Spring buttonHeight = Spring.constant(20);
    Spring x = Spring.constant(200);
    Spring y = Spring.constant(290);
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        log.debug("ok "+arg0.getActionCommand());
        saveFields();
        setVisible(false);
        log.debug("Options closed, root="+getRootPane().hashCode());
      }
     
    });
    this.add(ok, new SpringLayout.Constraints(x,y,buttonWidth,buttonHeight));

    x = Spring.constant(310);
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        log.debug("cancel "+arg0.getActionCommand());
        setVisible(false);
      }
     
    });
    this.add(cancel, new SpringLayout.Constraints(x,y,buttonWidth,buttonHeight));
   
    Spring width = Spring.constant(300);
    Spring height = Spring.constant(20);
    x = Spring.constant(10);
    y = Spring.constant(20);
    this.add(cheat, new SpringLayout.Constraints(x,y,width,height));

    JLabel mrXalgoLabel = new JLabel("MrX algorithm: ");
    y = Spring.constant(60);
    width = Spring.constant(100);
    this.add(mrXalgoLabel, new SpringLayout.Constraints(x,y,width,height));

    x = Spring.constant(120);
    width = Spring.constant(100);
    mrXAlgorithm.setSelectedIndex(1);
    this.add(mrXAlgorithm, new SpringLayout.Constraints(x,y,width,height));

    x = Spring.constant(10);
    y = Spring.constant(90);
    width = Spring.constant(100);
    this.add(levelLabel, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(115);
    width = Spring.constant(120);
      level.setPaintTicks( true );
      level.setMajorTickSpacing( 1 );
    this.add(level, new SpringLayout.Constraints(x,y,width,height));
    y = Spring.constant(105);
    width = Spring.constant(40);
    x = Spring.constant(110);
    this.add(easy, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(155);
    this.add(normal, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(220);
    this.add(hard, new SpringLayout.Constraints(x,y,width,height));

    x = Spring.constant(10);
    y = Spring.constant(140);
    width = Spring.constant(100);
    this.add(numberOfDetectivesLabel, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(120);
    width = Spring.constant(50);
    this.add(numberOfDetectives, new SpringLayout.Constraints(x,y,width,height));
   
    x = Spring.constant(10);
    y = Spring.constant(170);
    width = Spring.constant(100);
    this.add(numberOfMovesLabel, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(120);
    width = Spring.constant(50);
    numberOfMoves.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent ce) {
        log.debug("#moves: "+numberOfMoves.getValue());
        log.debug("#taxitickets: "+numberTaxiTickets.getValue());
        int maxTickets = ((Integer) numberOfMoves.getValue()).intValue();
        if(((Integer) numberTaxiTickets.getValue()).intValue()>maxTickets) {
          numberTaxiTickets.setValue(new Integer(maxTickets));
        }
        numberTaxiTickets.setModel(new SpinnerNumberModel(  ((Integer) numberTaxiTickets.getValue()).intValue(), 3, ((Integer) numberOfMoves.getValue()).intValue(), 1 ));
        numberTaxiTickets.repaint();
      }
    });
    this.add(numberOfMoves, new SpringLayout.Constraints(x,y,width,height));
   
    reset.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        log.debug("reset "+arg0.getActionCommand());
          numberTaxiTickets.setModel( new SpinnerNumberModel( 10, 3, 23, 1 ) );
          numberBusTickets.setModel( new SpinnerNumberModel8, 3, 23, 1 ) );
          numberUGTickets.setModel( new SpinnerNumberModel3, 3, 23, 1 ) );
          numberOfMoves.setModel( new SpinnerNumberModel23, 15, 50, 1 ) );
          repaint();
      }
     
    });
    x = Spring.constant(220);
    y = Spring.constant(170);
    buttonWidth = Spring.constant(140);
    this.add(reset, new SpringLayout.Constraints(x,y,buttonWidth,buttonHeight));

    x = Spring.constant(10);
    y = Spring.constant(200);
    width = Spring.constant(80);
    this.add(ticketsLabel, new SpringLayout.Constraints(x,y,width,height));
    width = Spring.constant(50);
    x = Spring.constant(120);
    this.add(taxiLabel, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(160);
    this.add(numberTaxiTickets, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(220);
    this.add(busLabel, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(260);
    this.add(numberBusTickets, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(320);
    this.add(ugLabel, new SpringLayout.Constraints(x,y,width,height));
    x = Spring.constant(360);
    this.add(numberUGTickets, new SpringLayout.Constraints(x,y,width,height));

    x = Spring.constant(10);
    y = Spring.constant(230);
    width = Spring.constant(180);
    startLoc.setSelected(true);
    this.add(startLoc, new SpringLayout.Constraints(x,y,width,height));
}
 
  private void initFields() {
    log.debug("Initializing fields...");
    Options opt = Manager.getOptions();
    mrXAlgorithm.setSelectedIndex(opt.getMrXAlgorithm());
    startLoc.setSelected(opt.useOriginalStartLocations());
    cheat.setSelected(opt.isCheat());
    level.setValue(opt.getLevel());
    numberOfDetectives.setValue(new Integer(opt.getNumberOfDetectives()));
    numberOfMoves.setValue(new Integer(opt.getNumberOfMoves()));
    numberTaxiTickets.setValue(new Integer(opt.getTickets()[0]));
    numberBusTickets.setValue(new Integer(opt.getTickets()[1]));
    numberUGTickets.setValue(new Integer(opt.getTickets()[2]));
   
    if(Manager.getGame().getState()>Game.NO_GAME) {
      levelLabel.setEnabled(false);
      ticketsLabel.setEnabled(false);
      numberOfMovesLabel.setEnabled(false);
      numberOfDetectivesLabel.setEnabled(false);
      taxiLabel.setEnabled(false);
      busLabel.setEnabled(false);
      ugLabel.setEnabled(false);
      easy.setEnabled(false);
      normal.setEnabled(false);
      hard.setEnabled(false);
      startLoc.setEnabled(false);

      level.setEnabled(false);
      numberOfDetectives.setEnabled(false);
      numberOfMoves.setEnabled(false);
      numberTaxiTickets.setEnabled(false);
      numberBusTickets.setEnabled(false);
      numberUGTickets.setEnabled(false);
      reset.setEnabled(false);
     
    }
   
  }

  private void saveFields() {
    log.debug("Saving fields...");
    Options opt = Manager.getOptions();
    opt.setMrXAlgorithm(mrXAlgorithm.getSelectedIndex());
    opt.setCheat(cheat.isSelected());
    opt.setNumberOfDetectives(((Integer)numberOfDetectives.getValue()).intValue());
    opt.setNumberOfMoves(((Integer)numberOfMoves.getValue()).intValue());
    opt.setLevel(((Integer)level.getValue()).intValue());
    int[] tickets = new int[3];
    tickets[0] = ((Integer)numberTaxiTickets.getValue()).intValue();
    tickets[1] = ((Integer)numberBusTickets.getValue()).intValue();
    tickets[2] = ((Integer)numberUGTickets.getValue()).intValue();
    opt.setTickets(tickets);
    opt.setOriginalStartLocations(startLoc.isSelected());
   
    PanelRepository.get(PanelRepository.MAIN_WINDOW).repaint();
  }

  public void setVisible(boolean b) {
    if(b) initFields();
    super.setVisible(b);
  }

  public void paint(Graphics g) {
    log.debug("Paint:      "+System.currentTimeMillis());
    super.paint(g);
  }
 
 
}
TOP

Related Classes of de.axxeed.animosy.gui.OptionsDialog

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.