0

我正在尝试将此代码从 java 转换为 C#(位于此处

我有一些 winform 经验,但在 winform 应用程序上绘制像素的经验不多。

我相当有信心我可以转换大多数子方法我只是不清楚我将如何在屏幕上绘制单个像素

任何将 java 转换为 c# 的帮助或工具都会受到极大的欢迎

// Buddhabrot
// j.tarbell   January, 2004
// Albuquerque, New Mexico
// complexification.net

// based on code by Paul Bourke
// astronomy.swin.edu.au/~pbourke/

// Processing 0085 Beta syntax update
// j.tarbell   April, 2005

int dim = 800;             // screen dimensions (square window)
int bailout = 200;         // number of iterations before bail
int plots = 10000;        // number of plots to execute per frame (x30 = plots per second)

// 2D array to hold exposure values
int[] exposure = new int[dim*dim];
int maxexposure;           // maximum exposure value
int time = 0;
int exposures = 0;

boolean drawing;
PFont metaBold;

//  MAIN ----------------------------------------------------------------

void setup() {
  // set up drawing area
  size(800,800,P3D);
  background(0);
  // take it nice and easy
  framerate(15);
  // load typeface
  metaBold = loadFont("Arial-48.vlw");
}

void draw() {
  plotPlots();
  time++;
  if (time%30==0) {
    // show progress every 2 seconds or so...
    findMaxExposure();
    renderBrot();
    // show exposure value
    fill(255);
    noStroke();
    textFont(metaBold, 14);
    text("bailout:  "+bailout+"    exposures: "+exposures, 5, dim-6);
  }
}

void plotPlots() {
  float x, y;
  // iterate through some plots
  for (int n=0;n<plots;n++) {
    // Choose a random point in same range
    x = random(-2.0,1.0);
    y = random(-1.5,1.5);
    if (iterate(x,y,false)) {
      iterate(x,y,true);
      exposures++;
    }
  }
}

void renderBrot() {
  // draw to screen
  for (int i=0;i<dim;i++) {
    for (int j=0;j<dim;j++) {
      float ramp = exposure[i*dim+j] / (maxexposure / 2.5);
      // blow out ultra bright regions
      if (ramp > 1)  {
        ramp = 1;
      }
      color c = color(int(ramp*255), int(ramp*255), int(ramp*255));
      set(j,i,c);
    }
  }
}

//   Iterate the Mandelbrot and return TRUE if the point exits
//   Also handle the drawing of the exit points
boolean iterate(float x0, float y0, boolean drawIt) {
  float x = 0;
  float y = 0;
  float xnew, ynew;
  int ix,iy;

  for (int i=0;i<bailout;i++) {
    xnew = x * x - y * y + x0;
    ynew = 2 * x * y + y0;
    if (drawIt && (i > 3)) {
      ix = int(dim * (xnew + 2.0) / 3.0);
      iy = int(dim * (ynew + 1.5) / 3.0);
      if (ix >= 0 && iy >= 0 && ix < dim && iy < dim) {
        // rotate and expose point
        exposure[ix*dim+iy]++;
      }

    }
    if ((xnew*xnew + ynew*ynew) > 4) {
      // escapes
      return true;
    }
    x = xnew;
    y = ynew;
  }
  // does not escape
  return false;
}

void findMaxExposure() {
  // assume no exposure
  maxexposure=0;
  // find the largest density value
  for (int i=0;i<dim;i++) {
    for (int j=0;j<dim;j++) {
      maxexposure = max(maxexposure,exposure[i*dim+j]);
    }
  }
}

// Buddhabrot
// j.tarbell   January, 2004
4

7 回答 7

5

这是一个类似的 SO 问题:Tools to assistant

这是一个MS 发布的用于将 Java 转换为 C# 的工具

于 2009-06-25T20:03:12.517 回答
4

查看 System.Drawing 和 Graphics 类。

编辑:只是为了消除混乱,OP的代码不是Java。这是处理。它在评论中这么说,但你也可以说出来,因为没有类定义,没有导入,而且调用不是 Java 调用。它编译为字节码,并与 Java 互操作,但它不是 Java——没有自动转换会有所帮助。

于 2009-06-25T20:00:51.100 回答
2

如果您试图在保持高帧率的同时操作像素并绘制到屏幕上,您可能会想要使用 lockbits 和 unlockbits 查看 System.Drawing.Bitmap。

一个很好的解释可以在这里找到。否则,您将无法以任何不错的速度进行像素级编辑。

于 2009-06-25T20:05:20.000 回答
2

System.Drawing命名空间中包含各种图形内容。并且有一些关于它的教程

于 2009-06-25T20:02:18.603 回答
1

如果您访问processing.org并下载 Processing 软件,您可以将此代码复制到 IDE 中。然后,如果您执行“文件>导出”,它将创建程序的 Java 小程序版本,并带有 Java 源代码。然后也许你可以运行一些其他的转换器来获取 C# 代码。

(请注意,此代码似乎是处理的旧版本;我必须将“framerate”更改为“frameRate”才能运行。此外,我必须执行命令“Tools>Create Font...”来创建需要的“Arial-48.vlw”字体文件。)

于 2009-12-03T20:01:42.067 回答
1

要绘制一个单独的像素,您可以使用:

e.Graphics.FillRectangle(aBrush, x, y, 1, 1);

你可能想看看这里

于 2009-06-25T20:02:08.277 回答
-1

我们在玩猜猜基类吗?:)

原始代码似乎不是完整的java,有全局变量和自由函数。它是类声明的内容吗?

问题是,Java 代码使用的是什么框架?因为如果它是一个花哨的双缓冲系统,那么通过直接在屏幕上绘制像素,您可能不会在 Winforms 中获得相同的结果。

您可能会通过谷歌搜索关键字获得最佳结果:DirectX C#

更新: OP 不知道它是什么语言。

于 2009-06-25T20:05:14.447 回答