-1

Hi I've been given the task of converting a java application to a c# windows form application. The program displays a Mandelbrot which then allows the user to zoom into. I've managed to display the Mandelbrot and even zoom. However when dragging a box to zoom, the box its self doesn't show, meaning the user cannot see what area they will be zooming into.

I believe I need to call the update function which draws the rectangle as I drag Here is the code that I believe is relevant.

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    //e.consume();
    if (action)
    {
        xs = e.X;
        ys = e.Y;
    }
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    int z, w;

    //e.consume();
    if (action)
    {
        xe = e.X;
        ye = e.Y;
        if (xs > xe)
        {
            z = xs;
            xs = xe;
            xe = z;
        }
        if (ys > ye)
        {
            z = ys;
            ys = ye;
            ye = z;
        }
        w = (xe - xs);
        z = (ye - ys);
        if ((w < 2) && (z < 2))
            initvalues();
        else
        {
            if (((float)w > (float)z * xy))
                ye = (int)((float)ys + (float)w / xy);
            else
                xe = (int)((float)xs + (float)z * xy);
            xende = xstart + xzoom * (double)xe;
            yende = ystart + yzoom * (double)ye;
            xstart += xzoom * (double)xs;
            ystart += yzoom * (double)ys;
        }
        xzoom = (xende - xstart) / (double)x1;
        yzoom = (yende - ystart) / (double)y1;
        mandelbrot();
        rectangle = false;

        Refresh();
    }
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    //e.consume();
    if (action)
    {
        xe = e.X;
        ye = e.Y;
        rectangle = true;
    }
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g1 = e.Graphics;
    g1.DrawImage(bitmap, 0, 0, x1, y1);
    g1.Dispose();
}

public void paint(Graphics g1)
{
    update(g1);
}

public void update(Graphics g1)
{
    Pen pen = new Pen(Color.White);
    g1.DrawImage(bitmap, 0, 0, x1, y1);
    if (rectangle)
    {

        if (xs < xe)
        {
            if (ys < ye) g1.DrawRectangle(pen, xs, ys, (xe - xs), (ye - ys));
            else g1.DrawRectangle(pen, xs, ye, (xe - xs), (ys - ye));
        }
        else
        {
            if (ys < ye) g1.DrawRectangle(pen, xe, ys, (xs - xe), (ye - ys));
            else g1.DrawRectangle(pen, xe, ye, (xs - xe), (ys - ye));
        }
        pen.Dispose();

    }
}
Dmitry
  • 13,797
  • 6
  • 32
  • 48
Dave Jones
  • 53
  • 10
  • 2
    You guys in the same class ought to talk to each other. This is a duplicate of a question your classmate asked: http://stackoverflow.com/questions/26721685/java-to-c-sharp-conversion-how-do-i-draw-a-rectangle-on-my-bitmap – Peter Duniho Nov 08 '14 at 00:20
  • I've had a look there, that didn't seem to help! – Dave Jones Nov 08 '14 at 00:31

1 Answers1

0

You need to call Form.Update() or Form.Refresh() from within your mousemove event handler. You also need to call the draw rectangle function within your Form1_Paint event handler. Then you'll be set.

DWCP
  • 136
  • 1
  • 4
  • When you say call the draw rectangle function do you mean to call update(); because that's where the draw rectangle function is, but that causes an error, or move the draw rectangle code from update into paint? – Dave Jones Nov 08 '14 at 00:38
  • You could either move the 'DrawRectangle' code into 'Form1_Paint', or call update from 'Form1_Paint'. What is important is that you draw the rectangle using the Graphics object from the 'PaintEventArgs' – DWCP Nov 08 '14 at 00:57
  • There's no need to use `Update()` or `Refresh()`. Just call `Invalidate()` when the UI needs to be redrawn, and then draw the rectangle in the `Paint` event handler. Which is, by the way, the answer given in the duplicate question. – Peter Duniho Nov 08 '14 at 04:52