Skip to content

Instantly share code, notes, and snippets.

@dherges
Created February 17, 2014 12:53
Show Gist options
  • Save dherges/9049994 to your computer and use it in GitHub Desktop.
Save dherges/9049994 to your computer and use it in GitHub Desktop.
sms2graph
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
class sms2graph extends JFrame {
private static int WINDOW_X = 640;
private static int WINDOW_Y = 480;
private view view;
private controller ctrl;
private model model;
public sms2graph() {
// set frame
super("sms2graph - a graphical visualization of sudden motion sensor values");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
// init model, view & controller
this.model = new model();
this.view = new view(50);
this.ctrl = new controller();
// il cuore
new javax.swing.Timer(100, (ActionListener)this.model).start();
// paint the frame
this.add(this.view);
this.pack();
}
/**
* launches the application
*/
public static void main(String args[]) {
new sms2graph();
}
/**
* model - link between controller and view
*/
private class model implements ActionListener {
public void actionPerformed(ActionEvent e) {
int[] v = ctrl.getSMS(); // controller-daten sammeln
int x = (int)(-1.5 * v[0]) + WINDOW_X/2; // medienbruch ctrl -> view umrechnen
int y = (int)( 1.5 * v[1]) + WINDOW_Y/2;
view.setNewCoords(x, y); // daten an view
view.setRawCoords(v[0], v[1], v[2]);
view.repaint(); // view repainten
}
}
/**
* controller
*/
private class controller {
private int[] calibrate = new int[3];
public controller() {
calibrate = sms.Unimotion.getSMSArray();
}
public int[] getSMS() {
int[] a = sms.Unimotion.getSMSArray();
System.out.println("x " + a[0] + " - y " + a[1] + " - z " + a[2]);
a[0] = a[0] - calibrate[0];
a[1] = a[1] - calibrate[1];
a[2] = a[2] - calibrate[2];
return a;
}
}
/**
* view
*/
private class view extends JPanel {
private LinkedList<Point> history = new LinkedList<Point>();
private int rawX = 0, rawY = 0, rawZ = 0;
private int max_size = 50;
private int ovalsize = 6;
/**
* creates new view
* @param size number of values to be stored in history
*/
public view(int size) {
super(true);
this.setSize(new Dimension(WINDOW_X, WINDOW_Y));
this.setPreferredSize(this.getSize());
this.setMinimumSize(this.getSize());
max_size = size;
history.addFirst(new Point(WINDOW_X/2, WINDOW_Y/2));
history.addFirst(new Point(WINDOW_X/2+1, WINDOW_Y/2+1));
}
/**
* set new coordinates
*/
public void setNewCoords(int x, int y) {
history.addFirst(new Point(x, y));
if(history.size() > max_size) {
history.removeLast();
}
}
/**
* set raw data coordinates of recent point
*/
public void setRawCoords(int x, int y, int z) {
rawX = x;
rawY = y;
rawZ = z;
}
/**
* @Override
* repaints the view
*/
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setBackground(Color.BLACK);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, WINDOW_X, WINDOW_Y);
int size = history.size();
ListIterator<Point> li = history.listIterator(size);
Color c = new Color(255, 255, 255);
Point prev = li.previous();
Point next = null;
while(li.hasPrevious()) {
// pre loop
next = li.previous();
// do loop
g2d.setColor(c);
g2d.drawLine(prev.x, prev.y, next.x, next.y); // paint lines
// post loop
prev = next;
c = new Color(c.getRed() - 255/size, c.getGreen() - 255/size, c.getBlue() - 255/size);
}
g2d.setColor(Color.BLACK);
g2d.fillOval(prev.x-ovalsize/2, prev.y-ovalsize/2, ovalsize, ovalsize); // paint dot
g2d.drawString("x: " + rawX + ", y: " + rawY + ", z: " + rawZ, 3, WINDOW_Y-3);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment