简体   繁体   English

将Java Applet代码转换为C#

[英]Convert java applet code to c#

I want to animate sorting algorithm in win form application.I google and found following java applet code 我想在win form application.ani谷歌动画排序算法,我发现以下Java小程序代码

import java.applet.Applet;
import java.awt.*;
abstract public class Animate 
  extends Applet implements Runnable 
  { Graphics g;
    Thread animatorThread;
    int N; double[] a;
    public void start() 
      { g = getGraphics();
        new Thread(this).start(); 
      }
    public void stop() { animatorThread = null; }
    public void run() 
      { N = Integer.parseInt(getParameter("N"));  
        a = new double[N];
        for (int i = 0; i < N; i++) 
          { a[i] = Math.random();
            dot(X(i), Y(a[i]), Color.black); }
        sort(a, 0, N-1);
      }
    int X(int i) 
      { return (i*getSize().width)/N; }
    int Y(double v) 
      { return (1.0 - v)*getSize().height; }
    void dot(int x, int y, Color c) 
      { g.setColor(c); g.fillOval(x, y, 5, 5); }
    void exch(double [] a, int i, int j) 
      { double t = a[i]; a[i] = a[j]; a[j] = t;
        dot(X(i), Y(a[j]), Color.red);
        dot(X(j), Y(a[i]), Color.red);
        dot(X(i), Y(a[i]), Color.black);
        dot(X(j), Y(a[j]), Color.black);
      }
    abstract void sort(double a[], int l, int r); 
}
-----
import java.awt.*;
public class SortAnimate extends Animate
  {
    void sort(double a[], int l, int r) 
      { 
        for (int i = l+1; i <= r; i++)
          for (int j = i; j > l; j--) 
            if (a[j] < a[j-1]) exch(a, j-1, j); 
      }
  }

I have converted the above code into equivalent c# code as below . 我已经将上面的代码转换为等效的c#代码,如下所示。 Following code is compiling and running fine but I am not getting any animation on the screen. 以下代码可以编译并且运行良好,但屏幕上没有任何动画。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnimationEx
{
    public partial class Form1 : Form
    {
        Graphics g;
        Thread thread;
        int N; int[] a;
        public Form1()
        {
            InitializeComponent();
            a = new[] { 1, -11, 11, -1, 4, 1, 13143, 23, 24234, 242, 243, 2424, 2432 };
            N = a.Length - 1;
            g = CreateGraphics();


        }
        int X(int i)
        { return (i * this.ClientRectangle.Width) / N; }
        int Y(int v)
        { return (1 - v) * this.ClientRectangle.Height; }
        private void Run()
        {

            for (int i = 0; i < N; i++)
            {
                dot(X(i), Y(a[i]), Color.Black);
            }
            sort(a, 0, N - 1);
        }

        private void exch(int[] arr, int i, int j)
        {
            int t = a[i]; a[i] = a[j]; a[j] = t;
            dot(X(i), Y(a[j]), Color.Red);
            dot(X(j), Y(a[i]), Color.Red);
            dot(X(i), Y(a[i]), Color.Black);
            dot(X(j), Y(a[j]), Color.Black);

        }

        private void dot(int p1, int p2, Color color)
        {
            float x = p1;
            float y = p2;
            //g.setColor(c); g.fillOval(x, y, 5, 5);
            Brush b = new SolidBrush(color);
            g.FillEllipse(b, x, y, 5, 5);

        }


        void sort(int[] a, int l, int r)
        {
            for (int i = l + 1; i <= r; i++)
                for (int j = i; j > l; j--)
                    if (a[j] < a[j - 1]) exch(a, j - 1, j);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            thread = new Thread(new ThreadStart(Run));
            thread.Start();

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {


        }


    }
}

Try this: g = CreateGraphics(); 试试这个: g = CreateGraphics(); to move here: 移到这里:

private void Form1_Load(object sender, EventArgs e)
    {
        g = Graphics.FromHwnd(this);
        thread = new Thread(new ThreadStart(Run));
        thread.Start();

    }

I just don't see what control is bounded to your Graphics object. 我只是看不到控件绑定到您的Graphics对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM