Problems with opening a window - Forms - c#

I'm trying to make a simple 2d game with C# using forms but when i run it at 60 fps it uses 50% of my CPU and doesn't use my GPU at all. I've been looking for hours and can't find another way to do it. How do i get it to run on my GPU?
Also I'm using a thread to wait in between frames with Thread.Sleep(15). I don't know if this is a good way to do it.
I was following this video for the window https://www.youtube.com/watch?v=JnGM1p2vsbE
This is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
namespace RadiantEngine
{
class Canvas : Form
{
public Canvas()
{
this.DoubleBuffered = true;
}
}
public abstract class RadiantWindow
{
public Vector2 ScreenSize;
public string Title;
private Canvas Window;
private Thread GameLoopThread;
public float num;
Bitmap image = (Bitmap)Image.FromFile("F:/VS/RadiantEngine/RadiantEngine/dog.png");
public RadiantWindow(Vector2 ScreenSize, string Title)
{
this.ScreenSize = ScreenSize;
this.Title = Title;
EngineInit();
}
public RadiantWindow(float x, float y, string Title)
{
Vector2 _ScreenSize = new Vector2();
this.ScreenSize = _ScreenSize;
this.Title = Title;
EngineInit();
}
void EngineInit()
{
Window = new Canvas();
Window.Text = Title;
Window.Size = new Size((int)ScreenSize.x, (int)ScreenSize.y);
Window.Paint += Renderer;
GameLoopThread = new Thread(GameLoop);
GameLoopThread.Start();
Application.Run(Window);
}
private void Renderer(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.Aqua);
g.DrawImage(RadiantEngine.engine.renderer.FrameRender(), 0, 0);
}
void GameLoop()
{
OnLoad();
while (GameLoopThread.IsAlive)
{
try
{
OnDraw();
Window.BeginInvoke((MethodInvoker)delegate { Window.Refresh(); });
}
catch
{
Console.WriteLine("Game Is Loading");
}
Thread.Sleep(15);
}
}
public abstract void OnDraw();
public abstract void OnLoad();
}
}
Thanks for any help.

Related

Running a Method from SoundEngine class's object anywhere

I am struggeling with getting my SoundEngine class's function PlaySound to be played where I want it.
The function needs to be a global function but I can't make it static as the function refers to an object that the SoundEngine makes.
I'll show the snippets that are important:
In my GameWorld class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;
using System;
using SoundEngineSpace;
public GameWorld(int width, int height, ContentManager Content)
{
screenWidth = width;
screenHeight = height;
random = new Random();
gameState = GameState.Playing;
block = Content.Load<Texture2D>("block");
font = Content.Load<SpriteFont>("SpelFont");
SoundEffect blockFallSE = Content.Load<SoundEffect>("BlockFallSE");
Song BuildingWallsintheCold = Content.Load<Song>("91 Building Walls in the Cold");
soundEngine = new SoundEngine();
soundEngine.addSound(blockFallSE);
soundEngine.addSong(BuildingWallsintheCold);
soundEngine.SetSoundVolume(20);
soundEngine.SetSongVolume(5);
soundEngine.PlaySong(0);
grid = new TetrisGrid(block);
}
In my TetrisGrid class
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SoundEngineSpace;
...
public void TetNaarBeneden()
{
soundEngine.PlaySound(0);
InPlayGrid.Velocity = new Vector2(0, gridblock.Height);
CheckValidLocation();
}//Moves tet down
And the soundEngine class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
namespace SoundEngineSpace
{
public class SoundEngine
{
private void soundEngine()
{
}
BackgroundSound backgroundSound = new BackgroundSound();
SoundEffects soundEffects = new SoundEffects();
public void addSong(Song s)
{
backgroundSound.AddBackgroundSound(s);
}
public void addSound(SoundEffect s)
{
soundEffects.AddSound(s);
}
public void PlaySong(int s)
{
backgroundSound.PlayBackgroundSound(s);
}
public void PlaySound(int s)
{
soundEffects.PlaySound(s);
}
public void SetSongVolume(int v)
{
backgroundSound.SetBackGroundSoundVolume(v);
}
public void SetSoundVolume(int v)
{
soundEffects.SetSoundEffectVolume(v);
}
}
}
class BackgroundSound
{
public static void backGroundSound()
{
}
private List<Song> BackgroundSoundEffects = new List<Song>();
private bool PlayingBackGroundSound = false;
public void AddBackgroundSound(Song s)//addsongs to the list
{
BackgroundSoundEffects.Add(s);
}
public void PlayBackgroundSound(int s)//plays BackgroundSound based on location in the list
{
MediaPlayer.IsRepeating = true;//If I use this exact soundengine again, move this to it's own function instead!
if (BackgroundSoundEffects.Count() > s)
{
if (PlayingBackGroundSound)
MediaPlayer.Stop();
MediaPlayer.Play(BackgroundSoundEffects.ElementAt(s));
PlayingBackGroundSound = true;
}
else
{
Console.WriteLine("Couldent find the BackgroundSound");
}
}
public void SetBackGroundSoundVolume(int v)
{
MediaPlayer.Volume = (float)v/100;
}
}
class SoundEffects
{
public static void soundeffects()
{
}
private List<SoundEffect> soundEffects = new List<SoundEffect>();
public void AddSound(SoundEffect s)//addsongs to the list
{
soundEffects.Add(s);
}
public void PlaySound(int s)//plays sound based on location in the list
{
if (soundEffects.Count() > s)
{
SoundEffect ToPlaySound = soundEffects.ElementAt(s);
ToPlaySound.Play();
}
else
{
Console.WriteLine("Couldent find the sound");
}
}
public void SetSoundEffectVolume(int v)
{
SoundEffect.MasterVolume = (float)v/100;
}
}
I ended up making the constructor of objects that needed acces to the SounEngine pass the SoundEngine as a paramter.
Then I could create a new soundEngine within the object I needed it to exsist and call: this.soundEngine = soundEngine.
That succesfully copied the soundEngine and let me use it's fucntions.

Draw Line & Thickness in a Class: method group / OverLoad issue

I'm trying to set up the code for a Line class with it's own thickness besides the DefaultLineThickness I have for my squares and circles. As much as I would like to use g.DrawLine(Pens.Black, 25, 40, 126, 126); it needs to be in it's own class and inherited which I've done with the other shapes. I found this topic closest to my issue but its in XAML and anywhere else it's just simple g.DrawLine
http://www.c-sharpcorner.com/uploadfile/mahesh/line-in-wpf/
Trying to make a crude bike out of circles, squares, and lines.
*edit
After enough times of playing around I'm on the brink of my goal. My only problem is that after I plug everything in and set the line thickness to x-number, it's giving me an error that it can't assign the number because of a 'method group'
Form1 Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Bicycle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
LineOne l1 = new LineOne(new PointF(50, 40));
l1.setFilledState(false);
l1.setLineColor(Color.Black);
l1.setLineThickness = (6);
//cannot assign to 'setLineThickness' because it is a method group'
l1.Draw(g);
sRectangle r2 = new sRectangle(new PointF(151, 160));
r2.setFilledState(true);
r2.setLineColor(Color.Green);
r2.setFilledColor(Color.Honeydew);
r2.Draw(g);
sRectangleEmpty r1 = new sRectangleEmpty(new PointF(150, 150));
r1.setFilledState(false);
r1.setLineColor(Color.Blue);
r1.Draw(g);
sCircle c1 = new sCircle(new PointF(180, 130));
c1.setFilledState(true);
c1.setLineColor(Color.Orange);
c1.setFilledColor(Color.Ivory);
c1.Draw(g);
sCircleEmpty c2 = new sCircleEmpty(new PointF(120, 130));
c2.setFilledState(false);
c2.setLineColor(Color.Black);
c2.Draw(g);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
The Line Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Bicycle
{
class LineOne : sRectangle
{
private void setDrawingAttributes()
{
const int lenght = 10;
Pen SmallPen = new Pen(Brushes.DeepSkyBlue);
SmallPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
PointF p1 = PointF.Add(location, new Size(-lenght / 2, 0));
}
private void init()
{
setDrawingAttributes();
}
public LineOne()
{
init();
}
public LineOne(PointF p)
: base(p)
{
init();
}
public override void Draw(Graphics g)
{
g.DrawEllipse(pen, rect);
}
}
}
The Shape Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Bicycle
{
class Shape
{
private Color DefaultLineColor = Color.Black;
private Color DefaultFillColor = Color.Blue;
private float DefaultLineThickness = 2;
protected bool bOutLine;
protected bool bFilled;
protected Pen pen;
protected Brush brush;
protected PointF location;
private void setDrawingAttributes()
{
pen = new Pen(DefaultLineColor, DefaultLineThickness);
brush = new SolidBrush(DefaultFillColor);
}
private void init()
{
bOutLine = true;
setDrawingAttributes();
}
public Shape()
{
init();
}
public Shape(PointF p)
{
location = p;
init();
}
public Color getFillColor()
{
return (DefaultFillColor);
}
public bool getFilledState()
{
return (bFilled);
}
public Color getLineColor()
{
return (DefaultLineColor);
}
public float getLineThickness()
{
return (DefaultLineThickness);
}
public bool getOutLineState()
{
return (bOutLine);
}
public bool isOutLine()
{
return (bOutLine);
}
public bool isFilled()
{
return (bFilled);
}
public void setFilledColor(Color C)
{
DefaultFillColor = C;
setDrawingAttributes();
}
public void setLineColor(Color C)
{
DefaultLineColor = C;
setDrawingAttributes();
}
public void setLineThickness(float value)
{
DefaultLineThickness = value;
setDrawingAttributes();
}
public void setFilledState(bool value)
{
bFilled = value;
}
public void setOutLineState(bool value)
{
bOutLine = value;
}
}
}
setLineThickness is a method, not a property.
Change l1.setLineThickness = (6); to l1.setLineThickness(6); (i.e. remove the equals sign)
Found it, after reviewing a article in regards of Overloads my Line code should had been
public override void Draw(Graphics g)
{
g.DrawLine(pen, new Point(0, 0), new Point(120, 95));
}
I'm now getting different strokes of lines when I play with the l1.setLineThickness(1); in my form code.
Thank you for your assist Mr.Williams on the clarification about LineThickness.

how can I add items to my ListView

I keep getting this error and I know why but I need help figuring out how I can solve it. The only way I have been able to add my items it to make a new form but that seems silly.
It wont work if I make all my methods static =(
I keep getting,
"An object reference is required for the non-static field, method, or
property 'Handicap_Calculator.FormMain.listViewLog'
\Form1.cs 74 13 Handicap Calculator"
HereĀ“s my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Handicap_Calculator
{
public partial class FormMain : Form
{
//FormAddScore FormAddNewScore = new FormAddScore();
public static bool addScoreIsShown = false;
public static FormAddScore _FormAddScore;
public static ListViewItem Item;
//public static List<string> ScoreInfo = new List<string>();
public FormMain()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
try
{
if (_FormAddScore == null || _FormAddScore.IsDisposed)
{
_FormAddScore = new FormAddScore();
}
_FormAddScore.Show();
if (_FormAddScore.WindowState == FormWindowState.Minimized)
{
_FormAddScore.WindowState = FormWindowState.Normal;
}
_FormAddScore.BringToFront();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public static void AddScore()
{
int Round = 1;
DateTime date = _FormAddScore.date;
string course = _FormAddScore.course;
int holes = _FormAddScore.holes;
int score = _FormAddScore.score;
float courseRating = _FormAddScore.courseRating;
float slopeRating = _FormAddScore.slopeRating;
string[] ScoreInfo = new string[7];
ScoreInfo[0] = Round.ToString();
ScoreInfo[1] = date.ToString();
ScoreInfo[2] = course;
ScoreInfo[3] = holes.ToString();
ScoreInfo[4] = score.ToString();
ScoreInfo[5] = courseRating.ToString();
ScoreInfo[6] = slopeRating.ToString();
AddToList(ScoreInfo);
}
public static void AddToList(string[] ScoreInfo)
{
Item = new ListViewItem(ScoreInfo);
//listViewLog.Items.Add(Item);
}
}
}
Edit...
Here is the class im calling it from:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Handicap_Calculator
{
public partial class FormAddScore : Form
{
public DateTime date;
public string course;
public int holes;
public int score;
public float courseRating;
public float slopeRating;
public FormAddScore()
{
InitializeComponent();
}
private void FormAddScore_FormClosing(object sender, FormClosingEventArgs e)
{
FormMain.addScoreIsShown = false;
}
public void getscore()
{
try
{
date = dateTimePicker1.Value;
course = textBoxCourse.Text;
holes = Convert.ToInt16(textBoxHoles.Text);
score = Convert.ToInt16(textBoxScore.Text);
courseRating = Convert.ToSingle(textBoxCourseRating.Text);
slopeRating = Convert.ToSingle(textBoxSlopeRating.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
getscore();
FormMain.AddScore();
}
}
}
The simple solution is to define your methods AddScore and AddToList as non-static.
public void AddScore()
{
//your code
}
public void AddToList(string[] ScoreInfo)
{
// your code
}
If you want to use static methods you should pass the instance of your Form to the method, on which you want to add items to the ListView.
public static void AddScore(FormMain mainForm)
{
//your code
AddToList(mainForm, ScoreInfo);
}
public static void AddToList(FormMain mainForm, string[] ScoreInfo)
{
// your code
}
Update:
According to your updated code the solution is to pass the instance of your FormMain to your FormAddScore when you create it. In FormAddScore you store the reference to the FormMain instance to call the methods on.
public partial class FormAddScore : Form
{
// your code
private FormMain _mainForm;
public FormAddScore(){
InitializeComponent();
}
public FormAddScore(FormMain mainForm) : this(){
_mainForm = mainForm;
}
In your FormMain when you create the instance of FormAddScore you should use the constructor that expects an instance of FormMain
_FormAddScore = new FormAddScore(this);
With this setup you can change your methods to non-static and you can call the methods of FormMain in your FormAddScore, by using the stored reference in variable _mainForm.
_mainForm.AddScore();

SharpDX, DirectWrite and Windows Forms

Can one render text using DirectWrite to a PictureBox in a WinForm app?
I'm using SharpDX and have gone through the DirectWrite samples trying to build the simplest working case I can.
I created a Form and added only a pictureBox to it. Then the following code. The form displays, but nothing visible with the PictureBox.
Any guidance would be much appreciated.
Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpDX.Direct2D1;
using SharpDX.DXGI;
using SharpDX;
using SharpDX.DirectWrite;
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using Factory = SharpDX.Direct2D1.Factory;
namespace d2dwTextEdit
{
public partial class Form1 : Form
{
public Factory Factory2D { get; private set; }
public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; }
public WindowRenderTarget RenderTarget2D { get; private set; }
public SolidColorBrush SceneColorBrush { get; private set; }
public TextFormat TextFormat { get; private set; }
public SharpDX.RectangleF ClientRectangle { get; private set; }
public Form1()
{
InitializeComponent();
Initialize();
Render();
}
protected void Initialize()
{
Factory2D = new SharpDX.Direct2D1.Factory();
FactoryDWrite = new SharpDX.DirectWrite.Factory();
HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
properties.Hwnd = pictureBox1.Handle;
properties.PixelSize = new System.Drawing.Size(pictureBox1.Width, pictureBox1.Height);
properties.PresentOptions = PresentOptions.None;
TextFormat = new TextFormat(FactoryDWrite, "Calibri", 30) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center };
RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;
ClientRectangle = new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height);
SceneColorBrush = new SolidColorBrush(RenderTarget2D, Colors.White);
SceneColorBrush.Color = Colors.Black;
}
private void Render()
{
RenderTarget2D.Clear(Colors.White);
RenderTarget2D.DrawText("Hello Marshall", TextFormat, ClientRectangle, SceneColorBrush);
}
}
}
Hi I sort of followed your question example but I finally managed to work it out as I would like to independently start writing games, all the draw methods have to be within the beginDraw and endDraw methods of the RenderTarget class'. you also need to implement the RenderLoop class this will give you the callback loop delegate. Apologies for the crudeness of my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using SharpDX.Direct3D;
using SharpDX.Direct2D1;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using SharpDX.DXGI;
using SharpDX;
using SharpDX.Windows;
using System.Globalization;
using SharpDX.DirectWrite;
namespace dx11
{
public partial class Form1 : Form
{
private SharpDX.Direct3D10.Device _mDevice = null;
WindowRenderTarget wndRender = null;
SharpDX.Direct2D1.Factory fact = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded);
SolidColorBrush scenebrush;
RenderTargetProperties rndTargProperties;
HwndRenderTargetProperties hwndProperties;
SharpDX.Windows.RenderForm form = new RenderForm();
RenderLoop.RenderCallback callback;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
form.Width = 600;
form.Height = 600;
test();
callback = new RenderLoop.RenderCallback(Render);
RenderLoop.Run(form, callback);
}
private void test()
{
rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
hwndProperties = new HwndRenderTargetProperties();
hwndProperties.Hwnd = form.Handle;
hwndProperties.PixelSize = new SharpDX.DrawingSize(form.ClientSize.Width, form.ClientSize.Height);
hwndProperties.PresentOptions = PresentOptions.None;
wndRender = new WindowRenderTarget(fact, rndTargProperties, hwndProperties);
scenebrush = new SolidColorBrush(wndRender, Colors.Red);
// scenebrush.Color = Colors.Cornsilk;
form.Show();
}
public void Render()
{
wndRender.BeginDraw();
wndRender.Clear(Colors.DarkBlue);
wndRender.DrawRectangle(new SharpDX.RectangleF(10, 10, 50, 50), scenebrush, 2.00F);
wndRender.Flush();
wndRender.EndDraw();
}
}
}
This should create a blue form with a red square in the top left corner.
You don't actually want to render "into the PictureBox" with an HwndRenderTarget. You actually want to render into the Bitmap that the PictureBox has the job of rendering. You can do that by creating a "shared bitmap" (I'm not familiar with SharpDX specifically but the underlying method is ID2D1RenderTarget::CreateSharedBitmap()), rendering into that, and then invalidating the PictureBox after you call EndDraw().

Decorator Pattern Help

I am working on an Oreilly's example
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
using Given;
// Decorator Pattern Example Judith Bishop August 2007
// Draws a single photograph in a window of fixed size
// Has decorators that are BorderedPhotos and TaggedPhotos that can be composed and added
// in different combinations
namespace Given {
// The original Photo class
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 DecoratorPatternExample {
// This simple BorderedPhoto decorator adds a colored BorderedPhoto of fixed size
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);
}
}
// The TaggedPhoto decorator keeps track of the tag number which gives it
// a specific place to be written
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 = "Tags are: ";
foreach (string t in tags) s +=t+" ";
return s;
}
}
static void Main () {
// Application.Run acts as a simple client
Photo photo;
TaggedPhoto foodTaggedPhoto, colorTaggedPhoto, tag;
BorderedPhoto composition;
// Compose a photo with two TaggedPhotos and a blue BorderedPhoto
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());
// Compose a photo with one TaggedPhoto and a yellow BorderedPhoto
photo = new Photo();
tag = new TaggedPhoto (photo,"Jug");
composition = new BorderedPhoto(tag, Color.Yellow);
Application.Run(composition);
Console.WriteLine(tag.ListTaggedPhotos());
}
}
/* Output
TaggedPhotos are: Food Yellow
TaggedPhotos are: Food Yellow Jug
*/
Next Exercise is
Assume that the Photo class was written with Drawer as a plain (not virtual)
method and it cannot be altered. Reconstruct Example 2-2 so that it works
under this constraint
How can i do that ?
My Approach
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using GivenWihInterface;
namespace GivenWihInterface
{
interface IPhoto
{
void Drawer(object sender, PaintEventArgs e);
}
class Photo : Form, IPhoto
{
Image image;
public Photo()
{
image = new Bitmap(#"c:\users\anishmarokey\documents\visual studio 2010\Projects\Design_Pattern_Decorator\DecoratorPattern_RealExample\Images\apple-6.jpg");
this.Text = "Apple";
this.Paint += new PaintEventHandler(Drawer);
}
public void Drawer(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(image, 20, 20);
}
}
class BorderPhoto : Form, IPhoto
{
IPhoto pho;
Color color;
public BorderPhoto(IPhoto p, Color c)
{
pho = p;
color = c;
this.Paint += new PaintEventHandler(Drawer);
}
public void Drawer(object sender, PaintEventArgs e)
{
pho.Drawer(sender, e);
e.Graphics.DrawRectangle(new Pen(color, 10), 25, 15, 215, 225);
}
}
}
namespace DecoratorPattern_RealExample
{
class DecoratorPatternWithInterface
{
static void Dispaly(GivenWihInterface.IPhoto p)
{
Application.Run((Form)p);
}
static void Main()
{
IPhoto component = new GivenWihInterface.Photo();
Dispaly(component);
component = new GivenWihInterface.Photo();
IPhoto p = new GivenWihInterface.BorderPhoto(component,Color.Red);
Application.Run((Form)p);
}
}
}
is this the correct Way?
Yes, that is an appropriate "decorator" implementation. The only thing I'd question is whether you actually need to inherit from Form, or whether implementing IPhoto is sufficient. Which can only be answered with more context.
Also, the hard-coded (dimensions?) values look like there may be a better way if some existing values are available somewhere.
The example itself is unusual, though - you've had to introduce the interface, which is pretty much as much of a change as you are trying to avoid; and the type handles an event on itself, which is bad practice. I almost wonder if they want you to hook into the event pipeline, but that wouldn't really be a decorator.
I suspect they want you to code against Form rather than the IPhoto you have introduced, which would allow you to decorate lots of things. But you would need to have a known method on Form to call, for example Paint() - except that is an event here, not a method so the name will be different. And again we could hook the event, but that isn't classic decorator usage.

Categories

Resources