Permalink
Browse files

refactoring

  • Loading branch information...
shabanovd committed Oct 8, 2012
1 parent f9eff30 commit 5b09cc36edc45f77830f0e364ce1a8bae4773055
@@ -27,6 +27,8 @@
*
*/
public interface Imageable {
+
+ public String getImageName(String ID);
public BufferedImage getImage(String ID);
}
@@ -66,6 +66,10 @@ public BufferedImage getImage(String ID) {
return image;
}
+ public String getImageName(String ID) {
+ return toString();
+ }
+
public String toString() {
return name;
}
@@ -37,6 +37,7 @@
import org.animotron.animi.cortex.MultiCortex;
import org.animotron.animi.cortex.Retina;
import org.animotron.animi.simulator.RectAnime;
+import org.animotron.animi.simulator.Stimulator;
/**
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
@@ -46,15 +47,13 @@
private static final long serialVersionUID = 3243253015790558286L;
- private WebcamPanel camView = null;
-
private JMenuItem miInit = null;
private JMenuItem miRun = null;
private JMenuItem miPause = null;
private JMenuItem miResume = null;
private JMenuItem miStop = null;
- protected static MultiCortex cortexs = null;
+ public static MultiCortex cortexs = null;
JDesktopPane desktop;
@@ -219,6 +218,7 @@ private void init() {
for (CortexZoneSimple zone : cortexs.zones) {
createFrame(zone, null);
}
+ stimulator.start();
// camView.resume();
@@ -386,7 +386,7 @@ public void stateChanged(ChangeEvent e) {
tools.add(panel);
}
- Imageable stimulator =
+ Stimulator stimulator =
new RectAnime(
Retina.WIDTH, Retina.HEIGHT, 50, 0.05,
new int[][] {
@@ -399,8 +399,8 @@ public void stateChanged(ChangeEvent e) {
);
//Create a new internal frame.
- protected void createFrame(Imageable simulator, String imageID) {
- VisualizeMatrix frame = new VisualizeMatrix( simulator, imageID );
+ protected void createFrame(Imageable imageable, String imageID) {
+ Visualizer frame = new Visualizer( imageable, imageID );
frame.setVisible(true); //necessary as of 1.3
desktop.add(frame);
try {
@@ -20,7 +20,6 @@
*/
package org.animotron.animi.gui;
-import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
@@ -33,7 +32,7 @@
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
-public class VisualizeMatrix extends JInternalFrame {
+public class Visualizer extends JInternalFrame {
private static final long serialVersionUID = -592610047528698167L;
@@ -43,8 +42,8 @@
private BufferedImage image = null;
- public VisualizeMatrix(Imageable simulator, String imageID) {
- super("V",
+ public Visualizer(Imageable simulator, String imageID) {
+ super(simulator.getImageName(imageID),
true, //resizable
true, //closable
false, //maximizable
@@ -54,16 +53,16 @@ public VisualizeMatrix(Imageable simulator, String imageID) {
BufferedImage img = simulator.getImage(imageID);
setSize(img.getWidth()+10, img.getHeight()+10);
- setOpaque(true);
- setDoubleBuffered(true);
+// setOpaque(true);
+// setDoubleBuffered(true);
getContentPane().add(canvas);
repainter = new Repainter(canvas, simulator, imageID);
repainter.start();
}
- public VisualizeMatrix(Imageable simulator) {
+ public Visualizer(Imageable simulator) {
this(simulator, null);
}
@@ -102,6 +101,8 @@ public void paint(Graphics g) {
}
private class Repainter extends Thread {
+
+ int frequency = 2;
Imageable simulator;
String imageID;
@@ -130,7 +131,7 @@ public void run() {
component.repaint();
}
-// Thread.sleep(1000 / frequency);
+ Thread.sleep(1000 / frequency);
} catch (Throwable e) {
}
}
@@ -24,14 +24,13 @@
import java.awt.Graphics;
import java.awt.image.BufferedImage;
-import org.animotron.animi.Imageable;
import org.animotron.animi.cortex.Retina;
/**
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
-public class Geometry implements Imageable {
+public class Geometry extends Stimulator {
private BufferedImage image;
@@ -23,13 +23,11 @@
import java.awt.*;
import java.awt.image.BufferedImage;
-import org.animotron.animi.Imageable;
-
/**
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
-public class OvalAnime implements Imageable {
+public class OvalAnime extends Stimulator {
private BufferedImage image;
private int[][] anime;
@@ -25,13 +25,11 @@
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
-import org.animotron.animi.Imageable;
-
/**
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
-public class RectAnime implements Imageable {
+public class RectAnime extends Stimulator {
private BufferedImage image;
private int[][] anime;
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2012 The Animo Project
+ * http://animotron.org
+ *
+ * This file is part of Animi.
+ *
+ * Animotron is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * Animotron is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of
+ * the GNU Affero General Public License along with Animotron.
+ * If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.animotron.animi.simulator;
+
+import static org.animotron.animi.gui.Application.cortexs;
+
+import java.awt.image.BufferedImage;
+
+import org.animotron.animi.Imageable;
+
+/**
+ * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
+ *
+ */
+public abstract class Stimulator implements Runnable, Imageable {
+
+ private int frequency = 60; // Hz
+
+ private long fps;
+ private long frame = 0;
+ private long t0 = System.currentTimeMillis();
+ private long count = 0;
+
+ private boolean run = true;
+
+ @Override
+ public void run() {
+ while (run) {
+ try {
+ if (paused) {
+ synchronized (this) {
+ this.wait();
+ }
+ }
+ final BufferedImage image = getImage(null);
+
+ if (cortexs != null && image != null) {
+ cortexs.retina.process(image);
+
+ if (cortexs.active) {
+ cortexs.cycle1();
+ cortexs.cycle2();
+ count++;
+ }
+ }
+
+ Thread.sleep(1000 / frequency);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ } finally {
+ frame++;
+ long t = System.currentTimeMillis();
+ long dt = t - t0;
+ if (dt > 1000) {
+ fps = 1000 * frame / dt;
+ frame = 0;
+ t0 = t;
+ }
+ }
+ }
+ }
+
+ private Thread th = null;
+ private volatile boolean paused = false;
+
+ public synchronized void start() {
+ if (th != null) return;
+
+ th = new Thread(this);
+ th.setDaemon(true);
+ th.start();
+ }
+
+ public void pause() {
+ if (paused) return;
+
+ paused = true;
+ }
+
+ public void resume() {
+ if (!paused) return;
+
+ synchronized (this) {
+ notifyAll();
+ }
+ paused = false;
+ }
+
+
+ @Override
+ public String getImageName(String ID) {
+ return this.getClass().getSimpleName();
+ }
+}
@@ -23,16 +23,14 @@
import java.awt.Dimension;
import java.awt.image.BufferedImage;
-import org.animotron.animi.Imageable;
-
import com.github.sarxos.webcam.*;
import com.github.sarxos.webcam.ds.openimaj.OpenImajDriver;
/**
* @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
*
*/
-public class Webcamera implements Imageable, WebcamListener {
+public class Webcamera extends Stimulator implements WebcamListener {
private Webcam webcam = null;

0 comments on commit 5b09cc3

Please sign in to comment.