Hi guys,
Im new to C# and am trying to make a little program where when i click a button it creates a rectangle that will bounce around the screen randomly and perhaps change colour (but the main thing is to make it bounce randomly first). I've got some code that draws a rectangle in a picturebox but I have no idea how to code the resulting rectangle to move around. Also I've been told I should put the rectangles in a separate class but I'm not sure if I should or not. Here's a snippet of my code that draws a rectangle at position 0,0 when i click a buttion "H".
namespace Lines1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.Drawing.Graphics g;
private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);
private voide button1_Click(object sender, EventArgs e)
{
g = PictureBox1.CreateGraphics();
g.DrawRectangle(pen1,0,0,30,30);
}
private voide PictureBox1_Click(object sender, EventArgs e)
{
}
}
}
I have code doing other things but this is all that draws a rectangle and its in the Form1 class.
Any help would be appreciated,
Thanks.
Edit: I should of mentioned im using visual studio 2010
Loading
Sam HobbsPosted Sep 28, 2011, 2:15 PM
You should read articles about graphics. This web site has many useful articles.
One of the important concepts is that in Windows, graphics must be drawn in a paint event. Sometimes we can draw a bitmap outside a paint event and sometimes the paint event is called something else, but you need to understand about how the paint event works. Read articles about that.
One possible solution is to use ControlPaint.DrawReversibleLine Method to draw the lines, or something liek that. Then it is easy to undraw the lines so it can be redrawn elsewhere. Graphics is not my specialty, so you need to read articles written by people with more experience. I think this web site even has a free book about graphics.
Rakesh JhaPosted Sep 28, 2011, 6:00 AM
Does it required that you do it on server side. i mean you can do it through JavaScript. Use a div & timer control and bounce.
in timer function, use random manipulation for div hight width and it will looks like animation.
You can also do it in you own ways. i mean you already created rectangle just use timer control and in timer method, manipulate top, height, left position.