将java小程序代码转换为c

将java小程序代码转换为c,java,c#,winforms,graphics,Java,C#,Winforms,Graphics,我想在win表单应用程序中设置排序算法的动画 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 = getGraphi

我想在win表单应用程序中设置排序算法的动画

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); 
      }
  }
我已经将上面的代码转换成等价的c代码,如下所示。下面的代码编译和运行良好,但我没有在屏幕上看到任何动画

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)
        {


        }


    }
}
试试这个:g=CreateGraphics;要搬到这里:

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

    }

我只是不知道什么控件绑定到图形对象。

我认为您需要阅读一些关于在中访问UI控件的文档threads@EZI:我没有得到任何跨线程异常。您应该添加一个重画标志,在任何排序迭代/更改中设置它,并在onidle事件或内部计时器上读取清除它(如果为true),然后清除它并重画场景。别忘了在这个标志中添加互斥锁,如果C++中存在这样的状态,那么它就好像是在C++中的易失性。C@Rayn:相同的结果点显示在from上,但没有动画关于动画本身:如果你想这样做,你应该使用,例如,具有适当间隔的System.Windows.Forms.Timer对象,并手动重新绘制每个勾号事件的ur点。好的,让我试试