瀏覽代碼

Adds basic visualisation, model and control

* Main starts the program
* MainFrame contains the GUI
* SmartDevice is a smart home device
* Visualisation paints the SmartDevices
* Model stores the important parts of the simulation
* Connection models physical connections between devices
Andreas T. Meyer-Berg 6 年之前
父節點
當前提交
53b7b0d0bc

+ 41 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -0,0 +1,41 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.MainFrame;
+
+/**
+ * Main class which initialises and connects the different parts and starts the program
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class Main {
+	/**
+	 * Model of the smart home
+	 */
+	static Model m;
+	
+	/**
+	 * Visualisation of the smart home network
+	 */
+	static MainFrame v;
+	
+	/**
+	 * Controller of the pogram 
+	 */
+	static Controller c;
+	
+	/**
+	 * Starts the program
+	 * @param args will be ignored
+	 */
+	public static void main(String[] args) {
+		
+		m = new Model();
+		c = new Controller(m);
+	    v = new MainFrame(m, c);
+	    v.setVisible(true);
+
+	}
+
+}

+ 19 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -0,0 +1,19 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+
+/**
+ * Controller which allows interaction with the Model and Simulation
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class Controller {
+	/**
+	 * {@link Model} which stores the smart home model
+	 */
+	Model model;
+	
+	public Controller(Model model) {
+		this.model = model;
+	}
+}

+ 66 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Connection.java

@@ -0,0 +1,66 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Connection medium for SmartDevices, which connects two or more devices and allows communication between them
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class Connection {
+
+	/**
+	 * name of the connection
+	 */
+	private String name;
+	
+	/**
+	 * Devices connected by this Connection
+	 */
+	private List<SmartDevice> devices;
+
+	/**
+	 * Initializes a Connection with name and an empty devices list.
+	 * @param name
+	 */
+	public Connection(String name){
+		this.name = name;
+		this.devices = new ArrayList<SmartDevice>();
+	}
+	
+	/**
+	 * @return the name
+	 */
+	public String getName() {
+		return name;
+	}
+
+	/**
+	 * @param name the name to set
+	 */
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	/**
+	 * @return the devices
+	 */
+	public List<SmartDevice> getDevices() {
+		return devices;
+	}
+
+	/**
+	 * @param device the devices to add
+	 */
+	public void addDevice(SmartDevice device) {
+		this.devices.add(device);
+	}
+	
+	/**
+	 * @param device the devices to remove
+	 */
+	public void removeDevice(SmartDevice device) {
+		this.devices.remove(device);
+	}
+}

+ 67 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Model.java

@@ -0,0 +1,67 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Model of the smart home, which contains all the important parts of the simulation, like {@link SmartDevice} and their {@link Connection}.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class Model {
+
+	private List<SmartDevice> devices;
+	private List<Connection> connectionNetworks;
+	
+	/**
+	 * Initializes the Model, with 3 default devices for testing purposes
+	 */
+	public Model() {
+		
+		devices = new ArrayList<SmartDevice>();
+		connectionNetworks = new ArrayList<Connection>();
+		
+		SmartDevice A = new SmartDevice("SmartTV");		
+		A.setX(50);
+		A.setY(50);
+		SmartDevice B = new SmartDevice("SmartDoor");
+		B.setX(100);
+		B.setY(100);
+		SmartDevice C = new SmartDevice("SmartLight");
+		C.setX(50);
+		C.setY(100);
+		
+		addDevices(A);
+		addDevices(B);
+		addDevices(C);
+	}
+
+	/**
+	 * @return the connectionNetworks
+	 */
+	public List<Connection> getConnectionNetworks() {
+		return connectionNetworks;
+	}
+
+	/**
+	 * Adds network connection
+	 * @param connectionNetwork the connectionNetwork to add
+	 */
+	public void addConnectionNetwork(Connection connectionNetwork) {
+		this.connectionNetworks.add(connectionNetwork);
+	}
+
+	/**
+	 * @return the devices
+	 */
+	public List<SmartDevice> getDevices() {
+		return devices;
+	}
+
+	/**
+	 * @param device the smartDevice to add to the smart home
+	 */
+	public void addDevices(SmartDevice device) {
+		this.devices.add(device);
+	}
+}

+ 121 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SmartDevice.java

@@ -0,0 +1,121 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Model of an SmartDevice, which keeps track of its main attributes, like name
+ * and connections
+ * 
+ * @author Andreas T. Meyer-Berg
+ */
+public class SmartDevice {
+
+	/**
+	 * Name of the Device
+	 */
+	private String name;
+
+	/**
+	 * Connections to other SmartDevices
+	 */
+	private List<Connection> connections;
+
+	/**
+	 * Creates a new SmartDevice without connections
+	 * 
+	 * @param name
+	 *            name of the device
+	 */
+	public SmartDevice(String name) {
+		this.name = name;
+		connections = new ArrayList<Connection>();
+	}
+
+	/**
+	 * Position on the x-Axis
+	 */
+	private int x;
+	
+	/**
+	 * Positon on the y-Axis
+	 */
+	private int y;
+	
+	/**
+	 * Position on the z-Axis
+	 */
+	private int z;
+	
+	/**
+	 * @return the name
+	 */
+	public String getName() {
+		return name;
+	}
+
+	/**
+	 * @param name
+	 *            the name to set
+	 */
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	/**
+	 * @return the connections of this device
+	 */
+	public List<Connection> getConnections() {
+		return connections;
+	}
+
+	/**
+	 * @param connection
+	 *            the connection to add
+	 */
+	public void addConnection(Connection connection) {
+		this.connections.add(connection);
+	}
+
+	/**
+	 * @return the x
+	 */
+	public int getX() {
+		return x;
+	}
+
+	/**
+	 * @param x the x to set
+	 */
+	public void setX(int x) {
+		this.x = x;	}
+
+	/**
+	 * @return the y
+	 */
+	public int getY() {
+		return y;
+	}
+
+	/**
+	 * @param y the y to set
+	 */
+	public void setY(int y) {
+		this.y = y;
+	}
+
+	/**
+	 * @return the z
+	 */
+	public int getZ() {
+		return z;
+	}
+
+	/**
+	 * @param z the z to set
+	 */
+	public void setZ(int z) {
+		this.z = z;
+	}
+
+}

+ 100 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/MainFrame.java

@@ -0,0 +1,100 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import java.awt.Dimension;
+import java.awt.Toolkit;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+import javax.swing.JFrame;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+
+import java.awt.BorderLayout;
+
+import javax.swing.JPanel;
+import javax.swing.SwingConstants;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+
+/**
+ * Main class of the GUI, which combines the UserInterface and visualisation of the simulation.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+@SuppressWarnings("serial")
+public class MainFrame extends JFrame {
+	
+	private final JPanel panel; 
+	
+	/**
+	 * Model of the smart home
+	 */
+	private Model model;
+	private Controller control;
+	
+	/**
+	 * Creates a new Frame for the program, which is the most outer frame of the application
+	 * @param m Model which is represented
+	 * @param c Controller which handles the user interaction
+	 */
+	public MainFrame(Model m, Controller c) {
+		
+		setTitle("Smart Home Network Simulator");
+		
+		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+		getContentPane().setLayout(new BorderLayout(0, 0));
+		panel = new VisualisationPanel(m);
+		getContentPane().add(panel, BorderLayout.CENTER);
+		
+		try {
+			/**
+			 * Icon of the MainFrame UI
+			 */
+			this.setIconImage(ImageIO.read(new File("src/main/ressources/images/SmartHomeNetworkSim_icon.jpeg")));
+		} catch (IOException e) {
+			System.err.println("WARNING: Failed to load Icon image in MainFrame.java");
+			e.printStackTrace();
+		}
+		
+		JMenuBar menuBar = new JMenuBar();
+		getContentPane().add(menuBar, BorderLayout.NORTH);
+		
+		JMenuItem mntmOptions = new JMenuItem("Options");
+		mntmOptions.setHorizontalAlignment(SwingConstants.LEFT);
+		menuBar.add(mntmOptions);
+		
+		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+		int width = screenSize.width; 
+		int height = screenSize.height;
+		this.setBounds(width/8, height/8, 6*width/8, 6*height/8);
+		this.setMinimumSize(new Dimension(640, 480));
+	}
+
+	/*
+	public MainFrame() {
+		super();
+		this.setTitle("Smart Home Network Simulator");
+		this.setBackground(Color.WHITE);
+		try {
+			/**
+			 * Icon of the MainFrame UI
+			 *//*
+			this.setIconImage(ImageIO.read(new File("src/main/ressources/images/SmartHomeNetworkSim_icon.jpeg")));
+		} catch (IOException e) {
+			System.err.println("WARNING: Failed to load Icon image in MainFrame.java");
+			e.printStackTrace();
+		}
+		this.setBounds(0, 0, 1000, 1000);
+		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
+		JMenuBar menu = new JMenuBar();
+		menu.add(new JMenuItem("Test1"));
+		menu.add(new JMenuItem("Test2"));
+		menu.add(new JMenuItem("Test3"));
+		menu.setVisible(true);
+		this.add(menu);
+		this.add(new VisualisationPanel());
+		
+	}*/
+}

+ 76 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationPanel.java

@@ -0,0 +1,76 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseMotionListener;
+
+import javax.swing.JPanel;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+
+@SuppressWarnings("serial")
+public class VisualisationPanel extends JPanel {
+
+	/**
+	 * Smart Home model which is visualized
+	 */
+	private Model model;
+	
+	/**
+	 * Initializes the Visualisation Panel
+	 * @param model Model to visualize
+	 */
+	public VisualisationPanel(Model model) {
+		super();
+		this.model = model;
+		this.addMouseMotionListener(new MouseMotionListener() {
+			
+			@Override
+			public void mouseMoved(MouseEvent e) {
+				if( e != null && e.getPoint() != null){
+					SmartDevice move = model.getDevices().get(0);
+					if(e.getX()>15 && e.getX()<getWidth()-16)
+						move.setX(e.getX());
+					if(e.getY()>15 && e.getY()<getHeight()-16)
+						move.setY(e.getY());
+					repaint();
+				}
+				
+			}
+			
+			@Override
+			public void mouseDragged(MouseEvent e) {
+				// TODO Auto-generated method stub
+				
+			}
+		});
+	}
+	
+	@Override
+	public void paint(Graphics g) {
+		super.paint(g);
+		//paint white background
+		g.setColor(Color.white);
+		g.fillRect(0, 0, this.getWidth(), this.getHeight());
+		
+		paintDevices(g);
+	}
+	
+	/**
+	 * Paints the smart devices of the Model
+	 * @param g
+	 */
+	public void paintDevices(Graphics g){
+		
+		for(SmartDevice s:model.getDevices()){
+			g.setColor(Color.BLACK);
+			g.drawOval(s.getX()-16, s.getY()-16, 32, 32);
+			g.setColor(Color.BLUE);
+			g.drawOval(s.getX()-14, s.getY()-14, 28, 28);
+			g.setColor(Color.BLACK);
+			g.drawString(s.getName(), s.getX()-g.getFontMetrics().stringWidth(s.getName())/2, s.getY()+26);
+		}
+	}
+}

二進制
src/main/ressources/images/SmartHomeNetworkSim_icon.jpeg