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 31KB

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