c#的图形绘制,怎么弄啊,老师给了代码,改了出不来图像

啦啦啦_ 发布于 2013/04/21 13:17
阅读 273
收藏 0

在校大学生,只是c#是限选课,所以老师教得有些蜻蜓点水,

都没怎么教基础的,(有一定的c语言基础),

但还是经常听得蒙蒙的,

有什么好的方法可以自己学得比较全面的嘛?

第二次上机布置来了一个作业(绘制图形),自己把老师的代码改了,结果出不来图像。

代码是这样的:

 

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Rect r;

        private Shape s;

        public Form1()
        {
            InitializeComponent();

            r = new Rect(CreateGraphics(), 10, 10, 20, 20);

            r.setColor(Color.Red, Color.Blue, this.BackColor);

            s = r;

            s.SetActive(true);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            r.Draw();

        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '6') s.MoveRight();
            else if (e.KeyChar == '4') s.MoveLeft();
            else if (e.KeyChar == '8') s.MoveUp();
            else if (e.KeyChar == '2') s.MoveDown();

        }

        private void Form1_Load(object sender, EventArgs e) /*这个方法是空的,有什么用处啊? */
        {

        }
    }

    public abstract class Shape
    {
        protected int x, y;
        protected bool actived;

        protected Graphics g;
        protected Color activeColor, normalColor, eraseColor;

        public abstract void Erase();
        public abstract void Draw();

        public void SetActive(bool actived)
        {
            this.actived = actived;

            Erase();
            Draw();
        }

        public void setColor(Color activeColor, Color normalColor, Color eraseColor)
        {
            this.activeColor = activeColor;
            this.normalColor = normalColor;
            this.eraseColor = eraseColor;
        }

        public void MoveLeft()
        {
            Erase();
            x = x - 5;
            Draw();
        }
        public void MoveRight()
        {
            Erase();
            x = x + 5;
            Draw();
        }
        public void MoveUp()
        {
            Erase();
            y = y - 5;
            Draw();
        }
        public void MoveDown()
        {
            Erase();
            y = y + 5;
            Draw();
        }
    }

    public class Rect : Shape
    {
        int width, height;

        public Rect(Graphics g, int x, int y, int width, int height)
        {
            actived = false;

            this.g = g;
            this.x = x;
            this.y = x;
            this.width = width;
            this.height = height;
        }

        public override void Erase()
        {
            SolidBrush sb = new SolidBrush(this.eraseColor);
            g.FillRectangle(sb, new Rectangle(x, y, width, height));
            sb.Dispose();
        }

        public override void Draw()
        {
            SolidBrush sb = new SolidBrush(actived ? activeColor : normalColor);
            g.FillRectangle(sb, new Rectangle(x, y, width, height));
            sb.Dispose();
        }
    }
}

求各位大神指点路。

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部