1完成Drawing工程
接口IColor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
namespace Drawing
{
interface IColor
{
void SetColor(Color color);
}
}
接口IDraw
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
namespace Drawing
{
interface IDraw
{
void SetLocation(int xCoord,int yCoord);
void Draw(Canvas canvas);
}
}
画方块类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
namespace Drawing
{
class Square:IDraw,IColor
{
private int sideLengh;
private int locX = 0, locy = 0;
private Rectangle rect = null;
public Square(int sideLength)
{
this.sideLengh = sideLength;
}
void IDraw.SetLocation(int xCoord, int yCoord)
{
// throw new NotImplementedException();
this.locX = xCoord;
this.locy = yCoord;
}
void IDraw.Draw(Canvas canvas)
{
//throw new NotImplementedException();
if (this.rect != null)
{
canvas.Children.Remove(this.rect);
}
else
{
this.rect = new Rectangle();
}
this.rect.Height = this.sideLengh;
this.rect.Width = this.sideLengh;
Canvas.SetTop(this.rect,this.locy);
Canvas.SetLeft(this.rect,this.locX);
canvas.Children.Add(rect);
}
void IColor.SetColor(Color color)
{
//throw new NotImplementedException();
if (rect != null)
{
SolidColorBrush brush = new SolidColorBrush(color);
rect.Fill = brush;
}
}
}
}
画圆类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
namespace Drawing
{
class Circle:IDraw,IColor
{
private int radius;
private int locX = 0, locY = 0;
private Ellipse circle = null;
public Circle(int radius)
{
this.radius = radius;
}
void IDraw.SetLocation(int xCoord, int yCoord)
{
this.locX = xCoord;
this.locY = yCoord;
}
void IDraw.Draw(Canvas canvas)
{
if (this.circle != null)
{
canvas.Children.Remove(this.circle);
}
else
{
this.circle = new Ellipse();
}
this.circle.Height = this.radius;
this.circle.Width = this.radius;
Canvas.SetTop(this.circle, this.locY);
Canvas.SetLeft(this.circle, this.locX);
canvas.Children.Add(circle);
}
void IColor.SetColor(Color color)
{
if (circle != null)
{
SolidColorBrush brush = new SolidColorBrush(color);
circle.Fill = brush;
}
}
}
}
鼠标事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Drawing
{
public partial class DrawingPadWindow : Window
{
public DrawingPadWindow()
{
InitializeComponent();
}
private void drawingCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)//鼠标点击事件
{
Point mouseLocation=e.GetPosition(this.drawingCanvas);
Square mySquare = new Square(100);
if (mySquare is IDraw)
{
IDraw drawSquare = mySquare;
drawSquare.SetLocation((int)mouseLocation.X,(int)mouseLocation.Y);
drawSquare.Draw(drawingCanvas);
}
if (mySquare is IColor)
{
IColor colorSquare = mySquare;
colorSquare.SetColor(Colors.White);
}
}
private void drawingCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)//鼠标右击事件
{
Point mouseLocation = e.GetPosition(this.drawingCanvas);
Circle myCircle = new Circle(100);
if (myCircle is IDraw)
{
IDraw drawCircle = myCircle;
drawCircle.SetLocation((int)mouseLocation.X, (int)mouseLocation.Y);
drawCircle.Draw(drawingCanvas);
}
if (myCircle is IColor)
{
IColor colorCircle = myCircle;
colorCircle.SetColor(Colors.Red);
}
}
}
}