Window Display
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
public partial class Form1 : Form
{
private Rectangle screen_rect;
private Dymetric action_class;
private bool CLICK_OCCURRED;
public Form1()
{
InitializeComponent();
screen_rect = Screen.PrimaryScreen.Bounds;
CLICK_OCCURRED = false;
action_class = new Dymetric(screen_rect.Width, screen_rect.Height);
}
private void Form1_Load(object sender, EventArgs e)
{
this.SetDesktopBounds(0, 0, screen_rect.Width, screen_rect.Height);
this.Text = "useOfMaths.com";
this.BackColor = Color.LightGray;
String path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
path = new Uri(path).LocalPath;
try
{
this.Icon = new Icon(path + "\\useOfMaths.ico");
}
catch(System.IO.FileNotFoundException ex)
{
}
Button response_btn = new Button();
response_btn.BackColor = Color.Magenta;
response_btn.ForeColor = Color.Blue;
response_btn.SetBounds(screen_rect.Width / 2 - 50, 5, 100, 40);
response_btn.Text = "Move";
response_btn.Click += new EventHandler(response_btn_Click);
this.Controls.Add(response_btn);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pencil = new Pen(Color.Black);
pencil.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
pencil.Width = 5;
e.Graphics.DrawLine(pencil, 0, 50, this.Width, 50);
pencil.Dispose();
Brush paint_brush = new SolidBrush(Color.Pink);
e.Graphics.FillRectangle(paint_brush, 0, 0, this.Width, 50);
paint_brush.Dispose();
action_class.decideAction(e, CLICK_OCCURRED);
CLICK_OCCURRED = false;
}
public void response_btn_Click(object sender, EventArgs e)
{
CLICK_OCCURRED = true;
this.Refresh();
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
}
}
Dymetric class
using System.Windows.Forms;
namespace Dymetric
{
class Dymetric
{
private MovingBody move_body;
private bool do_simulation;
public Dymetric(int width, int height)
{
move_body = new MovingBody(width, height);
do_simulation = false;
}
public void decideAction(PaintEventArgs e, bool click_check)
{
if (do_simulation && click_check)
{
move_body.inPlay(e);
do_simulation = false;
}
else
{
move_body.clearAndDraw(e);
do_simulation = true;
}
}
}
}
C# code for MovingBody class
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
class MovingBody
{
private int x, y, previous_x, previous_y;
private const int ballDIAMETER = 80;
private Bitmap offscreen_bitmap;
Graphics offscreen_g;
private Brush ball_colour, bg_colour;
public MovingBody(int screen_width, int screen_height)
{
ball_colour = new SolidBrush(Color.Yellow);
bg_colour = new SolidBrush(Color.LightGray);
offscreen_bitmap = new Bitmap(screen_width, screen_height - 55,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
offscreen_g = Graphics.FromImage(offscreen_bitmap);
offscreen_g.Clear(Color.LightGray);
previous_x = x = 10;
previous_y = y = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
public void clearAndDraw(PaintEventArgs e)
{
offscreen_g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER);
offscreen_g.FillEllipse(ball_colour, x, y, ballDIAMETER, ballDIAMETER);
previous_x = x;
previous_y = y;
Graphics gr = e.Graphics;
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
}
public void inPlay(PaintEventArgs e)
{
while (x < offscreen_bitmap.Width - ballDIAMETER)
{
clearAndDraw(e);
x += 10;
Thread.Sleep(50);
}
x = 10;
y = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
}
}
}