private synchronized void startAnimation( JComponent component, Part part, State startState, State endState, long millis) { boolean isForwardAndReverse = false; if (endState == State.DEFAULTED) { isForwardAndReverse = true; } Map<Part, AnimationState> map = animationStateMap.get(component); if (millis <= 0) { if (map != null) { map.remove(part); if (map.size() == 0) { animationStateMap.remove(component); } } return; } if (map == null) { map = new EnumMap<Part, AnimationState>(Part.class); animationStateMap.put(component, map); } map.put(part, new AnimationState(startState, millis, isForwardAndReverse)); if (!timer.isRunning()) { timer.start(); } }
public synchronized void actionPerformed(ActionEvent e) { java.util.List<JComponent> componentsToRemove = null; java.util.List<Part> partsToRemove = null; for (JComponent component : animationStateMap.keySet()) { component.repaint(); if (partsToRemove != null) { partsToRemove.clear(); } Map<Part, AnimationState> map = animationStateMap.get(component); if (!component.isShowing() || map == null || map.size() == 0) { if (componentsToRemove == null) { componentsToRemove = new ArrayList<JComponent>(); } componentsToRemove.add(component); continue; } for (Part part : map.keySet()) { if (map.get(part).isDone()) { if (partsToRemove == null) { partsToRemove = new ArrayList<Part>(); } partsToRemove.add(part); } } if (partsToRemove != null) { if (partsToRemove.size() == map.size()) { // animation is done for the component if (componentsToRemove == null) { componentsToRemove = new ArrayList<JComponent>(); } componentsToRemove.add(component); } else { for (Part part : partsToRemove) { map.remove(part); } } } } if (componentsToRemove != null) { for (JComponent component : componentsToRemove) { animationStateMap.remove(component); } } if (animationStateMap.size() == 0) { timer.stop(); } }
/** * \ Draw saved State (color dots on panel) on WhiteBoard * * @param outstream * @throws IOException */ public void writeState(OutputStream outstream) throws IOException { if (state == null) return; synchronized (state) { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(outstream)); // DataOutputStream dos=new DataOutputStream(outstream); dos.writeInt(state.size()); for (Map.Entry<Point, Color> entry : state.entrySet()) { Point point = entry.getKey(); Color col = entry.getValue(); dos.writeInt(point.x); dos.writeInt(point.x); dos.writeInt(col.getRGB()); } dos.flush(); System.out.println("wrote " + state.size() + " elements"); } }
public Animation() { String[] keys = {"no Animator found"}; if (tryDir(".")) // || tryDir("BLM305") || tryDir("CSE470")) keys = map.keySet().toArray(keys); System.out.println(map.size() + " classes loaded"); menu = new JComboBox(keys); pan.setLayout(new BorderLayout(GAP, GAP - 4)); pan.setBorder(new javax.swing.border.EmptyBorder(GAP, GAP, GAP, GAP)); pan.setBackground(COLOR); last = new JPanel(); last.setPreferredSize(DIM); pan.add(last, "Center"); ref.setFont(NORM); ref.setEditable(false); ref.setColumns(35); ref.setDragEnabled(true); pan.add(ref, "North"); pan.add(bottomPanel(), "South"); pan.setToolTipText("A collective project for BLM320"); menu.setToolTipText("Animator classes"); who.setToolTipText("author()"); ref.setToolTipText("description()"); Closer ear = new Closer(); menu.addActionListener(ear); stop.addActionListener(ear); frm.addWindowListener(ear); if (map.size() > 0) setItem(0); frm.setContentPane(pan); frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frm.setLocation(scaled(120), scaled(90)); frm.pack(); // setSize() is called here frm.setVisible(true); start(); }
/** * Read State (color dots) from input stream * * @param instream * @throws IOException */ public void readState(InputStream instream) throws IOException { DataInputStream in = new DataInputStream(new BufferedInputStream(instream)); Map<Point, Color> new_state = new LinkedHashMap<Point, Color>(); int num = in.readInt(); for (int i = 0; i < num; i++) { Point point = new Point(in.readInt(), in.readInt()); Color col = new Color(in.readInt()); new_state.put(point, col); } synchronized (state) { state.clear(); state.putAll(new_state); System.out.println("read " + state.size() + " elements"); createOffscreenImage(true); } }
boolean tryDir(String d) { ClassLoader L = getClass().getClassLoader(); File p = new File(d, PACKAGE); System.out.println("Try " + p); if (!p.exists() || !p.isDirectory()) return false; for (File f : p.listFiles()) { String s = f.getName(); if (!s.endsWith(".class")) continue; String name = s.substring(0, s.length() - 6); try { Class<?> c = L.loadClass(PACKAGE + "." + name); if (!Animator.class.isAssignableFrom(c)) continue; Animator a = (Animator) c.newInstance(); a.container().setPreferredSize(DIM); map.put(name, a); System.out.println(" " + name); // ClassNotFoundException InstantiationException IllegalAccessException } catch (Exception e) { continue; } } return map.size() > 0; }
private void refresh(int index) { GridBagConstraints c = new GridBagConstraints(); // No connector ................ if (acs.size() == 0) { add(new JLabel(bundle.getString("CTL_No_Connector")), c); return; } // Connector switch ................ if (acs.size() > 1) { c.insets = new Insets(0, 0, 3, 3); add(new JLabel(bundle.getString("CTL_Connector")), c); cbConnectors = new JComboBox(); int i, k = acs.size(); for (i = 0; i < k; i++) { AttachingConnector ac = (AttachingConnector) acs.get(i); int jj = ac.name().lastIndexOf('.'); String s = (jj < 0) ? ac.name() : ac.name().substring(jj + 1); cbConnectors.addItem(s + " (" + ac.description() + ")"); } c = new GridBagConstraints(); c.insets = new Insets(0, 3, 3, 0); c.weightx = 1.0; c.fill = java.awt.GridBagConstraints.HORIZONTAL; c.gridwidth = 0; cbConnectors.setSelectedIndex(index); cbConnectors.setActionCommand("SwitchMe!"); cbConnectors.addActionListener(this); add(cbConnectors, c); } ac = (AttachingConnector) acs.get(index); // Transport ................ c = new GridBagConstraints(); c.insets = new Insets(3, 0, 0, 3); add(new JLabel(bundle.getString("CTL_Transport")), c); JTextField tfTransport = new JTextField(ac.transport().name()); tfTransport.setEnabled(false); c = new GridBagConstraints(); c.gridwidth = 0; c.insets = new Insets(3, 3, 0, 0); c.fill = java.awt.GridBagConstraints.HORIZONTAL; c.weightx = 1.0; add(tfTransport, c); // Other params ................ args = ac.defaultArguments(); tfParams = new JTextField[args.size()]; Iterator it = args.keySet().iterator(); int i = 0; while (it.hasNext()) { String name = (String) it.next(); Argument a = (Argument) args.get(name); c = new GridBagConstraints(); c.insets = new Insets(6, 0, 0, 3); c.anchor = GridBagConstraints.WEST; add(new JLabel(a.label() + ": "), c); JTextField tfParam = new JTextField(a.value()); tfParams[i++] = tfParam; tfParam.setName(name); c = new GridBagConstraints(); c.gridwidth = 0; c.insets = new Insets(6, 3, 0, 0); c.fill = java.awt.GridBagConstraints.HORIZONTAL; c.weightx = 1.0; add(tfParam, c); } c = new GridBagConstraints(); c.weighty = 1.0; JPanel p = new JPanel(); p.setPreferredSize(new Dimension(1, 1)); add(p, c); }
@Override public int getUsagesCount() { return myUsageNodes.size(); }