注册 登录
编程论坛 C# 论坛

设计模式3.0

学好好要饭 发布于 2016-07-07 09:56, 2428 次点击
装饰器模式中的照片装饰器,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());
    }
}
1 回复
#2
学好好要饭2016-07-07 10:10
要放在debug里面,卧槽,谁说放哪里都可以的
1