| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2297 人关注过本帖
标题:设计模式3.0
只看楼主 加入收藏
学好好要饭
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2016-5-4
结帖率:100%
收藏
 问题点数:0 回复次数:1 
设计模式3.0
装饰器模式中的照片装饰器,Bitmap哪里出现问题,参数无效,味处理ArgumentException,怎么回事,怎么解决,下面附上代码?
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
using Given;

namespace Given
{
    //原始的Photo类
    public class Photo:Form
    {
        Image image;
        public Photo()
        {
            image = new Bitmap("jug.jpg");
            this.Text = "Lemonade";
            this.Paint += new PaintEventHandler(Drawer);
        }
        public virtual void Drawer(Object source,PaintEventArgs e)
        {
            e.Graphics.DrawImage(image, 30, 20);
        }
    }
}
class DecoratorPattetnExample
{
    //简单的BorderPhoto装饰器为图片添加一个固定大小的边框
    class BorderedPhoto :Photo
    {
        Photo photo;
        Color color;

        public BorderedPhoto(Photo p,Color c)
        {
            photo = p;
            color = c;
        }
        public override void Drawer(Object source,PaintEventArgs e)
        {
            photo.Drawer(source, e);
            e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225);
        }
    }
    //TaggedPhoto装饰器跟踪标签的使用并为其编号
    //从而给出特定标签被打印的位置
    class TaggedPhoto :Photo
    {
        Photo photo;
        string tag;
        int number;
        static int count;
        List<string> tags = new List<string>();

        public TaggedPhoto(Photo p,string t)
        {
            photo = p;
            tag = t;
            tags.Add(t);
            number = ++count;
        }
        public override void Drawer(Object source,PaintEventArgs e)
        {
            photo.Drawer(source, e);
            e.Graphics.DrawString(tag,
              new Font("Arial", 16),
              new SolidBrush(Color.Black),
              new PointF(80, 100 + number * 20));
        }
        public string ListTaggedPhotos()
        {
            string s = "Tag are:";
            foreach (string t in tags) s +=t+" ";
            return s;
        }
    }
    static void Main()
    {
        Photo photo;
        TaggedPhoto foodTaggedPhoto, colorTaggedPhoto, tag;
        BorderedPhoto composition;

        photo = new Photo();
        Application.Run(photo);
        foodTaggedPhoto = new TaggedPhoto(photo, "Food");
        colorTaggedPhoto = new TaggedPhoto(foodTaggedPhoto, "Yellow");
        composition = new BorderedPhoto(colorTaggedPhoto, Color.Blue);
        Application.Run(composition);
        Console.WriteLine(colorTaggedPhoto.ListTaggedPhotos());

        //合成一张拥有一个TaggedPhoto和一个黄色BorderedPhoto装饰照片
        photo = new Photo();
        tag = new TaggedPhoto(photo, "Jug");
        composition = new BorderedPhoto(tag, Color.Yellow);
        Application.Run(composition);
        Console.WriteLine(tag.ListTaggedPhotos());
    }
}
搜索更多相关主题的帖子: 照片 public 
2016-07-07 09:56
学好好要饭
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2016-5-4
收藏
得分:0 
要放在debug里面,卧槽,谁说放哪里都可以的
2016-07-07 10:10
快速回复:设计模式3.0
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017059 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved