Mandelbrot Android 应用程序的颜色渐变不起作用

发布于 2024-11-27 00:47:42 字数 2068 浏览 0 评论 0原文

我正在尝试为 Android 编写 Mandelbrot 集。 我试着把它画在画布上。它有效,但我想要一个颜色渐变。 这是我的代码:

package de.turbofractal;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;


public class FractalView extends View {
   private int n,g,xMax,yMax;
   private float zr,zrh,zr0,zi,zi0,ar,br,ai,bi;
   private int[][] Iterationen = new int[2560][1440];
   private Paint paint;

   public FractalView(Context context) {
      super(context);
      paint = new Paint();
      ar=-2; br=2; ai=-2; bi=2;
   }

   @Override
   protected void onDraw(Canvas canvas) {
       g = 20;
       for(int xx = 1; xx <= xMax; xx++) {
           for(int yy = 1; yy <= yMax; yy++) {
               schirmzupap(ar,br,bi,ai,xx,yy,xMax,yMax);
               n = 1;
               zr0 = zr;
               zi0 = zi;
               while ((n<g) && (zr*zr+zi*zi<4)) {
                   zrh = zr;
                   zr = (zr*zr)-zi*zi+zr0;
                   zi = zrh*zi+zi*zrh+zi0;
                   n++;
               }
               paint.setARGB(255,0,0,Math.round((n/g)*255));  //<--------------------this is where the color gradient should be created. But the programm only draws full blue (0,0,255). Why is that?
               canvas.drawPoint(xx, yy, paint);            
           }

        }
        invalidate();
   }      


   private void schirmzupap(float ax,float bx,float ay,float by,int xschirm,int yschirm,int w,int h) {
       zr = xschirm*(bx-ax)/w+ax;
       zi = yschirm*(ay-by)/h+by;
   } 

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {
      xMax = w-1;
      yMax = h-1;
   }
}

编辑: eclipse模拟器是这样绘制的: http://i53.tinypic.com/2mwfs7c.png

但我希望它画得像这个(用Delphi编程): http://i53.tinypic.com/ehg2et.png

I'm trying to to program the Mandelbrot set for Android.
I try to draw it on a Canvas. It works, but I want to have a color gradient.
This is my code:

package de.turbofractal;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;


public class FractalView extends View {
   private int n,g,xMax,yMax;
   private float zr,zrh,zr0,zi,zi0,ar,br,ai,bi;
   private int[][] Iterationen = new int[2560][1440];
   private Paint paint;

   public FractalView(Context context) {
      super(context);
      paint = new Paint();
      ar=-2; br=2; ai=-2; bi=2;
   }

   @Override
   protected void onDraw(Canvas canvas) {
       g = 20;
       for(int xx = 1; xx <= xMax; xx++) {
           for(int yy = 1; yy <= yMax; yy++) {
               schirmzupap(ar,br,bi,ai,xx,yy,xMax,yMax);
               n = 1;
               zr0 = zr;
               zi0 = zi;
               while ((n<g) && (zr*zr+zi*zi<4)) {
                   zrh = zr;
                   zr = (zr*zr)-zi*zi+zr0;
                   zi = zrh*zi+zi*zrh+zi0;
                   n++;
               }
               paint.setARGB(255,0,0,Math.round((n/g)*255));  //<--------------------this is where the color gradient should be created. But the programm only draws full blue (0,0,255). Why is that?
               canvas.drawPoint(xx, yy, paint);            
           }

        }
        invalidate();
   }      


   private void schirmzupap(float ax,float bx,float ay,float by,int xschirm,int yschirm,int w,int h) {
       zr = xschirm*(bx-ax)/w+ax;
       zi = yschirm*(ay-by)/h+by;
   } 

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {
      xMax = w-1;
      yMax = h-1;
   }
}

Edit:
This is how it is drawn by the eclipse emulator:
http://i53.tinypic.com/2mwfs7c.png

But I want it to be drawn like this (programmed with Delphi):
http://i53.tinypic.com/ehg2et.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-12-04 00:47:43
paint.setARGB(255,0,0,Math.round((n/g)*255));  

setARGB 的参数为 alpha、red、green 和 blue - 您只为 alpha 和 blue 参数提供值。

b参数中,ng都是整数参数,所以当你将它们相除时,你会得到一个整数结果,不是双精度。 (参见“Java整数除法,如何产生双精度数? ") 这意味着您只能得到结果的整数部分,这不是您想要的。将 ng 转换为双精度值,它应该可以工作。

paint.setARGB(255,0,0,Math.round((n/g)*255));  

The arguments for setARGB are alpha, red, green, and blue - you are only supplying values to the alpha and blue parameters.

Inside the b parameter, both n and g are integer parameters, so when you divide them, you will get an integer result, not a double. (See "Java Integer Division, How do you produce a double?") This means you only get the whole number part of the result, which isn't what you want. Cast n or g to a double and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文