Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frame.java 30KB

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