2018年5月10日木曜日

開発環境

Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の10章(コントロールとグラフィックス - 見栄えをよくする)、エクササイズ(p. 458)を取り組んでみる。

コード

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;

        private List<Flower> deadFlowers = new List<Flower>();
        private List<Bee> retiredBees = new List<Bee>();

        private Bitmap hiveInside;
        private Bitmap hiveOutside;
        private Bitmap flower;
        Bitmap[] beeAnimationLarg = {
            ResizeImage(Properties.Resources.Bee_animation_1, 40, 40),
            ResizeImage(Properties.Resources.Bee_animation_2, 40, 40),
            ResizeImage(Properties.Resources.Bee_animation_3, 40, 40),
            ResizeImage(Properties.Resources.Bee_animation_4, 40, 40),
        };
        Bitmap[] beeAnimationSmall = {
            ResizeImage(Properties.Resources.Bee_animation_1, 20, 20),
            ResizeImage(Properties.Resources.Bee_animation_2, 20, 20),
            ResizeImage(Properties.Resources.Bee_animation_3, 20, 20),
            ResizeImage(Properties.Resources.Bee_animation_4, 20, 20),
        };
        private int cell = 0;
        private int frame = 0;

        public Renderer(World world, HiveForm hiveForm, FieldForm fieldForm)
        {
            this.world = world;
            this.hiveForm = hiveForm;
            this.fieldForm = fieldForm;
            InitializeImage();
        }

        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 void InitializeImage()
        {
            hiveInside = ResizeImage(
                Properties.Resources.Hive__inside_,
                hiveForm.ClientRectangle.Width,
                hiveForm.ClientRectangle.Height);
            hiveOutside = ResizeImage(
                Properties.Resources.Hive__outside_, 85, 100);
            flower = ResizeImage(
                Properties.Resources.Flower, 75, 75);
        }
        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();
        }
        
        public void PaintHive(Graphics g)
        {
            g.FillRectangle(Brushes.SkyBlue, hiveForm.ClientRectangle);
            g.DrawImageUnscaled(hiveInside, 0, 0);
            foreach (Bee bee in world.Bees)
            {
                if (bee.InsideHive)
                {
                    g.DrawImageUnscaled(
                        beeAnimationLarg[cell],
                        bee.Location.X,
                        bee.Location.Y);
                }
            }
        }

        public void PaintField(Graphics g)
        {
            g.FillRectangle(Brushes.SkyBlue, 0, 0,
                fieldForm.ClientSize.Width,
                fieldForm.ClientSize.Height / 2);
            g.FillRectangle(Brushes.Green,
                0, fieldForm.ClientSize.Height / 2,
                fieldForm.ClientSize.Width,
                fieldForm.ClientSize.Height / 2);
            g.FillEllipse(Brushes.Yellow, new Rectangle(50, 20, 50, 50));
            using (Pen pen = new Pen(Brushes.Brown))
            {
                g.DrawLine(pen,
                    fieldForm.ClientSize.Width - 100, 0,
                    fieldForm.ClientSize.Width - 100, 20);
            }
            g.DrawImageUnscaled(hiveOutside,
                                fieldForm.ClientSize.Width - 140, 20);
            foreach (Flower f in world.Flowers)
            {
                g.DrawImageUnscaled(flower, 
                    f.Location.X, f.Location.Y);
            }
            foreach (Bee bee in world.Bees)
            {
                if (!bee.InsideHive)
                {
                    g.DrawImageUnscaled(
                        beeAnimationLarg[cell],
                        bee.Location.X,
                        bee.Location.Y);
                }
            }
        }
    }
}

0 コメント:

コメントを投稿

関連コンテンツ