当前位置:编程学习 > C#/ASP.NET >>

重绘问题,急需求解,在线等

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 自动演示皇后问题
{
    public partial class Form1 : Form
    {
        public int n;                   //输入的皇后个数
        public float x;                 //画表格的横坐标
        public float y;                 //画表格的纵坐标
        public float d;                 //矩形间的间距
        public bool flag;               //判断合法或部分着色的返回值
        public int k = 0;               //声明一个初始行数
        public int[] c = new int[20];   //声明一个数组,存放对应第k行的某一列
        private Image theImage;         //要绘图的图片对象
        private Graphics g;             //在图片上绘图的Graphics对象
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //timer1.Enabled = true;                      //设置时间控件循环

            string str = textBox1.Text;
            if (str != "")
            {
                try
                {
                    n = Int32.Parse(str);
                    show();                             //画表格
                    for (k = 0; k <= n - 1; k++)        //初始化行列
                        c[k] = 0;
                    k = 0;
                }
                catch                                   //异常处理
                {
                    timer1.Enabled = false;
                    MessageBox.Show("您的输入有误,请重新输入", "提示");
                    pictureBox1.Invalidate();
                    pictureBox1.Update();
                    textBox1.Clear();
                }


             }

                else
                {
                    MessageBox.Show("请先输入皇后个数", "消息框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }


            
        }
            
            

        private void show()                     //用bitmap直线画表格
        {
            theImage = new Bitmap(600, 800);//宽,高 
            g = Graphics.FromImage(theImage);
            g.Clear(Color.White);
            this.pictureBox1.Image = theImage;
            Pen BluePen = new Pen(Color.Black);

            //用直线画指定大小的矩形
            float x = 25.00F;
            float y = 25.00F;
            this.d = 300.00F / n;

            for (int i = 0; i <= n; i++)
            {
                PointF X1 = new PointF(x + i * d, y);
                PointF X2 = new PointF(x + i * d, y + 300.00F);
                g.DrawLine(BluePen, X1, X2);

            }
            x = 25.00F;
            for (int j = 0; j <= n; j++)
            {
                PointF Y1 = new PointF(x, y + j * d);
                PointF Y2 = new PointF(x + 300.00F, y + j * d);
                g.DrawLine(BluePen, Y1, Y2);
            }
            x = 25.00F;
            y = 25.00F;
            BluePen.Dispose();
        }

        public bool canput(int k) //判断合法着色
        {
            int i;
            for (i = 0; i < k; i++)
            { //如果k是最后一行,而且所有棋子不在同一列及同一对角线,则为合法着色
                if (k == n - 1 && c[k] != c[i] && c[i] - c[k] != i - k && c[i] - c[k] != k - i)
                {
                    flag = true;
                }
                else //否则为非法,跳出循环
                {
                    flag = false;
                    break;
                }
            }
            return flag; //返回真值
        }

        public bool partput(int k) //判断部分着色
        {
            int i;
            for (i = 0; i < k; i++)
            { //如果并没有摆放完N个棋子,而且满足摆放的棋子不在同一列及同一对角线,则为部分着色
                if (c[k] != c[i] && c[i] - c[k] != i - k && c[i] - c[k] != k - i)
                {
                    flag = true;
                }
                else   //否则为非法,跳出循环
                {
                    flag = false;
                    break;
                }
            }
            return flag;        //返回真值
        }
各位大哥,我是新手,我想在填充颜色的时候,窗口最小化的话,不会使颜色消失掉,但是不知道该怎么写这样的重绘,谢谢帮忙哈,还有部分代码,马上贴上来。 --------------------编程问答--------------------         private void button2_Click(object sender, EventArgs e)    //执行下一步按钮
        {
            if (timer1.Enabled == true)         //如果时间控件在循环,则提示用户
            {
                MessageBox.Show("请先按暂停键", "提示");
            }
            else                                //
            {
                Graphics gf = pictureBox1.CreateGraphics();//绘图对象

                SolidBrush myBrush = new SolidBrush(Color.Red); //画刷
                SolidBrush myBrush2 = new SolidBrush(Color.White);//画刷

                if (k >= 0)
                {
                    if (c[k] <= n - 1)
                    {
                        c[k] = c[k] + 1;
                        gf.FillRectangle(myBrush2, 25.00F + (c[k] - 2) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);


                        gf.FillRectangle(myBrush, 25.00F + (c[k] - 1) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);

                        if (k == 0) //第一行为部分着色
                        {
                            gf.FillRectangle(myBrush, 25.00F + (c[k] - 1) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);
                            k = k + 1;
                        }
                        else
                        {

                            if (canput(k)) //当为合法着色
                            {
                                MessageBox.Show("恭喜输出正确结果", "消息框");
                            }
                            else if (partput(k)) //当为部分着色
                            {
                                k = k + 1;
                            }
                        }
                    }
                    else
                    {
                        gf.FillRectangle(myBrush2, 25.00F + (c[k] - 1) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);
                        c[k] = 0;
                        k -= 1;
                    }
                }
                else
                {
                    if (n == 0 || n == 2 || n == 3)
                    {
                        MessageBox.Show("此问题无解", "提示");
                        timer1.Dispose();
                    }
                    else
                    {
                        MessageBox.Show("全部过程演示完毕", "提示");
                        timer1.Dispose();
                        return;
                    }
                }
                myBrush.Dispose();
                myBrush2.Dispose();
            }
        }
        /// <summary>
        /// 时间控件,执行与“下一步”按钮相同的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)        
        {
            Graphics gf = pictureBox1.CreateGraphics();
            SolidBrush myBrush = new SolidBrush(Color.Red);
            SolidBrush myBrush2 = new SolidBrush(Color.White);

            if (k >= 0)
            {
                if (c[k] <= n - 1)
                {
                    c[k] = c[k] + 1;
                    gf.FillRectangle(myBrush2, 25.00F + (c[k] - 2) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);


                    gf.FillRectangle(myBrush, 25.00F + (c[k] - 1) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);

                    if (k == 0) //第一行为部分着色
                    {
                        gf.FillRectangle(myBrush, 25.00F + (c[k] - 1) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);
                        k = k + 1;
                    }
                    else
                    {

                        if (canput(k)) //当为合法着色
                        {
                            timer1.Enabled = false;
                            MessageBox.Show("恭喜输出正确结果", "消息框");
                        }
                        else if (partput(k)) //当为部分着色
                        {
                            k = k + 1;
                        }
                    }
                }
                else
                {
                    gf.FillRectangle(myBrush2, 25.00F + (c[k] - 1) * d + 5, 25.00F + k * d + 5, d - 10, d - 10);
                    c[k] = 0;
                    k -= 1;
                }
            }
            else
            {
                if (n == 0 || n == 2 || n == 3)
                {
                    timer1.Enabled = false;         //计时器 停止循环
                    MessageBox.Show("此问题无解", "提示");
                    timer1.Dispose();
                    return;
                }
                else
                {
                    timer1.Enabled = false;         
                    MessageBox.Show("全部过程演示完毕", "提示");
                    timer1.Dispose();
                    return;
                }
            }
            myBrush.Dispose();
            myBrush2.Dispose();
        }
        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
        /// <summary>
        /// 继续
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }        
    }
} --------------------编程问答-------------------- 友情up --------------------编程问答-------------------- 谢谢```唉,这个问题我也不知道怎么说,只好把代码都贴上来了,望大家海涵哈... --------------------编程问答-------------------- 如果觉得分不够,我可以加啊~只希望有人能解决噢``` --------------------编程问答-------------------- 好象都没有正义之士啊~
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,