commit 6138e26c0c6932c1f0b2df72d082982ee5ae3059 Author: DonRenando Date: Wed Jan 18 10:43:17 2017 +0100 Initial commit diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..217af47 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/description.html b/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/libraries/ivy_java_1_2_17.xml b/.idea/libraries/ivy_java_1_2_17.xml new file mode 100644 index 0000000..dd16ac4 --- /dev/null +++ b/.idea/libraries/ivy_java_1_2_17.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..de8f7c7 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d995bea --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Projet_IHM.iml b/Projet_IHM.iml new file mode 100644 index 0000000..94e0193 --- /dev/null +++ b/Projet_IHM.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/ivy-java-1.2.17.jar b/lib/ivy-java-1.2.17.jar new file mode 100644 index 0000000..cea37f2 Binary files /dev/null and b/lib/ivy-java-1.2.17.jar differ diff --git a/src/Fenetre.java b/src/Fenetre.java new file mode 100644 index 0000000..a9ceed5 --- /dev/null +++ b/src/Fenetre.java @@ -0,0 +1,79 @@ +import fr.dgac.ivy.IvyException; + +import javax.swing.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; + +/** + * Created by renando on 16/01/17. + */ +class Fenetre extends JFrame { + private ivyTranslater monIvy; + + //private Automate a; + + + Fenetre(Panel panel) { + super("IHM TP Lines"); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + Panel panel1 = panel; + this.add(panel); + try { + this.monIvy = new ivyTranslater(); + } catch (IvyException e) { + e.printStackTrace(); + } + panel.addMouseListener(new MouseListener() { + @Override + public void mouseClicked(MouseEvent mouseEvent) { + } + + + @Override + public void mousePressed(MouseEvent mouseEvent) { + if (SwingUtilities.isLeftMouseButton(mouseEvent)) + panel.getStroke().addPoint(mouseEvent.getX(), mouseEvent.getY()); + panel.stroke = new Stroke(); + } + + @Override + public void mouseReleased(MouseEvent mouseEvent) { + monIvy.sendMsg("coord=" + panel.getStroke().toString()); + } + + + @Override + public void mouseEntered(MouseEvent mouseEvent) { + } + + @Override + public void mouseExited(MouseEvent mouseEvent) { + } + }); + + panel.addMouseMotionListener(new MouseMotionListener() { + + @Override + public void mouseDragged(MouseEvent mouseEvent) { + if (SwingUtilities.isLeftMouseButton(mouseEvent)) + panel.getStroke().addPoint(mouseEvent.getX(), mouseEvent.getY()); + panel.repaint(); + + } + + @Override + public void mouseMoved(MouseEvent mouseEvent) { + } + + }); + + + setSize(600, 600); + setVisible(true); + + + } + + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..0ec37de --- /dev/null +++ b/src/Main.java @@ -0,0 +1,10 @@ +/** + * Created by renando on 18/01/17. + */ +public class Main { + public static void main(String[] args) { + + Panel panel = new Panel(); + new Fenetre(panel); + } +} diff --git a/src/Panel.java b/src/Panel.java new file mode 100644 index 0000000..818acc3 --- /dev/null +++ b/src/Panel.java @@ -0,0 +1,40 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.geom.Point2D; + +/** + * Created by renando on 16/01/17. + */ +public class Panel extends JComponent { + + + public Stroke stroke; + + public Panel() { + super(); + stroke = new Stroke(); + } + + @Override + protected void paintComponent(Graphics graphics) { + super.paintComponent(graphics); + Graphics2D g2 = (Graphics2D) graphics; + + for (Point2D.Double p : stroke.getPoints()) { + int x = (int) p.getX(); + int y = (int) p.getY(); + g2.drawOval(x, y, 2, 2); + + } + + } + + public Stroke getStroke() { + return stroke; + } + + public void setStroke(Stroke stroke) { + this.stroke = stroke; + } + +} diff --git a/src/Stroke.java b/src/Stroke.java new file mode 100644 index 0000000..965d75b --- /dev/null +++ b/src/Stroke.java @@ -0,0 +1,202 @@ +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.util.ArrayList; + +public class Stroke { + private static final int NB_POINTS = 32; + private static final int SQUARE_SIZE = 100; + + ArrayList listePoint; + Point2D.Double centroid; + + public Stroke() { + listePoint = new ArrayList(); + } + + public void init() { + listePoint = null; + centroid = null; + listePoint = new ArrayList(); + } + + public boolean isEmpty() { + return listePoint == null || listePoint.size() == 0; + } + + public int size() { + return listePoint.size(); + } + + public Point2D.Double getPoint(int i) { + return listePoint.get(i); + } + + public Point2D.Double getCentroid() { + return centroid; + } + + public double getCentroidX() { + return centroid.x; + } + + public double getCentroidY() { + return centroid.y; + } + + public void addPoint(Point2D.Double p) { + listePoint.add(p); + } + + public void addPoint(int x, int y) { + listePoint.add(new Point2D.Double(x, y)); + } + + public void normalize() { + resample(); + rotateToZero(); + scaleToSquare(); + translateToOrigin(); + } + + public double getPathLength() { + double dist = 0.0; + for (int i = 1; i < listePoint.size(); i++) { + Point2D.Double p0 = listePoint.get(i - 1); + Point2D.Double p1 = listePoint.get(i); + dist += p0.distance(p1); + } + return dist; + } + + public ArrayList resample() { + double l = getPathLength() / (NB_POINTS - 1); + double D = 0; + ArrayList newPoints = new ArrayList(); + newPoints.add(listePoint.get(0)); + for (int i = 1; i < listePoint.size(); i++) { + Point2D.Double p0 = listePoint.get(i - 1); + Point2D.Double p1 = listePoint.get(i); + double d = p0.distance(p1); + if (D + d >= l) { + double x = p0.getX() + ((l - D) / d) * (p1.getX() - p0.getX()); + double y = p0.getY() + ((l - D) / d) * (p1.getY() - p0.getY()); + Point2D.Double q = new Point2D.Double(x, y); + newPoints.add(q); + listePoint.add(i, q); + D = 0; + } else { + D += d; + } + } + if (newPoints.size() < NB_POINTS) + newPoints.add(listePoint.get(listePoint.size() - 1)); + + listePoint = newPoints; + return newPoints; + } + + public Point2D.Double calculCentroid() { + double x = 0.0; + double y = 0.0; + for (int i = 0; i < listePoint.size(); i++) { + Point2D.Double p = listePoint.get(i); + x += p.getX(); + y += p.getY(); + } + return new Point2D.Double(x / listePoint.size(), y / listePoint.size()); + } + + public ArrayList rotateToZero() { + ArrayList newPoints = new ArrayList(); + centroid = calculCentroid(); + Point2D.Double p0 = listePoint.get(0); + double theta = Math.atan2(centroid.getY() - p0.getY(), centroid.getX() - p0.getX()); + theta *= -1; + for (int i = 0; i < listePoint.size(); i++) { + Point2D.Double p = listePoint.get(i); + double x = ((p.getX() - centroid.getX()) * Math.cos(theta)) - ((p.getY() - centroid.getY()) * Math.sin(theta)) + centroid.getX(); + double y = ((p.getX() - centroid.getX()) * Math.sin(theta)) + ((p.getY() - centroid.getY()) * Math.cos(theta)) + centroid.getY(); + Point2D.Double q = new Point2D.Double(x, y); + newPoints.add(q); + } + listePoint = newPoints; + return newPoints; + } + + public Rectangle2D.Double getBoundingBox() { + Point2D.Double p0 = listePoint.get(0); + double minX = p0.getX(); + double maxX = p0.getX(); + double minY = p0.getY(); + double maxY = p0.getY(); + for (int i = 0; i < listePoint.size(); i++) { + Point2D.Double p = listePoint.get(i); + if (minX > p.getX()) minX = p.getX(); + if (minY > p.getY()) minY = p.getY(); + if (maxX < p.getX()) maxX = p.getX(); + if (maxY < p.getY()) maxY = p.getY(); + } + return new Rectangle2D.Double(minX, minY, maxX - minX, maxY - minY); + } + + public ArrayList scaleToSquare() { + ArrayList newPoints = new ArrayList(); + Rectangle2D.Double boundingBox = getBoundingBox(); + System.out.println(boundingBox); + if (boundingBox.getHeight() > 10) { + for (int i = 0; i < listePoint.size(); i++) { + Point2D.Double p = listePoint.get(i); + double x = (p.getX() - boundingBox.getX()) * (SQUARE_SIZE / boundingBox.getWidth()); + double y = (p.getY() - boundingBox.getY()) * (SQUARE_SIZE / boundingBox.getHeight()); + Point2D.Double q = new Point2D.Double(x, y); + newPoints.add(q); + } + } + // Pour lisser une droite ... + else { + Point2D.Double p0 = listePoint.get(0); + double y = p0.getY(); + for (int i = 0; i < listePoint.size(); i++) { + Point2D.Double p = listePoint.get(i); + double x = (p.getX() - boundingBox.getX()) * (SQUARE_SIZE / boundingBox.getWidth()); + Point2D.Double q = new Point2D.Double(x, y); + newPoints.add(q); + } + } + listePoint = newPoints; + return newPoints; + + } + + public ArrayList translateToOrigin() { + ArrayList newPoints = new ArrayList(); + centroid = calculCentroid(); + for (int i = 0; i < listePoint.size(); i++) { + Point2D.Double p = listePoint.get(i); + double x = p.getX() - centroid.getX() + SQUARE_SIZE; + double y = p.getY() - centroid.getY() + SQUARE_SIZE; + Point2D.Double q = new Point2D.Double(x, y); + newPoints.add(q); + } + centroid = new Point2D.Double(SQUARE_SIZE, SQUARE_SIZE); + listePoint = newPoints; + return newPoints; + } + + public ArrayList getPoints() { + return listePoint; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (Point2D.Double p : listePoint) { + sb.append(p.getX()); + sb.append(","); + sb.append(p.getY()); + sb.append(";"); + } + sb.deleteCharAt(sb.lastIndexOf(";")); + return sb.toString(); + } +} diff --git a/src/ivyTranslater.java b/src/ivyTranslater.java new file mode 100644 index 0000000..b39992e --- /dev/null +++ b/src/ivyTranslater.java @@ -0,0 +1,57 @@ +import fr.dgac.ivy.Ivy; +import fr.dgac.ivy.IvyClient; +import fr.dgac.ivy.IvyException; +import fr.dgac.ivy.IvyMessageListener; + +class ivyTranslater implements IvyMessageListener { + + private final Ivy bus; + private String argument; + + ivyTranslater() throws IvyException { + // initialization, name and ready message + bus = new Ivy("IvyTranslater", "Geste", null); + // classical subscription + bus.bindMsg("sra5 Parsed=(.*) Confidence", this); + // inner class subscription ( think awt ) + bus.bindMsg("^Bye$", (client, args) -> { + // leaves the bus, and as it is the only thread, quits + bus.stop(); + }); + bus.start("127.255.255.255:2010"); // starts the bus on the default domain + } + + // callback associated to the "Hello" messages" + void recevoire(Panel panel) throws IvyException { + panel.stroke = new Stroke(); + bus.bindMsg("^coord=(.*)", (client, args) -> { + if (args[0] != null) { + panel.stroke = new Stroke(); + String[] listeCoor = args[0].split(";"); + String x; + String y; + for (String coor : listeCoor) { + x = coor.split(",")[0].replace(".0", ""); + y = coor.split(",")[1].replace(".0", ""); + panel.getStroke().addPoint(Integer.parseInt(x), Integer.parseInt(y)); + } + panel.repaint(); + + + } + }); + } + + void sendMsg(String msg) { + try { + bus.sendMsg(msg); + } catch (IvyException e) { + e.printStackTrace(); + } + } + + @Override + public void receive(IvyClient ivyClient, String[] strings) { + + } +} \ No newline at end of file