This is an exercise 3 from Ch1 in "C# 3.0 Design Patterns". I can't figure out why this won't work can someone help ? I'm trying to trigger the creation of a colored border and a string tag upon a mouse click event. The program is run on Visual 2008 command line prompt. Thank You. Help deeply appreciated.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using Given;
// Decorator Pattern Example Judith Bishop Aug 2007
// Draws a photograph and a user-generated drawing in a window
// of fixed size . Has decorators that are BorderedPhotos and
// TaggedPhotos that can be composed and added in different
// combinations
namespace Given {
interface IPhotoDraw {
void Drawer( Object source, PaintEventArgs e );
void Decorating_Method( Object source, MouseEventArgs e );
}
// The original Photo class
public class Photo : Form, IPhotoDraw {
Image image;
public Photo ( ) {
image = new Bitmap( "jug.jpg" );
this.Text = "Lemonade";
this.Paint += new PaintEventHandler( Drawer );
}
public void Drawer( Object source, PaintEventArgs e ) {
e.Graphics.DrawImage( image, 30, 20 ); // establishes position of
} // image in base window form
public void Decorating_Method( Object source, MouseEventArgs e ) {
}
}
// Hominid class in which a user-drawing can be decorated
// with tags and borders
public class Hominid : Form, IPhotoDraw {
static Image image;
private Pen p;
public string pen_color = "red";
private bool mouse_down = false;
private Point last_point = Point.Empty;
private Graphics graphics, gfx_seen;
public Hominid ( ) {
image = new Bitmap( "human.jpg" );
graphics = Graphics.FromImage(image);
p = new Pen(Color.FromName(pen_color));
this.BackColor = Color.Blue;
this.Text = "Hominid";
this.Paint += new PaintEventHandler( Drawer );
this.MouseDown += new MouseEventHandler(this.User_Drawing_MouseDown );
this.MouseUp += new MouseEventHandler(this.User_Drawing_MouseUp );
this.MouseMove += new MouseEventHandler(this.User_Drawing_MouseMove );
}
private void User_Drawing_MouseDown(object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Right ) {
image.Save("human.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
} else {
mouse_down = true;
}
}
private void User_Drawing_MouseUp(object sender, MouseEventArgs e ) {
last_point = new Point(e.X, e.Y);
mouse_down = false;
}
protected void User_Drawing_MouseMove(object sender, MouseEventArgs e ) {
if ( mouse_down ) {
Point pMousePos = new Point(e.X, e.Y);
gfx_seen = CreateGraphics();
gfx_seen.DrawLine(new Pen(Color.Black),pMousePos, last_point);
graphics.DrawLine(new Pen(Color.Black),pMousePos, last_point);
}
last_point = new Point(e.X, e.Y);
}
public void Drawer( Object source, PaintEventArgs e ) {
e.Graphics.DrawImage( image, 30, 20 ); // establishes position of
} // drawing image in base window form
public void Decorating_Method( Object source, MouseEventArgs e ) {
}
}
}
class DecoratorPatternExample {
// This simple BorderedPhoto decorator adds a colored border of fixed size
class BorderedPhoto : Form, IPhotoDraw {
IPhotoDraw photo;
Color color;
PaintEventArgs p;
public BorderedPhoto( IPhotoDraw p, Color c ) {
photo = p;
color = c;
this.Paint += new PaintEventHandler( Drawer );
this.MouseDown += new MouseEventHandler( Decorating_Method );
}
public void Drawer( Object source, PaintEventArgs e ) {
photo.Drawer( source, e );
p = e;
}
public void Decorating_Method( Object source, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
p.Graphics.DrawRectangle( new Pen( color, 10 ), 25, 15, 215, 225 );
}
}
}
// The TaggedPhoto decorator keeps track of the tag number which it
// a specific place to be written
class TaggedPhoto : Form, IPhotoDraw {
IPhotoDraw photo;
string tag;
int number;
static int count;
static List <string> tags = new List <string>( );
PaintEventArgs p;
public TaggedPhoto( IPhotoDraw p, string t ) {
photo = p;
tag = t;
tags.Add(tag);
number = ++count;
this.Paint += new PaintEventHandler( Drawer );
this.MouseDown += new MouseEventHandler( Decorating_Method );
}
public void Drawer( Object source, PaintEventArgs e ) {
photo.Drawer( source, e );
p = e;
}
public string ListTaggedPhotos( ) {
string s = "Tagged Photos are: ";
foreach( string tag in tags ) s+= tag+" ";
return s;
}
public void Decorating_Method( Object source, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Right ) {
p.Graphics.DrawString( tag, new Font( "Arial", 16 ), new SolidBrush( Color.Black ), new PointF( 80, 80+number*20 ) );
}
}
}
// Application.Run acts as a simple client
static void Main( ) {
IPhotoDraw photo, drawing;
TaggedPhoto colorTaggedPhoto, foodTaggedPhoto, composition2;
BorderedPhoto composition1;
// Compose a photo with two TaggedPhotos and a Green BorderedPhoto
photo = new Photo( );
Application.Run( (Photo)photo );
foodTaggedPhoto = new TaggedPhoto( photo, "Lemonade" );
//Application.Run( foodTaggedPhoto );
colorTaggedPhoto = new TaggedPhoto( foodTaggedPhoto, "Green" );
Application.Run( colorTaggedPhoto );
composition1 = new BorderedPhoto( colorTaggedPhoto, Color.Green );
Application.Run( composition1 );
Console.WriteLine( colorTaggedPhoto.ListTaggedPhotos( ) );
// Compose a drawing with two TaggedPhotos and a Orange Bordered Drawing
drawing = new Hominid( );
Application.Run( (Hominid)drawing );
foodTaggedPhoto = new TaggedPhoto( drawing, "Hominid" );
Application.Run( foodTaggedPhoto );
colorTaggedPhoto = new TaggedPhoto( foodTaggedPhoto, "Orange" );
Application.Run( colorTaggedPhoto );
composition1 = new BorderedPhoto( colorTaggedPhoto, Color.Orange );
Application.Run( composition1 );
Console.WriteLine( colorTaggedPhoto.ListTaggedPhotos( ) );
// Compose a drawing with one TaggedPhoto and a Red Bordered Drawing
drawing = new Hominid( );
Application.Run( (Hominid)drawing );
composition1 = new BorderedPhoto( drawing, Color.Red );
Application.Run( composition1 );
composition2 = new TaggedPhoto( composition1, "Stick Figure" );
Application.Run( composition2 );
Console.WriteLine( composition2.ListTaggedPhotos( ) );
}
}