使用画布在j2me中进行游戏复制 [英] game apllication in j2me using canvas

查看:65
本文介绍了使用画布在j2me中进行游戏复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用画布类在j2me中创建游戏应用程序
发送给我源代码

how to create game application in j2me using canvas class
send to me source code

推荐答案

以下是一些源代码:

Here''s some source code:

    private void BuildBitmap()
    {
        // load our files
        string camoPatternName = System.IO.Path.Combine(Application.StartupPath, "camomask_01.png");
        string outlineName     = System.IO.Path.Combine(Application.StartupPath, "ar15outline_01.png");
        string maskName        = System.IO.Path.Combine(Application.StartupPath, "ar15mask_01.png");
        Bitmap bmpPattern      = Bitmap.FromFile(camoPatternName) as Bitmap;
        Bitmap bmpOutline      = Bitmap.FromFile(outlineName) as Bitmap;
        Bitmap bmpMask         = Bitmap.FromFile(maskName) as Bitmap;
        Bitmap bmpResult       = new Bitmap(bmpPattern);
        // set the resolution of the result bmp
        bmpResult.SetResolution(bmpMask.HorizontalResolution, bmpMask.VerticalResolution);
        double percent = 40;
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#FFFF00"), Color.Tan);
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#00FF00"), Color.DarkGreen);
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#0000FF"), Color.Brown);
        //ReplaceColor(ref bmpResult, ColorTranslator.FromHtml("#FF0000"), Color.DarkKhaki);
        Color[] oldColors = {ColorTranslator.FromHtml("#FFFF00"),
                             ColorTranslator.FromHtml("#00FF00"),
                             ColorTranslator.FromHtml("#0000FF"),
                             ColorTranslator.FromHtml("#FF0000")};
        //Color[] newColors =   {Color.Tan,
        //                     Color.DarkGreen,
        //                     Color.Brown,
        //                     Color.DarkKhaki};
        Color[] newColors = {Color.Tan,
                             Color.DarkGreen};
        ReplaceColors(ref bmpResult, oldColors, newColors);
        using (Graphics g = Graphics.FromImage(bmpResult))
        {
            g.CompositingMode = CompositingMode.SourceOver;
            g.DrawImageUnscaled(bmpMask, 0, 0);
            g.CompositingMode = CompositingMode.SourceOver;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.DrawImageUnscaled(bmpOutline, 0, 0);
            bmpResult.MakeTransparent(ColorTranslator.FromHtml("#FF00FF"));
        }
        ShowStep(bmpResult.Resize(percent));
        bmpPattern.Dispose();
        bmpOutline.Dispose();
        bmpMask.Dispose();
        bmpResult.Dispose();
    }
    /// <summary>
    /// GetPixel/SetPixel are so slow as to be distracting. This method is MUCH better.
    /// It uses a ColorMap object to define the color replacement, and can even be used
    /// to effect a single call to this method to replace the colors. SO COOL!
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="a"></param>
    /// <param name="b"></param>
    public void ReplaceColor(ref Bitmap bmp, Color a, Color b)
    {
        ColorMap Map = new ColorMap();
        Map.OldColor = a;
        Map.NewColor = b;
        ColorMap[] ReMapTable = { Map };
        ImageAttributes Attributes = new ImageAttributes();
        using (Graphics g = Graphics.FromImage(bmp))
        {
            Attributes.SetRemapTable(ReMapTable, ColorAdjustType.Bitmap);
            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attributes);
        }
    }
    //--------------------------------------------------------------------------------
    /// <summary>
    /// Replaces all of the specified old colors with the specified new colors. This
    /// method also accomodates the ocurrence where there are more old colors than
    /// new colors.
    /// </summary>
    /// <param name="bmp"></param>
    /// <param name="oldColors"></param>
    /// <param name="newColors"></param>
    public void ReplaceColors(ref Bitmap bmp, Color[] oldColors, Color[] newColors)
    {
        ColorMap[] ReMapTable = new ColorMap[oldColors.Length];
        int newIndex = -1;
        //int oldIndex = -1;
        for (int  i = 0; i  < oldColors.Length; i++)
        {
            newIndex++;
            //oldIndex++;
            //if (oldIndex >= newColors.Length)
            if (i >= newColors.Length)
            {
                newIndex = 0;
            }
            ReMapTable[i] = new ColorMap(){OldColor = oldColors[i], NewColor = newColors[newIndex]};
        }
        ImageAttributes Attributes = new ImageAttributes();
        using (Graphics g = Graphics.FromImage(bmp))
        {
            Attributes.SetRemapTable(ReMapTable, ColorAdjustType.Bitmap);
            g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, Attributes);
        }
    }
    private void ShowStep(Image img)
    {
        pictureBox1.Image = null;
        pictureBox1.Image = img;
        pictureBox1.Refresh();
    }
}

public class MainClass {
    private List<Item> items;
    public List<Item> Items
    {
        get { return items; }
        set { items = value; }
    }
    public MainClass()
    {
        this.Items = new List<Item>();
    }
    private int id;
    public int Id {
        get {
            return id;
        }
        set {
            id = value;
        }
    }
}
public class Item {
    private string isc;
    public string IsC
    {
        get
        {
            return isc;
        }
        set
        {
            isc = value;
        }
    }
}



如果需要的话,我还有更多的源代码.



I''ve got more source code if you want it.


这篇关于使用画布在j2me中进行游戏复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆