⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gameoptionspanel.java

📁 Java mulitplayer strategy game. Adaptation of KDE Galaxy Conquest. (rules are changed - but still th
💻 JAVA
字号:
/*
 * GameOptionsPanel.java
 *
 * Created on 1 listopad 2005, 09:38
 */

package net.sf.jawp.gui.client;

import javax.swing.JDialog;
import net.sf.jawp.api.domain.Density;
import net.sf.jawp.api.domain.GameSpeed;
import net.sf.jawp.api.domain.SpeedSetting;
import net.sf.jawp.api.domain.stub.JAWPOptionsVO;

/**
 * Options for new game.
 * @author  jarek
 */
public class GameOptionsPanel extends javax.swing.JPanel
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JAWPOptionsVO options;
	
	private String gameName;
	
	private final JDialog parentDialog;
	
	/** Creates new form GameOptionsPanel */
	public GameOptionsPanel(final JDialog parentDialog)
	{
		initComponents();
		setOptions( JAWPOptionsVO.createDefault());
		this.parentDialog = parentDialog;
	}

	public final JAWPOptionsVO getOptions()
	{
		return options;
	}

	public final void setOptions(final JAWPOptionsVO options)
	{
		this.options = options;
		if (this.options != null)
		{
			updatePanel(this.options);
		}
	}

	private void updatePanel(final JAWPOptionsVO opts)
	{
		final SpeedSetting speed = opts.getGameSpeed().getSpeedSetting();
		final int spdVal = SpeedSetting.getPosition(speed);
		this.gameSpeedSlider.setValue( spdVal);
		updateSpeedLabel();
		this.gameNeutralPlanetsSlider.setValue( opts.getNeutralPlanets());
		updateNeutralPlanetsLabel();
		setRadioButtons( opts.getPlanetsDensity());
		
	}
	
	private void retrieveOptions( )
	{
		this.setGameName(this.gameNameTextField.getText());
		//SPEED
		this.options.setGameSpeed( new GameSpeed(getSpeedFromSlider())); 
		this.options.setNeutralPlanets( this.gameNeutralPlanetsSlider.getValue());
		this.options.setPlanetsDensity( getDensityFromButtons());
	}
	
	private void setRadioButtons( final Density density)
	{
		this.sparseRadioButton.setSelected( density.equals(Density.SPARSE ));
		this.normalRadioButton.setSelected( density.equals(Density.NORMAL ));
		this.denseRadioButton.setSelected( density.equals(Density.DENSE ));
	}
	
	private SpeedSetting getSpeedFromSlider()
	{
		return SpeedSetting.values()[this.gameSpeedSlider.getValue()];
	}
	
	private Density getDensityFromButtons()
	{
		if (this.sparseRadioButton.isSelected() )
		{
			return Density.SPARSE;
		}
		if (this.normalRadioButton.isSelected() )
		{
			return Density.NORMAL;
		}
		if (this.denseRadioButton.isSelected() )
		{
			return Density.DENSE;
		}
		throw new IllegalStateException();
	}
	
	
	private void updateSpeedLabel()
	{
		this.selectedGameSpeedLabel.setText( getSpeedFromSlider().getLabel());
	}
	
	private void updateNeutralPlanetsLabel()
	{
		neutralPanetsLabel.setText( String.valueOf( this.gameNeutralPlanetsSlider.getValue()));
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        densityButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        gameNameTextField = new javax.swing.JTextField();
        gameSpeedSlider = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        selectedGameSpeedLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        sparseRadioButton = new javax.swing.JRadioButton();
        normalRadioButton = new javax.swing.JRadioButton();
        denseRadioButton = new javax.swing.JRadioButton();
        gameNeutralPlanetsSlider = new javax.swing.JSlider();
        neutralPanetsLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Game title");
        add(jLabel1, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(gameNameTextField, gridBagConstraints);

        gameSpeedSlider.setMajorTickSpacing(5);
        gameSpeedSlider.setMaximum(SpeedSetting.values().length-1);
        gameSpeedSlider.setMinorTickSpacing(1);
        gameSpeedSlider.setPaintTicks(true);
        gameSpeedSlider.setValue(5);
        gameSpeedSlider.addChangeListener(new javax.swing.event.ChangeListener()
        {
            public void stateChanged(javax.swing.event.ChangeEvent evt)
            {
                gameSpeedSliderStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(gameSpeedSlider, gridBagConstraints);

        jLabel2.setText("Game speed");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        add(jLabel2, gridBagConstraints);

        selectedGameSpeedLabel.setText("Normal");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        add(selectedGameSpeedLabel, gridBagConstraints);

        jLabel3.setText("Neutral planets");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        add(jLabel3, gridBagConstraints);

        jLabel4.setText("Density");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.weightx = 0.1;
        add(jLabel4, gridBagConstraints);

        densityButtonGroup.add(sparseRadioButton);
        sparseRadioButton.setText("sparse");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.weightx = 0.3;
        add(sparseRadioButton, gridBagConstraints);

        densityButtonGroup.add(normalRadioButton);
        normalRadioButton.setSelected(true);
        normalRadioButton.setText("normal");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.weightx = 0.3;
        add(normalRadioButton, gridBagConstraints);

        densityButtonGroup.add(denseRadioButton);
        denseRadioButton.setText("dense");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.weightx = 0.3;
        add(denseRadioButton, gridBagConstraints);

        gameNeutralPlanetsSlider.setMaximum(50);
        gameNeutralPlanetsSlider.setValue(10);
        gameNeutralPlanetsSlider.addChangeListener(new javax.swing.event.ChangeListener()
        {
            public void stateChanged(javax.swing.event.ChangeEvent evt)
            {
                gameNeutralPlanetsSliderStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(gameNeutralPlanetsSlider, gridBagConstraints);

        neutralPanetsLabel.setText("10");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        add(neutralPanetsLabel, gridBagConstraints);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        add(jButton1, gridBagConstraints);

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton2ActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        add(jButton2, gridBagConstraints);

    }
    // </editor-fold>//GEN-END:initComponents

	private void gameNeutralPlanetsSliderStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_gameNeutralPlanetsSliderStateChanged
	{//GEN-HEADEREND:event_gameNeutralPlanetsSliderStateChanged
		updateNeutralPlanetsLabel();
	}//GEN-LAST:event_gameNeutralPlanetsSliderStateChanged

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
	{//GEN-HEADEREND:event_jButton1ActionPerformed
		retrieveOptions();
		closeParentDialog();
	}//GEN-LAST:event_jButton1ActionPerformed

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
	{//GEN-HEADEREND:event_jButton2ActionPerformed
		setOptions( null );
		closeParentDialog();
	}//GEN-LAST:event_jButton2ActionPerformed

	private void gameSpeedSliderStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_gameSpeedSliderStateChanged
	{//GEN-HEADEREND:event_gameSpeedSliderStateChanged
		updateSpeedLabel();
	}//GEN-LAST:event_gameSpeedSliderStateChanged

	
	private void closeParentDialog()
	{
		if ( parentDialog != null)
		{
			parentDialog.setVisible(false);
		}
	}

	public String getGameName()
	{
		return gameName;
	}

	public void setGameName(final String gameName)
	{
		this.gameName = gameName;
	}
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton denseRadioButton;
    private javax.swing.ButtonGroup densityButtonGroup;
    private javax.swing.JTextField gameNameTextField;
    private javax.swing.JSlider gameNeutralPlanetsSlider;
    private javax.swing.JSlider gameSpeedSlider;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel neutralPanetsLabel;
    private javax.swing.JRadioButton normalRadioButton;
    private javax.swing.JLabel selectedGameSpeedLabel;
    private javax.swing.JRadioButton sparseRadioButton;
    // End of variables declaration//GEN-END:variables
	
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -