using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Xml; using System.Collections; using System.Threading; using Mobile_Robot; namespace Mobile_Robot_Form { public partial class MobileRobotForm : Form { private int panelWidth, panelHeight; private ArrayList obstacles; private Environment env; private Thread robotControlThread; private Robot robot; private void startRobotControllerThread(object obj) { Graphics g = panel1.CreateGraphics(); robot = new Robot("ROBOT1", new Position(80, 100, 0)); robot.paint(new SolidBrush(Color.Gray), g); for (int i = 0; i < 100; i++) { robot.GlobalPosition.rotateAndTranslate(robot.GlobalPosition, (2.0*Math.PI)/5000.0, 0.1, 0.0); robot.paint(new SolidBrush(Color.Gray), g); Thread.Sleep(100); robot.paint(new SolidBrush(panel1.BackColor), g); } } private void loadMapToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.InitialDirectory = "Map Files"; openFileDialog1.Filter = "xml files (*.xml) |" + "*.xml|" + "All files (*.*) |" + "*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { String fileName = openFileDialog1.FileName; if (fileName.Length != 0) { try { parseXML(fileName); env = new Environment(obstacles); panel1.Invalidate(); robotControlThread.Start(); } catch (ArgumentException ex) { MessageBox.Show(String.Format( "{0} is not a valid xml file", fileName)); } } } } private void panel1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (env != null) foreach (Obstacle o in env.Obstacles) o.paintObstacle(sender, e); } public MobileRobotForm() { InitializeComponent(); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); panel1.BackColor = Color.White; // Set the scale factor for drawing the map panelWidth = panel1.Size.Width; panelHeight = panel1.Size.Height; // List of obstacles obstacles = new ArrayList(); // Initialize lift threads robotControlThread = new Thread(new ParameterizedThreadStart(startRobotControllerThread)); } public bool parseXML(String xmlFilename) { XmlTextReader reader = new XmlTextReader(xmlFilename); Obstacle obs = null; ArrayList vertices = null; while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an element. if (reader.Name.Equals("OBSTACLE")) { obs = new Obstacle(); vertices = new ArrayList(); while (reader.MoveToNextAttribute()) // Read the attributes. { if (reader.Name.Equals("NAME")) obs.Name = reader.Value; if (reader.Name.Equals("OPAQUE")) { if (reader.Value.Equals("true")) obs.Opaque = true; else obs.Opaque = false; } } } else if (reader.Name.Equals("POINT")) { Point p = new Point(); while (reader.MoveToNextAttribute()) // Read the attributes. { if (reader.Name.Equals("X")) { p.X = Convert.ToInt32(reader.Value); } else if (reader.Name.Equals("Y")) { p.Y = Convert.ToInt32(reader.Value); } } vertices.Add(p); } break; case XmlNodeType.EndElement: //Display the end of the element. if (reader.Name.Equals("OBSTACLE")) { obs.Vertices = vertices; obstacles.Add(obs); } break; } } return true; } } public class Environment { private ArrayList obstacles; public Environment(ArrayList os) { obstacles = os; } public ArrayList Obstacles { get { return obstacles; } } } public class Obstacle { private String name; private Boolean opaque; private ArrayList vertices; public Obstacle() { } public Obstacle(String n, Boolean op, ArrayList vs) { name = n; opaque = op; vertices = vs; } public String Name { get { return name; } set { name = value; } } public ArrayList Vertices { get { return vertices; } set { vertices = value; } } public Boolean Opaque { get { return opaque; } set { opaque = value; } } public override String ToString() { String s = name + " " + opaque + " "; foreach (Point p in vertices) s += ("X= " + p.X + " Y= " + p.Y + " "); return s; } public void paintObstacle(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; Point[] vertexArray = vertices.ToArray(typeof(Point)) as Point[]; if (vertexArray.Length > 1) { if (!opaque) graphics.DrawPolygon(new Pen(Color.Black), vertexArray); else graphics.FillPolygon(new SolidBrush(Color.Black), vertexArray); } } } }