Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

frame.java 32KB

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