如何在同一个面板中绘制多个椭圆 [英] How to draw multiple ellipse in the same panel

查看:136
本文介绍了如何在同一个面板中绘制多个椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在同一个面板上绘制一些椭圆,并且通过鼠标点击确定协调器。这里是我的代码,这段代码只能绘制一个圆圈。较新的圆圈总是更新面板上较旧的圆圈。所以总是只有一个圆。

I am trying to draw some ellipse in the same panel, and the coordinators are determined by mouse click. Here is my code, this code can only draw one circle. The newer circle is always updating the older circle on the panel. So there is always only one circle.

private void panel1_MouseDown(object sender, MouseEventArgs e)
        {

            x = e.X;
            y = e.Y;
            panel1.Invalidate();
        }
        Graphics g;
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            g = panel1.CreateGraphics();

            g.FillEllipse(Brushes.Red, x,y, 10, 10);
        }


推荐答案

circle:

This will let you draw many circles:

List<Point> points = new List<Point>();   // List<T> is wonderful !

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
     points.Add(e.Location);
     panel1.Invalidate();
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
     g = e.Graphics;    // only ever use this one for persistent graphics!!
     foreach( Point pt in points)
        g.FillEllipse(Brushes.Red, pt.X, pt.Y,  10, 10);
}

全部删除它们

points.Clear();

删除最后一个

Delete the last one by

points.Remove(points.Last());

对于其他尺寸商店列表< Rectangle> 代替。对于更复杂的绘图,您可以创建一个自己的 DrawAction 类来存放笔,颜色,甚至旋转和其他形状等。

For other sizes store List<Rectangle> instead. For more complex drawing create a DrawAction class of your own to hold pens, colors or even rotations and other shapes etc..

这篇关于如何在同一个面板中绘制多个椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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