using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace WindowsFormsApplication3 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
_shapes = CreateShapes(3);
}
private void Form1_Paint(object sender, PaintEventArgs e) {
DrawShapes(e.Graphics);
}
IEnumerable<Ellipse> CreateShapes(int count) {
ICollection<Ellipse> result = new List<Ellipse>(count);
Random random = new Random();
for (int i = 0; i < count; i++) {
Rectangle rectangle = GetRandomRectanle(random);
result.Add(new Ellipse(rectangle, CreateBrush(GetRandomColor(random), rectangle)));
}
return result;
}
void DrawShapes(Graphics graphics) {
if (_shapes != null) {
foreach (Ellipse shape in _shapes) {
shape.Draw(graphics);
}
}
}
static readonly Color[] _predefined_colors = new Color[] { Color.Blue, Color.Yellow, Color.Red, Color.SandyBrown };
IEnumerable<Ellipse> _shapes;
class Ellipse : IDisposable {
readonly Rectangle _rectangle;
readonly Brush _brush;
bool _disposed;
public Ellipse(Rectangle rectangle, Brush brush) {
_rectangle = rectangle;
_brush = brush;
}
public void Dispose() {
if (_disposed) {
return;
}
_brush.Dispose();
_disposed = true;
GC.SuppressFinalize(this);
}
public void Draw(Graphics graphics) {
graphics.FillEllipse(_brush, _rectangle);
}
}
static LinearGradientBrush CreateBrush(Color color, Rectangle rectangle) {
return new LinearGradientBrush(rectangle.Location, rectangle.Location + rectangle.Size, Color.White, color);
}
static Color GetRandomColor(Random random) {
return _predefined_colors[random.Next(_predefined_colors.Length)];
}
Rectangle GetRandomRectanle(Random random) {
Size client_size = ClientSize;
Point location = new Point(random.Next(client_size.Width), random.Next(client_size.Height));
Size size = new Size(random.Next(client_size.Width - location.X), random.Next(client_size.Height - location.Y));
return new Rectangle(location, size);
}
protected override void Dispose(bool disposing) {
if (disposing) {
DisposeShapes();
DisposeComponents();
}
base.Dispose(disposing);
}
void DisposeShapes() {
foreach (Ellipse shape in _shapes) {
shape.Dispose();
}
}
void DisposeComponents() {
if (components != null) {
components.Dispose();
}
}
}
}
Графика формах
Код
Код:
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;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int cx, cy;
Graphics graph;
Brush brush;
Color color;
Random rnd;
void MyInit()
{
cx = ClientSize.Width;
cy = ClientSize.Height;
graph = CreateGraphics();
rnd = new Random();
}
void DrawShapes()
{
for (int i = 0; i < 3; i++)
{
int numcolor = rnd.Next(3);
switch (numcolor)
{
case 0:
color = Color.Blue; break;
case 1:
color = Color.Yellow; break;
case 2:
color = Color.Red; break;
}
Point top = new Point(rnd.Next(cx), rnd.Next(cy));
Size sz = new Size(rnd.Next(cx - top.X), rnd.Next(cy - top.Y));
Rectangle rct = new Rectangle(top, sz);
Point bottom = top + sz;
brush = new LinearGradientBrush(top, bottom, Color.White, color);
graph.FillEllipse(brush, rct);
}
}
public Form1()
{
InitializeComponent();
MyInit();
}
public void Form1_Paint(object sender, PaintEventArgs e)
{
DrawShapes();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
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;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int cx, cy;
Graphics graph;
Brush brush;
Color color;
Random rnd;
void MyInit()
{
cx = ClientSize.Width;
cy = ClientSize.Height;
graph = CreateGraphics();
rnd = new Random();
}
void DrawShapes()
{
for (int i = 0; i < 3; i++)
{
int numcolor = rnd.Next(3);
switch (numcolor)
{
case 0:
color = Color.Blue; break;
case 1:
color = Color.Yellow; break;
case 2:
color = Color.Red; break;
}
Point top = new Point(rnd.Next(cx), rnd.Next(cy));
Size sz = new Size(rnd.Next(cx - top.X), rnd.Next(cy - top.Y));
Rectangle rct = new Rectangle(top, sz);
Point bottom = top + sz;
brush = new LinearGradientBrush(top, bottom, Color.White, color);
graph.FillEllipse(brush, rct);
}
}
public Form1()
{
InitializeComponent();
MyInit();
}
public void Form1_Paint(object sender, PaintEventArgs e)
{
DrawShapes();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}