的Java到C#转换。我如何动用我的位图的矩形? [英] Java to C# conversion. How do i draw a rectangle on my bitmap?

查看:131
本文介绍了的Java到C#转换。我如何动用我的位图的矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我在C#和一个完整的小白爪哇。

所以,我一直在考虑这个任务转换的Java小程序到C#,我已成功地从通过拖放在屏幕上绘制一个矩形分开做的一切,并使用鼠标事件下降。

请告诉我应该发生的是,当我点击并拖动我的鼠标在屏幕上的矩形无填充和白色边框应该会出现。在codeI低于仅仅是一个白色的屏幕,通过它一个红叉,如果我在form1_Paint注释掉如果(动作)语句,然后它的工作原理,但没有矩形所以它必须是code,它搞乱它了。

http://gyazo.com/b2506b8c2ea9b304e34172c42ce98aab < - 它应该是什么样子。

http://gyazo.com/a8764ac9f5380f0109623d7a7750ddb6 < - 它实际上看起来像

[更新]

我现在已经有一个矩形做显示器,但它发生在MouseUp事件,而不是创建它,因为我拖我的鼠标。最明显的下一步是将其移动到喜欢的mouseMove不同的鼠标事件,但那就真的食堂和创建矩形不断为我使它更大。我怎样才能使它不断调整矩形的大小,因为我拖我的鼠标和跟不上创建矩形不断?
在code

 私人无效Form1_Paint(对象发件人,PaintEventArgs的E)
    {
        图形G1 = e.Graphics;
        g1.DrawImage(位图,0,0,X1,Y1);    }
    //添加加载方法
    私人无效Form1_Load的(对象发件人,EventArgs五)//运行在负载功能
    {
        在里面();
        开始();    }
    私人无效Form1_MouseMove(对象发件人,MouseEventArgs E)
    {
        如果(动作)
        {
            XE = e.X;
            你们= e.Y;        }
    }    私人无效Form1_MouseDown(对象发件人,MouseEventArgs E)
    {
        行动=真;
        // e.consume();
        XS = XE = e.X;
        YS = =你们e.Y; //起点Y
         Form1_MouseMove(发件人,E);
         this.Invalidate();    }    私人无效Form1_MouseUp(对象发件人,MouseEventArgs E)
    {
          使用(图形G = this.CreateGraphics())
            {
                喷喷=新笔(Color.White);
                g.DrawRectangle(笔,XS,YS,Math.Abs​​(XS - XE),Math.Abs​​(YS - 你们));            }        INT Z,W;
        //e.consume();            // XE = e.X;
            //你们= e.Y;
            如果(XS> XE)
            {
                Z = XS;
                XS = XE;
                XE = Z;
            }
            如果(YS>叶)
            {
                Z =伊苏;
                YS =你们;
                你们= Z;
            }
            W =(XE - XS);
            Z =(YE - YS);
            如果((W 2)及及(z,其中; 2))initvalues​​();
            其他
            {
                如果(((浮点)W>(浮点)Z * XY))=你们(INT)((浮点)YS +(浮点)W / XY);
                否则XE =(INT)((浮点)XS +(浮点)Z * XY);
                xende = XSTART + xzoom *(双)XE;
                yende = ystart + yzoom *(双)你们;
                XSTART + = xzoom *(双)XS;
                ystart + = yzoom *(双)YS;
            }
            xzoom =(xende - XSTART)/(双)×1;
            yzoom =(yende - ystart)/(双)Y1;
            曼德尔布罗();            this.Invalidate();
            //重画();    }


解决方案

在您的code而言,最大的问题是在 Form1_Paint()方法,这种说法:

  g1.Dispose();

您应该的从不的是处理传递给你的图形实例。它属于框架,而不是你的code。但是,你应该特别绝不处置,你打算以后使用的对象。当你在这里处理它,那么图形实例无效后,当您尝试绘制矩形上。

请注意,这是相同的Java。我希望原始的Java code不叫 Graphics.dispose()呢!

其他的一些建议:


  • 创建一个新的对象时,添加一个使用语句,以确保实例创建妥善处理(你自己的那一个!:))。在这种情况下,虽然,你并不需要创建一个新的对象...只是用股票 .NET提供。即 Pens.White

  • 您似乎没有被调用的Invalidate()的MouseDown 的MouseMove 事件处理程序。除非你做,因为Paint事件处理程序将不会被调用,你不会得到任何视觉反馈。

修复code,所以它看起来是这样的:

  //小助手方法:)
私有静态无效交换< T>(REF牛逼T1,裁判牛逼T2)
{
    ŧTEMP = T1;
    T1 = T2;
    T 2 = T;
}私人无效Form1_Paint(对象发件人,PaintEventArgs的E)
{
    图形G1 = e.Graphics;
    g1.DrawImage(位图,0,0,X1,Y1);    如果(动作)
    {
        //g.setColor(Color.White);
        如果(XE< XS)
        {
            掉期(REF XS,裁判XE);
        }        如果(你们< YS)
        {
            掉期(REF YS,你们参考);
        }        g1.DrawRectangle(Pens.White,XS,YS,(XE - XS),(你们 - YS));
    }
}私人无效Form1_MouseMove(对象发件人,MouseEventArgs E)
{
   // e.consume();
    如果(动作)
    {
        XE = e.X;
        你们= e.Y;
        无效();
        //重绘();
    }}私人无效Form1_MouseDown(对象发件人,MouseEventArgs E)
{
    行动=真;
    // e.consume();
    如果(动作)
    {
        XS = XE = e.X;
        YS = =你们e.Y;
        无效();
    }
}

Firstly, i am a complete noob at both C# and Java.

So i have been given this assignment to convert a java applet into C#, i have managed to do everything apart from drawing a rectangle on the screen via drag and drop using mouse events.

Whats supposed to happen is when i click and drag my mouse across the screen a rectangle with no fill and white border should appear. The code i have below is just a white screen with a red cross through it, if i comment out the if(action) statement in the form1_Paint then it works but no rectangle so it must be that code that messing it up.

http://gyazo.com/b2506b8c2ea9b304e34172c42ce98aab <-- what it should look like

http://gyazo.com/a8764ac9f5380f0109623d7a7750ddb6 <-- what it actually looks like

[update]

I have now got a rectangle do display but it happens on the MouseUp event rather than creating it as i am dragging my mouse. The obvious next step was to move it to a different mouse event like mouseMove but then it really messes up and created rectangles constantly as i make it bigger. How can i make it constantly resize the rectangle as i drag my mouse and not keep creating rectangles constantly? The code

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

    }
    //added load method
    private void Form1_Load(object sender, EventArgs e)//runs functions on load
    {
        init();
        start();

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

        }




    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        action = true;
        // e.consume();  
        xs = xe = e.X;
        ys = ye = e.Y; // starting point y
         Form1_MouseMove(sender, e);
         this.Invalidate();

    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
          using (Graphics g = this.CreateGraphics())
            {
                Pen pen = new Pen(Color.White);
                g.DrawRectangle(pen, xs, ys, Math.Abs(xs - xe), Math.Abs(ys - ye));

            }

        int z, w;  
        //e.consume();

            //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();

            this.Invalidate();
            //Repaint();



    }

解决方案

The biggest problem in your code is this statement in the Form1_Paint() method:

g1.Dispose();

You should never be disposing the Graphics instance passed to you. It belongs to the framework, not your code. But you should especially never dispose an object that you plan to use later. When you dispose it here, then the Graphics instance isn't valid later on when you try to draw the rectangle.

Note that this is the same as in Java. I hope the original Java code didn't call Graphics.dispose() too!

Some other suggestions:

  • when creating a new Pen object, add a using statement to ensure the Pen instance you create is disposed properly (you do own that one! :) ). In this case though, you don't need to create a new Pen object...just use the stock Pen provided by .NET. I.e. Pens.White.
  • you don't appear to be calling Invalidate() in the MouseDown and MouseMove event handlers. You won't get any visual feedback unless you do that, because the Paint event handler won't be called.

Fix the code so it looks like this:

// Little helper method :)
private static void Swap<T>(ref T t1, ref T t2)
{
    T temp = t1;
    t1 = t2;
    t2 = t1;
}

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

    if (action)
    {
        //g.setColor(Color.White);
        if (xe < xs)
        {
            Swap(ref xs, ref xe);
        }

        if (ye < ys)
        {
            Swap(ref ys, ref ye);
        }

        g1.DrawRectangle(Pens.White, xs, ys, (xe - xs), (ye - ys));
    }
}

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

}

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

这篇关于的Java到C#转换。我如何动用我的位图的矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆