zoukankan      html  css  js  c++  java
  • Drawing Shapes using Threading

    This is simple multi-threading program that draws circles and rectangles. Each shape is handle by individual thread that created every time you press start button. Using sleep method on each thread we can change the speed of each shape.

    I have used VS.NET to implement this program. Double click on Thread_Example.Zip and extract all the files and folders to a folder in C drive.






    其中,基类Shapes.cs:
    代码如下:

    using System;
    using System.Drawing;
    using System.Threading;



    namespace ThreadTester
    {
        
    public abstract class Shapes
        
    {
            
    protected int  xVal;
            
    protected int yVal;
            
    protected Color color;
            
    protected int width; 
            
    protected int height;
            
    protected int directionY = 1;
            
    protected int directionX = 1;
            
    protected Form1 frm1;
            
    protected int speed;
                
            
    public Shapes()
            
    {  }

            
    public abstract void paint(Graphics g);
                
            
    public void CheckCoordinates()
            
    {
                
    if((frm1.panel1.Size.Height-20 < yVal) || (yVal <= 0))
                    directionY 
    = directionY * (-1);    

                
    if((frm1.panel1.Size.Width-20 < xVal) || (xVal <= 0))
                    directionX 
    = directionX * (-1);
            }

        }

    }


    继承Shapes的2个继承类:Rectangle和Circle类定义如下:

    using System;
    using System.Drawing;
    using System.Threading;



    namespace ThreadTester
    {
        
    public class Rectangle : Shapes
        
    {
            
    public Rectangle(int x,int y,int w,int h, Color c,Form1 f,int spd)
            
    { xVal = x; yVal = y; color = c; width = w; height = h;    frm1 = f; speed = spd;}
                    
            
    public override void  paint(Graphics g)
            
    {    
                
    try
                
    {
                    Thread.Sleep(speed);
                    g.DrawRectangle(
    new System.Drawing.Pen(frm1.BackColor),xVal,yVal,width,height);
                    
    lock(typeof(Thread))
                    
    {
                            xVal 
    = xVal+ base.directionX; 
                        yVal 
    = yVal+ base.directionY;
                        
    base.CheckCoordinates();
                    }

                    g.DrawRectangle(
    new System.Drawing.Pen(color),xVal,yVal,width,height); 
                }

                
    catch
                
    {}
                
            }

        }

    }

            




    using System;
    using System.Drawing;
    using System.Threading;



    namespace ThreadTester
    {
        
    public class Circle : Shapes
        
    {
            
    public Circle(int x,int y,int w,int h, Color c, Form1 f, int spd)
            
    { xVal = x; yVal = y; color = c; width = w; height = h;    frm1 = f; speed = spd;}

                            
            
    public override void  paint(Graphics g)
            
    {    
                
    try
                
    {
                    Thread.Sleep(speed);
                    g.DrawEllipse(
    new System.Drawing.Pen(frm1.BackColor),xVal,yVal,width,height); 
                            
                    
    lock(typeof(Thread))
                    
    {
                        xVal 
    = xVal+ base.directionX; 
                        yVal 
    = yVal+ base.directionY;
                        
    base.CheckCoordinates();
                    }

                    g.DrawEllipse(
    new System.Drawing.Pen(color),xVal,yVal,width,height); 
                }

                
    catch
                
    {    }        
            }

        }

    }




    主界面如下:

    全局变量定义如下:

    public volatile Panel panel1;
            
    public static Color shapeColor = Color.Blue;
            
    public ColorDialog c;
            
    public static int threadCount = 0;
            
    private Hashtable threadHolder = new Hashtable();
            
    private const int shapSize = 15
            
    private volatile Graphics g;
            

    注意其中的:

            private volatile Graphics g;
            
    public volatile Panel panel1;

    退出部分代码如下:

        private void cmdExit_Click(object sender, System.EventArgs e)
            
    {
                
    /* Abort all the threads that are currently alive */
                
    foreach(Thread t in threadHolder.Values)
                
    {
                        
    if(t != null && t.IsAlive)
                       t.Abort();
                }

                Form1.ActiveForm.Close();    
            }
    选择颜色代码:
        private void cmdColor_Click(object sender, System.EventArgs e)
            
    {
                c 
    = new ColorDialog();
                c.ShowDialog();
                shapeColor 
    = c.Color;
            }


    开始按钮代码 如下:
        private void cmdStart_Click(object sender, System.EventArgs e)
            
    {
                Thread t 
    = new Thread(new ThreadStart(StartThread));
                threadHolder.Add(threadCount
    ++,t);
                
    /* Assign a name for each thread (but it's not necessary) */
                t.Name 
    = "Thread ID: "+threadCount;
                t.IsBackground  
    = true;
                t.Start();
            }

            

    当中对线程的定义部分如下:
        private void StartThread()
            
    {
                
    /* Create the shape object */
                Shapes shapes;
                
    if(comboBox1.Text.Equals("Rectangle"))
                    shapes 
    =  new Rectangle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim()));
                
    else if(comboBox1.Text.Equals("Circle"))
                    shapes 
    =  new Circle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim())); 
                    
    /**The line below is not necessary , but if you want to add more shapes this will help*/
                
    else
                    shapes 
    =  new Rectangle(0,0,shapSize,shapSize,shapeColor,this,Convert.ToInt32(comboBoxSpeed.Text.Trim()));
            
                
    while(true)
                
    {
                        
    try
                    
    { shapes.paint(g);     }
                        
                    
    catch(Exception e1)
                    
    {
                            Console.WriteLine(
    "Exception in Form1 whileloop >> "+e1);
                        
    break;     
                    }

                }

            }

  • 相关阅读:
    人工智能与信息社会——人工智能发展简史
    人工智能与信息社会——新闻热点与身边的人工智能
    蓝桥杯——入门训练
    Android Studio安装虚拟机步骤
    Android开发环境搭建教程
    Android系统架构(图解)
    第6.7节 stack 1982 问题 B: Problem E
    Matlab高级教程_第二篇:MATLAB和C#对应数据类型的讲解(多讲一点儿C#的矩阵运算)
    Matlab高级教程_第二篇:一个简单的混编例子
    Matlab高级教程_第二篇:Matlab2016a和VS2013混合
  • 原文地址:https://www.cnblogs.com/jhtchina/p/369343.html
Copyright © 2011-2022 走看看