CSharp:Flyweight Patterns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/// <summary>
  /// Summary description for Positioner.
  /// Flyweight Patterns享元 模式
  ///20220918
  /// geovindu,Geovin Du,涂聚文
  /// </summary>
  public class Positioner
  {
      private const int pLeft = 30;
      private const int pTop = 30;
      private const int HSpace = 70;
      private const int VSpace = 80;
      private const int rowMax = 2;
      private int x, y, cnt;
      /// <summary>
      ///
      /// </summary>
      public Positioner()
      {
          reset();
      }
      /// <summary>
      ///
      /// </summary>
      public void reset()
      {
          x = pLeft;
          y = pTop;
          cnt = 0;
      }
      /// <summary>
      ///
      /// </summary>
      /// <returns></returns>
      public int nextX()
      {
          return x;
      }
      /// <summary>
      ///
      /// </summary>
      public void incr()
      {
          cnt++;
          if (cnt > rowMax)
          {   //reset to start new row
              cnt = 0;
              x = pLeft;
              y += VSpace;
          }
          else
          {
              x += HSpace;
          }
      }
      /// <summary>
      ///
      /// </summary>
      /// <returns></returns>
      public int nextY()
      {
          return y;
      }
  }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <summary>
    /// Flyweight Patterns享元 模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public class Rectangle
    {
        private int x1, x2, y1, y2;
        private int w, h;
        public Rectangle() { }
        /// <summary>
        ///
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void init(int x, int y)
        {
            x1 = x;
            y1 = y;
            x2 = x1 + w;
            y2 = y1 + h;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="w_"></param>
        /// <param name="h_"></param>
        public void setSize(int w_, int h_)
        {
            w = w_;
            h = h_;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="xp"></param>
        /// <param name="yp"></param>
        /// <returns></returns>
        public bool contains(int xp, int yp)
        {
            return (x1 <= xp) && (xp <= x2) &&
                    (y1 <= yp) && (yp <= y2);
        }
    }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// <summary>
 /// Summary description for Folder.
 /// Flyweight Patterns享元 模式
 ///20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class Folder
 {
     //Draws a folder at the specified coordinates
     private const int w = 50;
     private const int h = 30;
     private Pen blackPen, whitePen;
     private Pen grayPen;
     private SolidBrush backBrush, blackBrush;
     private Font fnt;
     /// <summary>
     ///
     /// </summary>
     /// <param name="col"></param>
     public Folder(Color col)
     {
         backBrush = new SolidBrush(col);
         blackBrush = new SolidBrush(Color.Black);
         blackPen = new Pen(Color.Black);
         whitePen = new Pen(Color.White);
         grayPen = new Pen(Color.Gray);
         fnt = new Font("Arial", 12);
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="g"></param>
     /// <param name="x"></param>
     /// <param name="y"></param>
     /// <param name="title"></param>
     public void draw(Graphics g, int x, int y, string title)
     {
         g.FillRectangle(backBrush, x, y, w, h);
         g.DrawRectangle(blackPen, x, y, w, h);
         g.DrawLine(whitePen, x + 1, y + 1, x + w - 1, y + 1);
         g.DrawLine(whitePen, x + 1, y, x + 1, y + h);
 
         g.DrawRectangle(blackPen, x + 5, y - 5, 15, 5);
         g.FillRectangle(backBrush, x + 6, y - 4, 13, 6);
 
         g.DrawLine(grayPen, x, y + h - 1, x + w, y + h - 1);
         g.DrawLine(grayPen, x + w - 1, y, x + w - 1, y + h - 1);
         g.DrawString(title, fnt, blackBrush, x, y + h + 5);
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
    /// Summary description for FolderFactory.
    /// Flyweight Patterns享元 模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public class FolderFactory
    {
        private Folder selFolder, unselFolder;
        /// <summary>
        ///
        /// </summary>
        public FolderFactory()
        {
            //create the two folders
            selFolder = new Folder(Color.Brown);
            unselFolder = new Folder(Color.Bisque);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="selected"></param>
        /// <returns></returns>
        public Folder getFolder(bool selected)
        {
            if (selected)
                return selFolder;
            else
                return unselFolder;
        }
    }

  

窗体调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/// <summary>
  /// Flyweight Patterns享元 模式
  ///20220918
  /// geovindu,Geovin Du,涂聚文
  /// </summary>
  public partial class FlyweightPatternsForm : Form
  {
      private ArrayList names;
      private Folder fol;
      private const int pLeft = 30;
      private const int pTop = 30;
      private const int HSpace = 70;
      private const int VSpace = 80;
      private const int rowMax = 2;
      private string selectedName;
      private FlyweightPatterns.Rectangle rect;
      private FolderFactory folFact;
      private Positioner posn;
      /// <summary>
      ///
      /// </summary>
      private void init()
      {
          names = new ArrayList();
          names.Add("Adam");
          names.Add("Bill");
          names.Add("Charlie");
          names.Add("Dave");
          names.Add("Edward");
          names.Add("Fred");
          names.Add("George");
          fol = new Folder(Color.Bisque);
          selectedName = (string)names[0];
          Pic.Paint += new PaintEventHandler(picPaint);
          rect = new FlyweightPatterns.Rectangle();
          rect.setSize(50, 30);
          folFact = new FolderFactory();
          posn = new Positioner();
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void picPaint(object sender, PaintEventArgs e)
      {
          Graphics g = e.Graphics;
          posn.reset();
          for (int i = 0; i < names.Count; i++)
          {
              fol = folFact.getFolder(selectedName.Equals((string)names[i]));
              fol.draw(g, posn.nextX(), posn.nextY(), (string)names[i]);
              posn.incr();
          }
      }
      /// <summary>
      ///
      /// </summary>
      public FlyweightPatternsForm()
      {
          InitializeComponent();
          init();
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void FlyweightPatternsForm_Load(object sender, EventArgs e)
      {
 
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void Pic_Click(object sender, EventArgs e)
      {
 
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void Pic_MouseMove(object sender, MouseEventArgs e)
      {
          string oldname = selectedName;  //save old name
          bool found = false;
          posn.reset();
          int i = 0;
          selectedName = "";
          while (i < names.Count && !found)
          {
              rect.init(posn.nextX(), posn.nextY());
              //see if a rectangle contains the mouse
              if (rect.contains(e.X, e.Y))
              {
                  selectedName = (string)names[i];
                  found = true;
              }
              posn.incr();
              i++;
          }
          //only refresh if mouse in new rectangle
          if (!oldname.Equals(selectedName))
          {
              Pic.Refresh();
          }
      }
  }

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-09-25 SQLHelper.cs
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示