CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2010
    Posts
    3

    Exclamation Basic c# question for my assignment

    Hi,

    I have to do an EtchaSketch program for my assignment. So far I have this code:

    --------------------------------------…

    public partial class Form1 : Form
    {
    int x = 50, y = 50; // starting position
    Bitmap bm;
    int x1 = 50, y1 = 50;

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    bm = new Bitmap(this.Width, this.Height); // create a form-size bitmap
    Graphics g = Graphics.FromImage(bm); // get a graphic object for the bitmap
    g.FillEllipse(Brushes.Blue, x, y, 20, 20); // put a circle in the bitmap
    this.BackgroundImage = bm; // use the bitmap as the form background
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
    Graphics h = e.Graphics; // get a graphic object for the bitmap
    h.FillEllipse(Brushes.Blue, x1, y1, 20, 20);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    string input;
    input = keyData.ToString();

    if (input == "Down")
    {
    y1 = y1 + 10;
    Refresh();
    return true;

    }

    return false; // return true if key processed, otherwise false
    }

    --------------------------------------…

    At the moment when I press the down key, the second circle moves down the screen.

    How do I make it so that when I press down, it forms a line (so basically makes a new circle and keeps the previous circle where it was), just like the etchasketch game.

    Thanks

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Basic c# question for my assignment

    You need to build up a list of the points that the circle as been to. Then in your Paint event handler, rather than just drawing the ellipse at x1, y1, you loop around all of the points drawing an ellipse at each....
    Code:
    // class members
    private List<Point> _points = new List<Point>();
    private Int32 _x = 50;
    private Int32 _y = 50;
    
    // paint handler
    
    Graphics graphics = e.Graphics;
    
    foreach (Point point in _points)
        graphics.FillEllipse(Brushes.Blue, point.X, point.Y, 20, 20)
    
    // in your process command...
    if (input == "Down")
    {
        _y += 10;
        
        // add the new point to the list
        _points.Add(new Point(_x, _y);
    
        // ... as before
    }
    You can now also easily implement a [Shake] button by simply calling:

    _points.Clear();

    and then refreshing....
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured