Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

frame.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * frame.java
  3. *
  4. *
  5. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  6. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  7. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  8. *
  9. * This file is part of LED-Cube.
  10. *
  11. * LED-Cube is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * LED-Cube is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import javax.swing.*;
  27. import javax.swing.event.*;
  28. import java.io.File;
  29. import com.sun.j3d.utils.universe.*;
  30. import com.sun.j3d.utils.geometry.*;
  31. import javax.media.j3d.*;
  32. import javax.vecmath.*;
  33. import com.sun.j3d.utils.behaviors.mouse.*;
  34. class Led3D {
  35. private Canvas3D canvas = null;
  36. private SimpleUniverse universe = null;
  37. private BranchGroup group = null;
  38. private Transform3D trans3D = null;
  39. private BranchGroup inBetween = null;
  40. private TransformGroup transGroup = null;
  41. private Sphere[][][] leds = new Sphere[8][8][8];
  42. private static ColoringAttributes redColor = new ColoringAttributes(new Color3f(1.0f, 0.0f, 0.0f), ColoringAttributes.FASTEST);
  43. private static ColoringAttributes whiteColor = new ColoringAttributes(new Color3f(1.0f, 1.0f, 1.0f), ColoringAttributes.FASTEST);
  44. private static Material whiteMat = new Material(new Color3f(1.0f, 1.0f, 1.0f), new Color3f(1.0f, 1.0f, 1.0f), new Color3f(1.0f, 1.0f, 1.0f), new Color3f(1.0f, 1.0f, 1.0f), 42.0f);
  45. private static Material redMat = new Material(new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), new Color3f(1.0f, 0.0f, 0.0f), 42.0f);
  46. private Point3d eye = new Point3d(3.5, 3.5, -13.0);
  47. private Point3d look = new Point3d(3.5, 3.5, 0.0);
  48. private Vector3d lookVect = new Vector3d(1.0, 1.0, 1.0);
  49. Led3D(Canvas3D canv) {
  50. canvas = canv;
  51. group = new BranchGroup();
  52. // Position viewer, so we are looking at object
  53. trans3D = new Transform3D();
  54. trans3D.lookAt(eye, look, lookVect);
  55. trans3D.invert();
  56. transGroup = new TransformGroup(trans3D);
  57. transGroup = new TransformGroup();
  58. ViewingPlatform viewingPlatform = new ViewingPlatform();
  59. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  60. transGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  61. transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
  62. transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  63. transGroup.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
  64. Viewer viewer = new Viewer(canvas);
  65. universe = new SimpleUniverse(viewingPlatform, viewer);
  66. group.addChild(transGroup);
  67. universe.getViewingPlatform().getViewPlatformTransform().setTransform(trans3D);
  68. // universe.getViewingPlatform().setNominalViewingTransform();
  69. universe.addBranchGraph(group); // Add group to universe
  70. BoundingBox boundBox = new BoundingBox(new Point3d(-5.0, -5.0, -5.0), new Point3d(13.0, 13.0, 13.0));
  71. // roration with left mouse button
  72. MouseRotate behaviour = new MouseRotate(transGroup);
  73. BranchGroup inBetween = new BranchGroup();
  74. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  75. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  76. inBetween.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  77. inBetween.addChild(behaviour);
  78. transGroup.addChild(inBetween);
  79. behaviour.setSchedulingBounds(boundBox);
  80. // zoom with middle mouse button
  81. MouseZoom beh2 = new MouseZoom(transGroup);
  82. BranchGroup brM2 = new BranchGroup();
  83. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  84. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  85. brM2.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  86. brM2.addChild(beh2);
  87. inBetween.addChild(brM2);
  88. beh2.setSchedulingBounds(boundBox);
  89. // move with right mouse button
  90. MouseTranslate beh3 = new MouseTranslate(transGroup);
  91. BranchGroup brM3 = new BranchGroup();
  92. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  93. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  94. brM3.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  95. brM3.addChild(beh3);
  96. inBetween.addChild(brM3);
  97. beh3.setSchedulingBounds(boundBox);
  98. // Add all our led sphares to the universe
  99. for (int x = 0; x < 8; x++) {
  100. for (int y = 0; y < 8; y++) {
  101. for (int z = 0; z < 8; z++) {
  102. leds[x][y][z] = new Sphere(0.05f);
  103. Appearance a = new Appearance();
  104. a.setMaterial(whiteMat);
  105. leds[x][y][z].setAppearance(a);
  106. leds[x][y][z].getShape().setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
  107. TransformGroup tg = new TransformGroup();
  108. tg.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
  109. tg.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  110. tg.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
  111. Transform3D transform = new Transform3D();
  112. Vector3f vector = new Vector3f(x, y, z);
  113. transform.setTranslation(vector);
  114. tg.setTransform(transform);
  115. tg.addChild(leds[x][y][z]);
  116. BranchGroup allTheseGroupsScareMe = new BranchGroup();
  117. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  118. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
  119. allTheseGroupsScareMe.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
  120. allTheseGroupsScareMe.addChild(tg);
  121. inBetween.addChild(allTheseGroupsScareMe);
  122. }
  123. }
  124. }
  125. // Add an ambient light
  126. Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
  127. AmbientLight light2 = new AmbientLight(light2Color);
  128. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  129. light2.setInfluencingBounds(bounds);
  130. BranchGroup fffuuuuu = new BranchGroup();
  131. light2.setEnable(true);
  132. fffuuuuu.addChild(light2);
  133. inBetween.addChild(fffuuuuu);
  134. }
  135. public void printTranslationData() {
  136. Matrix4d mat = new Matrix4d();
  137. Transform3D t = new Transform3D();
  138. transGroup.getTransform(t);
  139. t.get(mat);
  140. String s = mat.toString();
  141. System.out.println(s.replaceAll(", ", "\t"));
  142. }
  143. // 64 Element byte array
  144. public void setData(short[] data) {
  145. for (int y = 0; y < 8; y++) {
  146. for (int z = 0; z < 8; z++) {
  147. for (int x = 0; x < 8; x++) {
  148. Appearance a = new Appearance();
  149. if ((data[y + (z * 8)] & (1 << x)) != 0) {
  150. // Activate led
  151. a.setColoringAttributes(redColor);
  152. a.setMaterial(redMat);
  153. } else {
  154. // Deactivate led
  155. a.setColoringAttributes(whiteColor);
  156. a.setMaterial(whiteMat);
  157. }
  158. leds[x][y][z].setAppearance(a);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. public class frame extends JFrame implements ListSelectionListener {
  165. // Anfang Variablen
  166. private GraphicsConfiguration gConfig = SimpleUniverse.getPreferredConfiguration();
  167. private Canvas3D cubeCanvas = new Canvas3D(gConfig);
  168. private Led3D ledView = new Led3D(cubeCanvas);
  169. // Anfang Attribute
  170. private JButton editA = new JButton();
  171. private JButton editB = new JButton();
  172. private JButton editC = new JButton();
  173. private JButton editD = new JButton();
  174. private JButton editE = new JButton();
  175. private JButton editF = new JButton();
  176. private JButton editG = new JButton();
  177. private JButton editH = new JButton();
  178. private DefaultListModel frameListModel = new DefaultListModel();
  179. private JList frameList = new JList();
  180. private JScrollPane frameListScrollPane = new JScrollPane(frameList);
  181. private JButton frameUp = new JButton();
  182. private JButton frameDown = new JButton();
  183. private JButton frameAdd = new JButton();
  184. private JButton frameRemove = new JButton();
  185. private JButton frameRename = new JButton();
  186. private JButton frame = new JButton();
  187. private JList animList = new JList();
  188. private DefaultListModel animModel = new DefaultListModel();
  189. private JScrollPane animScrollPane = new JScrollPane(animList);
  190. private JButton animUp = new JButton();
  191. private JButton animDown = new JButton();
  192. private JButton animAdd = new JButton();
  193. private JButton animRemove = new JButton();
  194. private JButton animRename = new JButton();
  195. private JTextField animPath = new JTextField();
  196. private JButton load = new JButton();
  197. private JButton save = new JButton();
  198. private JButton saveAs = new JButton();
  199. private JComboBox jComboBox1 = new JComboBox();
  200. private JButton upload = new JButton();
  201. private JButton download = new JButton();
  202. private JLabel jLabel4 = new JLabel();
  203. private JTextField frameRemaining = new JTextField();
  204. private JLabel frmLngthLbl = new JLabel();
  205. private JTextField frmLngthTxt = new JTextField();
  206. private JButton frameDuration = new JButton();
  207. // Ende Attribute
  208. private cubeWorker worker = new cubeWorker();
  209. private boolean fileSelected = false;
  210. // Ende Variablen
  211. private int saveExitDialog() {
  212. String[] Optionen = {"Yes", "No"};
  213. int Auswahl = JOptionPane.showOptionDialog(this, "Do you want to save your changes?", "Save?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Optionen, Optionen[0]);
  214. if (Auswahl == JOptionPane.YES_OPTION) {
  215. return 1;
  216. } else {
  217. return 0;
  218. }
  219. }
  220. private String askString(String title, String text) {
  221. return JOptionPane.showInputDialog(null, text, title, JOptionPane.QUESTION_MESSAGE);
  222. }
  223. private void errorMessage(String s) {
  224. String[] Optionen = {"OK"};
  225. JOptionPane.showOptionDialog(this, s, "Error!", JOptionPane.YES_OPTION, JOptionPane.ERROR_MESSAGE, null, Optionen, Optionen[0]);
  226. }
  227. public void valueChanged(ListSelectionEvent evt) {
  228. if ((!evt.getValueIsAdjusting()) && ((evt.getSource() == animList) || (evt.getSource() == frameList))) {
  229. // If animList or framsList is the source, we act...
  230. // If both selections are valid, update frame duration and set 3D data
  231. if ((animList.getSelectedIndex() != -1) && (frameList.getSelectedIndex() != -1)) {
  232. ledView.setData(worker.getFrame(animList.getSelectedIndex(), frameList.getSelectedIndex()));
  233. frmLngthTxt.setText(Integer.toString(worker.getFrameTime(animList.getSelectedIndex(), frameList.getSelectedIndex())));
  234. } else {
  235. // clear frame duration
  236. frmLngthTxt.setText("");
  237. }
  238. if ((evt.getSource() == animList) && (animList.getSelectedIndex() != -1)) {
  239. // animList selection changed, update frameList
  240. frameListModel.clear();
  241. for (int i = 0; i < worker.numOfFrames(animList.getSelectedIndex()); i++) {
  242. frameListModel.addElement(worker.getFrameName(animList.getSelectedIndex(), i));
  243. }
  244. frameList.setModel(frameListModel);
  245. }
  246. }
  247. }
  248. private void save() {
  249. if (fileSelected == false) {
  250. JFileChooser fc = new JFileChooser();
  251. int ret = fc.showSaveDialog(this);
  252. if (ret == JFileChooser.APPROVE_OPTION) {
  253. File file = fc.getSelectedFile();
  254. fileSelected = true;
  255. animPath.setText(file.getPath());
  256. worker.saveState(animPath.getText());
  257. }
  258. } else {
  259. worker.saveState(animPath.getText());
  260. }
  261. }
  262. public frame(String title) {
  263. // Frame-Initialisierung
  264. super(title);
  265. String[] sPorts = worker.getSerialPorts();
  266. for(int i = 0; i < sPorts.length; i++){
  267. jComboBox1.addItem(sPorts[i]);
  268. }
  269. for(int i = 0; i < worker.numOfAnimations(); i++){
  270. animModel.addElement(worker.getAnimationName(i));
  271. }
  272. addWindowListener(new WindowAdapter() {
  273. public void windowClosing(WindowEvent evt) {
  274. if (worker.changedStateSinceSave()) {
  275. if (saveExitDialog() == 1) {
  276. save();
  277. } else {
  278. return;
  279. }
  280. }
  281. System.exit(0);
  282. }
  283. });
  284. int frameWidth = 661;
  285. int frameHeight = 417;
  286. setSize(frameWidth, frameHeight);
  287. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  288. int x = (d.width - getSize().width) / 2;
  289. int y = (d.height - getSize().height) / 2 ;
  290. setLocation(x, y);
  291. Container cp = getContentPane();
  292. cp.setLayout(null);
  293. // Anfang Komponenten
  294. // ledView init
  295. short[] dat = new short[64];
  296. for (int i = 0; i < dat.length; i++) {
  297. dat[i] = 0xFF;
  298. }
  299. ledView.setData(dat);
  300. //----- 3D-----
  301. //-------------
  302. cubeCanvas.setBounds(8, 8, 250, 250);
  303. cp.add(cubeCanvas);
  304. //-------------
  305. editA.setBounds(264, 8, 107, 25);
  306. editA.setText("Layer A");
  307. editA.setFont(new Font("Dialog", Font.PLAIN, 13));
  308. cp.add(editA);
  309. editA.addActionListener(new ActionListener() {
  310. public void actionPerformed(ActionEvent evt) {
  311. editA_ActionPerformed(evt);
  312. }
  313. });
  314. editB.setBounds(264, 40, 107, 25);
  315. editB.setText("Layer B");
  316. editB.setFont(new Font("Dialog", Font.PLAIN, 13));
  317. cp.add(editB);
  318. editB.addActionListener(new ActionListener() {
  319. public void actionPerformed(ActionEvent evt) {
  320. editB_ActionPerformed(evt);
  321. }
  322. });
  323. editC.setBounds(264, 72, 107, 25);
  324. editC.setText("Layer C");
  325. editC.setFont(new Font("Dialog", Font.PLAIN, 13));
  326. cp.add(editC);
  327. editC.addActionListener(new ActionListener() {
  328. public void actionPerformed(ActionEvent evt) {
  329. editC_ActionPerformed(evt);
  330. }
  331. });
  332. editD.setBounds(264, 104, 107, 25);
  333. editD.setText("Layer D");
  334. editD.setFont(new Font("Dialog", Font.PLAIN, 13));
  335. cp.add(editD);
  336. editD.addActionListener(new ActionListener() {
  337. public void actionPerformed(ActionEvent evt) {
  338. editD_ActionPerformed(evt);
  339. }
  340. });
  341. editE.setBounds(264, 136, 107, 25);
  342. editE.setText("Layer E");
  343. editE.setFont(new Font("Dialog", Font.PLAIN, 13));
  344. cp.add(editE);
  345. editE.addActionListener(new ActionListener() {
  346. public void actionPerformed(ActionEvent evt) {
  347. editE_ActionPerformed(evt);
  348. }
  349. });
  350. editF.setBounds(264, 168, 107, 25);
  351. editF.setText("Layer F");
  352. editF.setFont(new Font("Dialog", Font.PLAIN, 13));
  353. cp.add(editF);
  354. editF.addActionListener(new ActionListener() {
  355. public void actionPerformed(ActionEvent evt) {
  356. editF_ActionPerformed(evt);
  357. }
  358. });
  359. editG.setBounds(264, 200, 107, 25);
  360. editG.setText("Layer G");
  361. editG.setFont(new Font("Dialog", Font.PLAIN, 13));
  362. cp.add(editG);
  363. editG.addActionListener(new ActionListener() {
  364. public void actionPerformed(ActionEvent evt) {
  365. editG_ActionPerformed(evt);
  366. }
  367. });
  368. editH.setBounds(264, 232, 107, 25);
  369. editH.setText("Layer H");
  370. editH.setFont(new Font("Dialog", Font.PLAIN, 13));
  371. cp.add(editH);
  372. editH.addActionListener(new ActionListener() {
  373. public void actionPerformed(ActionEvent evt) {
  374. editH_ActionPerformed(evt);
  375. }
  376. });
  377. frameListScrollPane.setBounds(384, 8, 145, 249);
  378. frameList.setModel(frameListModel);
  379. cp.add(frameListScrollPane);
  380. frameUp.setBounds(544, 8, 107, 28);
  381. frameUp.setText("Move up");
  382. frameUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  383. cp.add(frameUp);
  384. frameUp.addActionListener(new ActionListener() {
  385. public void actionPerformed(ActionEvent evt) {
  386. frameUp_ActionPerformed(evt);
  387. }
  388. });
  389. frameAdd.setBounds(544, 39, 107, 28);
  390. frameAdd.setText("Add");
  391. frameAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  392. cp.add(frameAdd);
  393. frameAdd.addActionListener(new ActionListener() {
  394. public void actionPerformed(ActionEvent evt) {
  395. frameAdd_ActionPerformed(evt);
  396. }
  397. });
  398. frameRemove.setBounds(544, 70, 107, 28);
  399. frameRemove.setText("Remove");
  400. frameRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  401. cp.add(frameRemove);
  402. frameRemove.addActionListener(new ActionListener() {
  403. public void actionPerformed(ActionEvent evt) {
  404. frameRemove_ActionPerformed(evt);
  405. }
  406. });
  407. frameRename.setBounds(544, 101, 107, 28);
  408. frameRename.setText("Rename");
  409. frameRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  410. cp.add(frameRename);
  411. frameRename.addActionListener(new ActionListener() {
  412. public void actionPerformed(ActionEvent evt) {
  413. int a = animList.getSelectedIndex();
  414. if (a < 0) {
  415. errorMessage("Select an animation!");
  416. return;
  417. }
  418. int f = frameList.getSelectedIndex();
  419. if (f < 0) {
  420. errorMessage("Select a frame!");
  421. return;
  422. }
  423. worker.setFrameName(askString("Rename", "Rename " + frameList.getSelectedValue() + "?"), a, f);
  424. frameListModel.set(f, worker.getFrameName(a, f));
  425. frameList.setModel(frameListModel);
  426. }
  427. });
  428. frameDown.setBounds(544, 132, 107, 28);
  429. frameDown.setText("Move down");
  430. frameDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  431. cp.add(frameDown);
  432. frameDown.addActionListener(new ActionListener() {
  433. public void actionPerformed(ActionEvent evt) {
  434. frameDown_ActionPerformed(evt);
  435. }
  436. });
  437. frmLngthLbl.setBounds(536, 160, 113, 24);
  438. frmLngthLbl.setText("Time (1/24 sec)");
  439. frmLngthLbl.setFont(new Font("Dialog", Font.PLAIN, 13));
  440. cp.add(frmLngthLbl);
  441. frmLngthTxt.setBounds(536, 184, 50, 24);
  442. frmLngthTxt.setText("");
  443. frmLngthTxt.setFont(new Font("Dialog", Font.PLAIN, 13));
  444. cp.add(frmLngthTxt);
  445. frameDuration.setBounds(590, 184, 50, 24);
  446. frameDuration.setText("OK");
  447. frameDuration.setFont(new Font("Dialog", Font.PLAIN, 13));
  448. cp.add(frameDuration);
  449. frameDuration.addActionListener(new ActionListener() {
  450. public void actionPerformed(ActionEvent evt) {
  451. if (frmLngthTxt.getText().equals("0")) {
  452. errorMessage("0 is not a valid value!");
  453. frmLngthTxt.setText("1");
  454. } else if (Integer.parseInt(frmLngthTxt.getText()) > 256) {
  455. errorMessage("Value too high. Max: 256");
  456. frmLngthTxt.setText("256");
  457. return;
  458. } else {
  459. if (animList.getSelectedIndex() == -1) {
  460. errorMessage("Please select an animation!");
  461. return;
  462. }
  463. if (frameList.getSelectedIndex() == -1) {
  464. errorMessage("Please select a frame!");
  465. return;
  466. }
  467. worker.setFrameTime((byte)(Integer.parseInt(frmLngthTxt.getText()) - 1), animList.getSelectedIndex(), frameList.getSelectedIndex());
  468. }
  469. }
  470. });
  471. animScrollPane.setBounds(8, 264, 209, 121);
  472. animList.setModel(animModel);
  473. cp.add(animScrollPane);
  474. animUp.setBounds(224, 264, 99, 25);
  475. animUp.setText("Move up");
  476. animUp.setFont(new Font("Dialog", Font.PLAIN, 13));
  477. cp.add(animUp);
  478. animUp.addActionListener(new ActionListener() {
  479. public void actionPerformed(ActionEvent evt) {
  480. animUp_ActionPerformed(evt);
  481. }
  482. });
  483. animDown.setBounds(224, 368, 99, 25);
  484. animDown.setText("Move down");
  485. animDown.setFont(new Font("Dialog", Font.PLAIN, 13));
  486. cp.add(animDown);
  487. animDown.addActionListener(new ActionListener() {
  488. public void actionPerformed(ActionEvent evt) {
  489. animDown_ActionPerformed(evt);
  490. }
  491. });
  492. animRename.setBounds(224, 342, 99, 25);
  493. animRename.setText("Rename");
  494. animRename.setFont(new Font("Dialog", Font.PLAIN, 13));
  495. cp.add(animRename);
  496. animRename.addActionListener(new ActionListener() {
  497. public void actionPerformed(ActionEvent evt) {
  498. int a = animList.getSelectedIndex();
  499. if (a < 0) {
  500. errorMessage("Select an animation!");
  501. return;
  502. }
  503. worker.setAnimationName(askString("Rename", "Rename " + animList.getSelectedValue() + "?"), a);
  504. animModel.set(a, worker.getAnimationName(a));
  505. animList.setModel(animModel);
  506. }
  507. });
  508. animAdd.setBounds(224, 290, 99, 25);
  509. animAdd.setText("Add");
  510. animAdd.setFont(new Font("Dialog", Font.PLAIN, 13));
  511. cp.add(animAdd);
  512. animAdd.addActionListener(new ActionListener() {
  513. public void actionPerformed(ActionEvent evt) {
  514. animAdd_ActionPerformed(evt);
  515. }
  516. });
  517. animRemove.setBounds(224, 316, 99, 25);
  518. animRemove.setText("Remove");
  519. animRemove.setFont(new Font("Dialog", Font.PLAIN, 13));
  520. cp.add(animRemove);
  521. animRemove.addActionListener(new ActionListener() {
  522. public void actionPerformed(ActionEvent evt) {
  523. animRemove_ActionPerformed(evt);
  524. }
  525. });
  526. animPath.setBounds(344, 264, 305, 24);
  527. animPath.setEditable(false);
  528. animPath.setText("Load/Save an animation file...");
  529. animPath.setFont(new Font("Dialog", Font.PLAIN, 13));
  530. cp.add(animPath);
  531. load.setBounds(344, 296, 100, 25);
  532. load.setText("Load");
  533. load.setFont(new Font("Dialog", Font.PLAIN, 13));
  534. cp.add(load);
  535. load.addActionListener(new ActionListener() {
  536. public void actionPerformed(ActionEvent evt) {
  537. load_ActionPerformed(evt);
  538. }
  539. });
  540. save.setBounds(454, 296, 100, 25);
  541. save.setText("Save");
  542. save.setFont(new Font("Dialog", Font.PLAIN, 13));
  543. cp.add(save);
  544. save.addActionListener(new ActionListener() {
  545. public void actionPerformed(ActionEvent evt) {
  546. save_ActionPerformed(evt);
  547. }
  548. });
  549. saveAs.setBounds(564, 296, 90, 25);
  550. saveAs.setText("Save As");
  551. saveAs.setFont(new Font("Dialog", Font.PLAIN, 13));
  552. cp.add(saveAs);
  553. saveAs.addActionListener(new ActionListener() {
  554. public void actionPerformed(ActionEvent evt) {
  555. saveAs_ActionPerformed(evt);
  556. }
  557. });
  558. jComboBox1.setBounds(344, 328, 305, 24);
  559. jComboBox1.setFont(new Font("Dialog", Font.PLAIN, 13));
  560. cp.add(jComboBox1);
  561. upload.setBounds(344, 360, 147, 25);
  562. upload.setText("Upload");
  563. upload.setFont(new Font("Dialog", Font.PLAIN, 13));
  564. cp.add(upload);
  565. upload.addActionListener(new ActionListener() {
  566. public void actionPerformed(ActionEvent evt) {
  567. upload_ActionPerformed(evt);
  568. }
  569. });
  570. download.setBounds(504, 360, 147, 25);
  571. download.setText("Download");
  572. download.setFont(new Font("Dialog", Font.PLAIN, 13));
  573. cp.add(download);
  574. download.addActionListener(new ActionListener() {
  575. public void actionPerformed(ActionEvent evt) {
  576. download_ActionPerformed(evt);
  577. }
  578. });
  579. jLabel4.setBounds(536, 208, 112, 20);
  580. jLabel4.setText("Remaining:");
  581. jLabel4.setFont(new Font("Dialog", Font.PLAIN, 13));
  582. cp.add(jLabel4);
  583. frameRemaining.setBounds(536, 232, 113, 24);
  584. frameRemaining.setEditable(false);
  585. frameRemaining.setText(String.valueOf(worker.framesRemaining()));
  586. frameRemaining.setFont(new Font("Dialog", Font.PLAIN, 13));
  587. cp.add(frameRemaining);
  588. animList.setFont(new Font("Dialog", Font.PLAIN, 13));
  589. frameList.setFont(new Font("Dialog", Font.PLAIN, 13));
  590. // Ende Komponenten
  591. animList.addListSelectionListener(this);
  592. frameList.addListSelectionListener(this);
  593. setResizable(false);
  594. setVisible(true);
  595. }
  596. // Anfang Methoden
  597. // Anfang Ereignisprozeduren
  598. public void editA_ActionPerformed(ActionEvent evt) {
  599. if(animList.getSelectedIndex() == -1){
  600. errorMessage("Please select an animation.");
  601. } else if(frameList.getSelectedIndex() == -1){
  602. errorMessage("Please select a frame.");
  603. } else {
  604. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 0, worker);
  605. }
  606. }
  607. public void editB_ActionPerformed(ActionEvent evt) {
  608. if(animList.getSelectedIndex() == -1){
  609. errorMessage("Please select an animation.");
  610. } else if(frameList.getSelectedIndex() == -1){
  611. errorMessage("Please select a frame.");
  612. } else {
  613. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 1, worker);
  614. }
  615. }
  616. public void editC_ActionPerformed(ActionEvent evt) {
  617. if(animList.getSelectedIndex() == -1){
  618. errorMessage("Please select an animation.");
  619. } else if(frameList.getSelectedIndex() == -1){
  620. errorMessage("Please select a frame.");
  621. } else {
  622. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 2, worker);
  623. }
  624. }
  625. public void editD_ActionPerformed(ActionEvent evt) {
  626. if(animList.getSelectedIndex() == -1){
  627. errorMessage("Please select an animation.");
  628. } else if(frameList.getSelectedIndex() == -1){
  629. errorMessage("Please select a frame.");
  630. } else {
  631. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 3, worker);
  632. }
  633. }
  634. public void editE_ActionPerformed(ActionEvent evt) {
  635. if(animList.getSelectedIndex() == -1){
  636. errorMessage("Please select an animation.");
  637. } else if(frameList.getSelectedIndex() == -1){
  638. errorMessage("Please select a frame.");
  639. } else {
  640. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 4, worker);
  641. }
  642. }
  643. public void editF_ActionPerformed(ActionEvent evt) {
  644. if(animList.getSelectedIndex() == -1){
  645. errorMessage("Please select an animation.");
  646. } else if(frameList.getSelectedIndex() == -1){
  647. errorMessage("Please select a frame.");
  648. } else {
  649. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 5, worker);
  650. }
  651. }
  652. public void editG_ActionPerformed(ActionEvent evt) {
  653. if(animList.getSelectedIndex() == -1){
  654. errorMessage("Please select an animation.");
  655. } else if(frameList.getSelectedIndex() == -1){
  656. errorMessage("Please select a frame.");
  657. } else {
  658. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 6, worker);
  659. }
  660. }
  661. public void editH_ActionPerformed(ActionEvent evt) {
  662. if(animList.getSelectedIndex() == -1){
  663. errorMessage("Please select an animation.");
  664. } else if(frameList.getSelectedIndex() == -1){
  665. errorMessage("Please select a frame.");
  666. } else {
  667. layerEditFrame layerFrame1 = new layerEditFrame(animList.getSelectedIndex(), frameList.getSelectedIndex(), 7, worker);
  668. }
  669. }
  670. public void frameUp_ActionPerformed(ActionEvent evt) {
  671. int i = frameList.getSelectedIndex();
  672. if ((i > 0) && (frameListModel.getSize() >= 2)) {
  673. Object tmp = frameListModel.get(i);
  674. frameListModel.set(i, frameListModel.get(i - 1));
  675. frameListModel.set(i - 1, tmp);
  676. frameList.setSelectedIndex(i - 1);
  677. worker.moveFrame(worker.UP, animList.getSelectedIndex(), frameList.getSelectedIndex());
  678. }
  679. }
  680. public void frameDown_ActionPerformed(ActionEvent evt) {
  681. int i = frameList.getSelectedIndex();
  682. if ((i >= 0) && (frameListModel.getSize() >= 2) && (i < (frameListModel.getSize() - 1))) {
  683. Object tmp = frameListModel.get(i);
  684. frameListModel.set(i, frameListModel.get(i + 1));
  685. frameListModel.set(i + 1, tmp);
  686. frameList.setSelectedIndex(i + 1);
  687. worker.moveFrame(worker.DOWN, animList.getSelectedIndex(), frameList.getSelectedIndex());
  688. }
  689. }
  690. public void frameAdd_ActionPerformed(ActionEvent evt) {
  691. if(animList.getSelectedIndex() == -1){
  692. errorMessage("Please select an animation!");
  693. } else {
  694. worker.addFrame(animList.getSelectedIndex());
  695. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  696. int n = worker.numOfFrames(animList.getSelectedIndex()) - 1;
  697. if (n < 0) {
  698. n = 0;
  699. }
  700. frameListModel.add(n, worker.getFrameName(animList.getSelectedIndex(), n));
  701. frameList.setModel(frameListModel);
  702. }
  703. }
  704. public void frameRemove_ActionPerformed(ActionEvent evt) {
  705. if(animList.getSelectedIndex() == -1){
  706. errorMessage("Select an animation.");
  707. } else if(frameList.getSelectedIndex() == -1){
  708. errorMessage("Select a frame.");
  709. } else {
  710. worker.removeFrame(animList.getSelectedIndex(), frameList.getSelectedIndex());
  711. frameRemaining.setText(Integer.toString(worker.framesRemaining()));
  712. frameListModel.removeElementAt(frameList.getSelectedIndex());
  713. frameList.setModel(frameListModel);
  714. }
  715. }
  716. public void animUp_ActionPerformed(ActionEvent evt) {
  717. int i = animList.getSelectedIndex();
  718. if ((i > 0) && (animModel.getSize() >= 2)) {
  719. Object tmp = animModel.get(i);
  720. animModel.set(i, animModel.get(i - 1));
  721. animModel.set(i - 1, tmp);
  722. animList.setSelectedIndex(i - 1);
  723. worker.moveAnimation(worker.UP, animList.getSelectedIndex());
  724. }
  725. }
  726. public void animDown_ActionPerformed(ActionEvent evt) {
  727. int i = animList.getSelectedIndex();
  728. if ((i >= 0) && (animModel.getSize() >= 2) && (i < (animModel.getSize() - 1))) {
  729. Object tmp = animModel.get(i);
  730. animModel.set(i, animModel.get(i + 1));
  731. animModel.set(i + 1, tmp);
  732. animList.setSelectedIndex(i + 1);
  733. worker.moveAnimation(worker.DOWN, animList.getSelectedIndex());
  734. }
  735. }
  736. public void animAdd_ActionPerformed(ActionEvent evt) {
  737. if(worker.addAnimation() == -1){
  738. errorMessage("Could not add animation!");
  739. } else {
  740. animModel.clear();
  741. for (int i = 0; i < worker.numOfAnimations(); i++) {
  742. animModel.add(i, worker.getAnimationName(i));
  743. }
  744. animList.setModel(animModel);
  745. }
  746. }
  747. public void animRemove_ActionPerformed(ActionEvent evt) {
  748. if(animList.getSelectedIndex() == -1){
  749. errorMessage("Select an animation.");
  750. } else {
  751. worker.removeAnimation(animList.getSelectedIndex());
  752. animModel.removeElementAt(animList.getSelectedIndex());
  753. animList.setModel(animModel);
  754. }
  755. }
  756. public void load_ActionPerformed(ActionEvent evt) {
  757. JFileChooser fc = new JFileChooser();
  758. int ret = fc.showOpenDialog(this);
  759. if (ret == JFileChooser.APPROVE_OPTION) {
  760. File file = fc.getSelectedFile();
  761. if (fileSelected == false) {
  762. fileSelected = true;
  763. }
  764. animPath.setText(file.getPath());
  765. worker.loadState(animPath.getText());
  766. animModel.clear();
  767. for (int i = 0; i < worker.numOfAnimations(); i++) {
  768. animModel.addElement(worker.getAnimationName(i));
  769. }
  770. animList.setModel(animModel);
  771. frameListModel.clear();
  772. frameList.setModel(frameListModel);
  773. }
  774. }
  775. public void save_ActionPerformed(ActionEvent evt) {
  776. if (fileSelected == false) {
  777. JFileChooser fc = new JFileChooser();
  778. int ret = fc.showSaveDialog(this);
  779. if (ret == JFileChooser.APPROVE_OPTION) {
  780. File file = fc.getSelectedFile();
  781. fileSelected = true;
  782. animPath.setText(file.getPath());
  783. worker.saveState(animPath.getText());
  784. }
  785. } else {
  786. worker.saveState(animPath.getText());
  787. }
  788. }
  789. public void saveAs_ActionPerformed(ActionEvent evt) {
  790. JFileChooser fc;
  791. if (fileSelected == true) {
  792. fc = new JFileChooser(new File(animPath.getText()).getParentFile());
  793. } else {
  794. fc = new JFileChooser();
  795. }
  796. int ret = fc.showSaveDialog(this);
  797. if (ret == JFileChooser.APPROVE_OPTION) {
  798. File file = fc.getSelectedFile();
  799. if (fileSelected == false) {
  800. fileSelected = true;
  801. }
  802. animPath.setText(file.getPath());
  803. worker.saveState(animPath.getText());
  804. }
  805. }
  806. public void upload_ActionPerformed(ActionEvent evt) {
  807. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  808. errorMessage("No serial port selected...");
  809. } else {
  810. worker.uploadState((String)jComboBox1.getSelectedItem());
  811. }
  812. }
  813. public void download_ActionPerformed(ActionEvent evt) {
  814. if (jComboBox1.getSelectedItem().equals("Select serial port...")) {
  815. errorMessage("No serial port selected...");
  816. } else {
  817. worker.downloadState((String)jComboBox1.getSelectedItem());
  818. }
  819. }
  820. public Led3D get3D() {
  821. return ledView;
  822. }
  823. // Ende Ereignisprozeduren
  824. public static void main(String[] args) {
  825. frame f = new frame("Cube Control");
  826. Led3D l = f.get3D();
  827. java.util.Scanner sc = new java.util.Scanner(System.in);
  828. System.out.println("## Cube Control Debug Console ##");
  829. System.out.println("Enter a Command ('h' for help)\n");
  830. System.out.print("$> ");
  831. do {
  832. if (sc.hasNext()) {
  833. String s = sc.next();
  834. if (s.equals("p") || (s.equals("print")))
  835. l.printTranslationData();
  836. if (s.equals("q") || s.equals("quit"))
  837. System.exit(0);
  838. if (s.equals("h") || (s.equals("help"))) {
  839. System.out.println("Commands:");
  840. System.out.println("\t'print' / 'p'\t:\tPrint 3D Translation Matrix Data");
  841. System.out.println("\t'help' / 'h'\t:\tShow this message");
  842. System.out.println("\t'quit' / 'q'\t:\tExit Cube Control");
  843. }
  844. System.out.print("$> ");
  845. }
  846. } while (true);
  847. }
  848. // Ende Methoden
  849. }