• R/O
  • SSH

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

アルファ版。新版→https://osdn.jp/users/tacticsrealize/pf/ChlorophyllUploader/wiki/FrontPage


File Info

修訂. 82b375367456635f711775be0d76dcafef88ca32
大小 5,501 bytes
時間 2015-07-08 20:15:01
作者
Log Message

新ファイル形式に対応しArduinoとの連携

Content

package ants.chlorofilsender;

import java.awt.Component;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.function.Consumer;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.Timer;

import jp.hishidama.swing.layout.GroupLayoutUtil;
import mirrg.util.FrameMirrg;
import mirrg.util.Tuple;

public class FrameChlorofilPacket extends FrameMirrg
{

	public FrameChlorofilPacket(Consumer<PacketChlorofilSender> consumerPacket)
	{
		super("ChlorofilPacket");

		{
			JCheckBox checkBox1 = new JCheckBox("現在時刻に同期", true);
			Tuple<JLabel, JSpinner> spinnerTime = createSpinnerRowDate("Time");

			// P S
			ArrayList<JSpinner[]> listSpinners = new ArrayList<>();
			for (int i = 0; i < 6; i++) {
				listSpinners.add(createSpinnersRowDouble());
			}
			JSpinner[] spinnersAverage = createSpinnersRowDouble();

			Tuple<JLabel, JSpinner> spinnerHeight = createSpinnerRowDouble("Height", 0, -10000, 10000, 0.1);

			JButton buttonSend = new JButton("Send!");
			JButton buttonRandomize = new JButton("Randomize");

			//

			buttonSend.addActionListener(e -> {
				consumerPacket.accept(new PacketChlorofilSender(
					LocalDateTime.ofInstant(
						((Date) spinnerTime.getY().getValue()).toInstant(),
						ZoneId.systemDefault()),
					((Double) listSpinners.get(0)[0].getValue()),
					((Double) listSpinners.get(0)[1].getValue()),
					((Double) listSpinners.get(1)[0].getValue()),
					((Double) listSpinners.get(1)[1].getValue()),
					((Double) listSpinners.get(2)[0].getValue()),
					((Double) listSpinners.get(2)[1].getValue()),
					((Double) listSpinners.get(3)[0].getValue()),
					((Double) listSpinners.get(3)[1].getValue()),
					((Double) listSpinners.get(4)[0].getValue()),
					((Double) listSpinners.get(4)[1].getValue()),
					((Double) listSpinners.get(5)[0].getValue()),
					((Double) listSpinners.get(5)[1].getValue()),
					((Double) spinnersAverage[0].getValue()),
					((Double) spinnersAverage[1].getValue()),
					((Double) spinnerHeight.getY().getValue())));
			});

			buttonRandomize.addActionListener(e -> {
				listSpinners.forEach(spinners -> {
					spinners[0].setValue(Math.random() * 500);
					spinners[1].setValue(Math.random() * 500);
				});
				spinnersAverage[0].setValue(Math.random() * 500);
				spinnersAverage[1].setValue(Math.random() * 500);
				spinnerHeight.getY().setValue(Math.random() * 500);
			});

			{
				Timer timer1 = new Timer(1000, e -> {
					if (!checkBox1.isSelected()) return;

					LocalDateTime value = LocalDateTime.now();
					spinnerTime.getY().getModel().setValue(
						Date.from(
							value.atZone(ZoneId.systemDefault()).toInstant()));
				});
				timer1.start();

				onClosed(e -> {
					timer1.stop();
				});
			}

			{
				ArrayList<Component[]> arrayComponents = new ArrayList<>();
				arrayComponents.add(new Component[] {
					null, checkBox1, GroupLayoutUtil.SAME_L,
				});
				arrayComponents.add(new Component[] {
					spinnerTime.getX(), spinnerTime.getY(), GroupLayoutUtil.SAME_L,
				});
				arrayComponents.add(new Component[] {
					null, new JLabel("P"), new JLabel("S"),
				});
				for (int i = 0; i < listSpinners.size(); i++) {
					JSpinner[] spinners = listSpinners.get(i);
					arrayComponents.add(new Component[] {
						new JLabel("CH" + i), spinners[0], spinners[1],
					});
				}
				arrayComponents.add(new Component[] {
					new JLabel("Average"), spinnersAverage[0], spinnersAverage[1],
				});
				arrayComponents.add(new Component[] {
					spinnerHeight.getX(), spinnerHeight.getY(), GroupLayoutUtil.SAME_L,
				});
				arrayComponents.add(new Component[] {
					null, buttonSend, buttonRandomize,
				});

				Component[][] components = arrayComponents.toArray(new Component[0][]);

				GroupLayoutUtil groupLayoutUtil = new GroupLayoutUtil();

				groupLayoutUtil.setComponents(components);
				groupLayoutUtil.setGroupLayoutTo(this.getContentPane());

				groupLayoutUtil.getGroupLayout().setAutoCreateContainerGaps(true);
				groupLayoutUtil.getGroupLayout().setAutoCreateGaps(true);
			}
		}

		prepareFrame();
	}

	private static Tuple<JLabel, JSpinner> createSpinnerRowDouble(
		String labelText, double value, double min, double max, double step)
	{
		return new Tuple<>(new JLabel(labelText), createSpinnerRowDouble(value, min, max, step));
	}

	private static JSpinner createSpinnerRowDouble(
		double value, double min, double max, double step)
	{
		JSpinner spinner = new JSpinner(new SpinnerNumberModel(value, min, max, step));
		spinner.setEditor(new JSpinner.NumberEditor(spinner));

		return spinner;
	}

	private static JSpinner[] createSpinnersRowDouble()
	{
		return new JSpinner[] {
			createSpinnerRowDouble(0, -10000, 10000, 0.1),
			createSpinnerRowDouble(0, -10000, 10000, 0.1),
		};
	}

	private static final String dateFormat = "yyyy/MM/dd HH:mm:ss";

	private static Tuple<JLabel, JSpinner> createSpinnerRowDate(String labelText)
	{
		JLabel label = new JLabel(labelText);

		JSpinner spinner = new JSpinner(new SpinnerDateModel());
		spinner.setEditor(new JSpinner.DateEditor(spinner, dateFormat));

		return new Tuple<>(label, spinner);
	}

}