BlueMagma3141
Member
- Joined
- Jun 29, 2019
- Messages
- 6
- Programming Experience
- 1-3
I am fairly new to using C# and I am creating applications using Gtk in MonoDevelop. I can make simple static drawings, but I am unable to create a loop of drawing multiple objects.
I currently have an interval set up using System.Timers to draw every sixtieth of a second. This is being used to simply move a rectangle and bounce it off the edges. However, it seems that nothing is drawn after Application.Run() is called.
I currently have an interval set up using System.Timers to draw every sixtieth of a second. This is being used to simply move a rectangle and bounce it off the edges. However, it seems that nothing is drawn after Application.Run() is called.
Main Method:
public static void Main()
{
Application.Init();
Window w = new Window("Drawing");
w.SetDefaultSize(width, height);
canvas = new DrawingArea();
canvas.ExposeEvent += ExposeHandler;
canvas.SizeAllocated += SizeAllocatedHandler;
SetupPensBrushes();
Box box = new HBox(true, 0)
{
canvas
};
w.Add(box);
w.ShowAll();
w.DeleteEvent += Quit;
Application.Run();
}
Other useful code:
public static void ExposeHandler(object obj, ExposeEventArgs args)
{
ev = args.Event;
window = ev.Window;
ctx = Gtk.DotNet.Graphics.FromDrawable(window);
FillRect(new SolidBrush(Color.Black), 0, 0, width, height); // (Custom function for a rectangle) This works and creates a black background
interval.Interval = 1000 / 60;
interval.Elapsed += ElapsedEvt;
interval.Start(); // 'ElapsedEvt' includes moving and drawing an object, but it cannot be seen on the screen.
}
public class Obj
{
public int x;
public int y;
public int w;
public int h;
public int xv = rnd.Next(-width / 50, width / 50);
public int yv = rnd.Next(-height / 50, height / 50);
public Obj(int x, int y, int w, int h)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
public void Move()
{
x += xv;
y += yv;
}
public void Show()
{
FillRect(Brushes["LightBlue"], x, y, w, h);
}
}
public static void ElapsedEvt(object sender, ElapsedEventArgs e)
{
boxObj.Move();
boxObj.Show();
// boxObj has been defined already as a new Obj()
}