Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(コントロールとグラフィックス: 見栄えをよくする)、エクササイズ(p.458)を解いてみる。
エクササイズ(p.458)
コード
Renderer.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace _3_Beehive_Simulator__controls_ { public class Renderer { private World world; private HiveForm hiveForm; private FieldForm fieldForm; Bitmap hiveInside; Bitmap hiveOutside; Bitmap flower; Bitmap[] beeAnimationLarge = new Bitmap[4]; Bitmap[] beeAnimationSmall = new Bitmap[4]; private List<Flower> deadFlowers = new List<Flower>(); private List<Bee> retiredBees = new List<Bee>(); public Renderer(World world, HiveForm hiveForm, FieldForm fieldForm) { this.world = world; this.hiveForm = hiveForm; this.fieldForm = fieldForm; InitializeImages(); } public static Bitmap ResizeImage(Bitmap picture, int width, int height) { Bitmap resizedPicture = new Bitmap(width, height); using (Graphics graphics = Graphics.FromImage(resizedPicture)) { graphics.DrawImage(picture, 0, 0, width, height); } return resizedPicture; } private int cell = 0; private int frame = 0; public void AnimateBees() { frame++; if (frame >= 6) frame = 0; switch (frame) { case 0: cell = 0; break; case 1: cell = 1; break; case 2: cell = 2; break; case 3: cell = 3; break; case 4: cell = 2; break; case 5: cell = 1; break; default: cell = 0; break; } hiveForm.Invalidate(); fieldForm.Invalidate(); } private void InitializeImages() { Image[] images = { Properties.Resources.Bee_animation_1, Properties.Resources.Bee_animation_2, Properties.Resources.Bee_animation_3, Properties.Resources.Bee_animation_4 }; foreach(Image image in images) { beeAnimationLarge[cell] = new Bitmap(image, new Size(40, 40)); } foreach(Image image in images) { beeAnimationSmall[cell] = new Bitmap(image, new Size(20, 20)); } hiveInside = new Bitmap(Properties.Resources.Hive__inside_, hiveForm.ClientSize); hiveOutside = new Bitmap(Properties.Resources.Hive__outside_, new Size(100, 100)); flower = new Bitmap(Properties.Resources.Flower, new Size(50, 50)); } public void PaintHive(Graphics g) { g.FillRectangle(Brushes.SkyBlue, hiveForm.ClientRectangle); g.DrawImageUnscaled(hiveInside, new Point(0, 0)); foreach (Bee bee in world.Bees) { if (bee.InsideHive) { g.DrawImageUnscaled(beeAnimationSmall[cell], new Point(bee.Location.X, bee.Location.Y)); } } } public void PaintField(Graphics g) { g.FillRectangle(Brushes.SkyBlue, new Rectangle(0, 0, fieldForm.ClientSize.Width, fieldForm.ClientSize.Height)); g.FillRectangle(Brushes.Green, new Rectangle(0, fieldForm.ClientSize.Height / 2, fieldForm.ClientSize.Width, fieldForm.ClientSize.Height / 2)); g.FillEllipse(Brushes.Yellow, new Rectangle(new Point(5, 5), new Size(50, 50))); using (Pen pen = new Pen(Brushes.Brown,5.0f)) { g.DrawLine(pen, new Point(fieldForm.Width - 100, 0), new Point(fieldForm.Width - 100, 10)); } g.DrawImageUnscaled(hiveOutside, new Point(fieldForm.Width - 150, 10)); foreach (Flower f in world.Flowers) { g.DrawImageUnscaled(flower, new Point(f.Location.X, f.Location.Y)); } foreach (Bee bee in world.Bees) { g.DrawImageUnscaled(beeAnimationSmall[cell], new Point(bee.Location.X, bee.Location.Y)); } } } }
0 コメント:
コメントを投稿