2 qq 28846727 qq_28846727 于 2015.06.08 10:55 提问

C#用Graphics 定义一个Mycircle类画圆,如何在类外改变画笔颜色再画一个圆

class MyCircle
{
int x; int y; int r;
public MyCircle(int x, int y, int r) { this.x = x; this.y = y; this.r = r; }
public override void Draw(Graphics g)
{
Random rd = new Random();
int red, blue, green;
red = rd.Next(255);
blue = rd.Next(255);
green = rd.Next(255);
Pen pen = new Pen(Color.FromArgb(red, blue, green), 2);
g.DrawEllipse(pen, x, y, r, r);
}
private void button1_Click(object sender, EventArgs e)
{
for (int s = 30; s <= 600; s++)
{
shapes = new MyCircle(x, s, r);
shapes.Draw(g);
System.Threading.Thread.Sleep(10);
在这里重绘~~

                        }
        }

5个回答

caozhy
caozhy   Ds   Rxr 2015.06.08 10:58
已采纳

你的代码逻辑不对,你应该让Paint事件去画,而不是在Button里画,道理很简单,即便你在按钮事件中画了,如果窗体被遮挡、最小化等等,画上去的东西就没有了。

qq_28846727
qq_28846727 在paint事件中怎么画呀,我是个新手0.0
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.06.08 11:03

MyCircle是一个控件还是一个窗体?

qq_28846727
qq_28846727 我截个图 代码是这样的。
2 年多之前 回复
qq_28846727
qq_28846727 是自己定义的类,在picturebox上画圆
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.06.08 11:07

那你重写的是哪里的Draw呢

qq_28846727
qq_28846727 就是在if(type==0)下面哪里的for循环中画一个圆,在画一个相同但不同颜色的圆覆盖掉
2 年多之前 回复
qq_28846727
qq_28846727   2015.06.08 11:10
  1. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace 项目训练1_几何世界_
{
public partial class Form1 : Form
{
bool drawing;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
drawing = false;
}

    private void button1_Click(object sender, EventArgs e)
    {
        if (drawing) { timer1.Stop(); drawing = false; }
        else { timer1.Start(); drawing = true; }


    }

    private void timer1_Tick(object sender, EventArgs e)
    {
         MyShapes  shapes = new MyShapes();
        Graphics g =pictureBox1.CreateGraphics();
        int x, y, r; int i, j, k,h;
        Random rd = new Random();
        x = rd.Next(1200);
        y = rd.Next(100,300);
        r = rd.Next(200);
        i = rd.Next(40, 80);
        j = rd.Next(20, 60);
        k = rd.Next(20, 40);
        h = rd.Next(20);
        int type;
        type = rd.Next(5);
        if (type == 0)
        {
            for (int s = 30; s <= 600; s++)
           {
                shapes = new MyCircle(x, s, r);
                shapes.Draw(g);
                System.Threading.Thread.Sleep(10);
              //  Pen pen = new Pen(Color.White,2);






                //g.TranslateTransform(200, 500);
                //g.Clear(BackColor);
            }

        }
        if (type == 1)
        {
            shapes = new MyRectangle(x, 100, 40+ rd.Next(200), 40 + rd.Next(200));
            shapes.Draw(g);
        }
        if (type == 2)
        {
            shapes = new MyTri(x, 150, x + rd.Next(50,100), y -50- rd.Next(50,100), x+50 + rd.Next(50,100), y -rd.Next(60,100));
            shapes.Draw(g);
        }
        if (type == 3)
        {
            shapes = new MySineWave(x,y,x+rd.Next(40),y+rd.Next(40));
            shapes.Draw(g);
        }
        if (type == 4)
        {
            shapes = new MyTrapezoid(60 + x, y-2*k, 60 + x + i, y-2*k, 60+i+x +2*j, y-k, x -j-h, y-k);
            shapes.Draw(g);

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Graphics g = pictureBox1.CreateGraphics();
        g.Clear(BackColor);
    }

}

}

qq_28848705
qq_28848705   2015.06.08 11:44

for (int i = 0; i < 10; i++)
{
f.Add(a.get(i * 3));
f.Add(a.get(i * 3 + 1));
f.Add(a.get(i * 3 + 2));
f.Add(b.get(i * 2));
f.Add(b.get(i * 2 + 1));
f.Add(c.get(i));
}

Csdn user default icon
上传中...
上传图片
插入图片