using System; using Cairo; using Gdk; using Gtk; class Area : DrawingArea { private static int s = 2; // крок переміщення круга по горизонталі чи вертикалі public int r = 8, // радіус рухомого круга xmax=500, // розміри поля по горизонталі ymax=250, // розміри поля по вертикалі x, // поточна абсциса центра круга y; // поточна ордината центра круга public bool timer = true; // чи потрібна перемальовувати? protected override bool OnDrawn(Context cr) // малювання { cr.SetSourceRGB(0, 0, 0); cr.Paint(); cr.SetSourceColor(new Cairo.Color(0, 1, 0, 1)); cr.Arc(x, y, r, 0, 2 * Math.PI); cr.Fill(); return true; } public void Move() { x += s; // Прямоліний рівномірний рух праворуч timer = x < xmax; } } class OwnWindow : Gtk.Window { public Area a; public OwnWindow() : base("Взаємодія об'єктів") { a = new Area(); Resize(a.xmax, a.ymax); a.x = a.r; // поточна абсциса центра круга a.y = a.ymax/2; // поточна ордината центра круга DeleteEvent += delegate { Application.Quit(); }; GLib.Timeout.Add(20, new GLib.TimeoutHandler(OnTimer)); // Кожні 20 мс виклик методу OnTimer. Add(a); ShowAll(); } protected override bool OnDeleteEvent(Event e) { Application.Quit(); return true; } bool OnTimer() { if (!a.timer) return false; a.Move(); a.QueueDraw(); return true; } } class Example { static void Main() { Application.Init(); OwnWindow w = new OwnWindow(); Application.Run(); } }