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

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