颜色梯度曼德尔布罗Android应用程序无法正常工作 [英] Color gradient for Mandelbrot Android application doesn't work
问题描述
我试图编程Mandelbrot集为Android。
我试图把它画在画布上。它的工作原理,但我希望有一个颜色渐变。
这是我的code:
包de.turbofractal;进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.view.View;
公共类FractalView扩展视图{
私人诠释N,G,XMAX,YMAX;
私人浮动ZR,ZRH,ZR0,紫,zi0,AR,BR,AI,双向;
私人INT [] [] = Iterationen新INT [2560] [1440]
私人涂料粉刷; 公共FractalView(上下文的背景下){
超级(上下文);
油漆=新的油漆();
AR = -2; BR = 2; AI = -2; BI = 2;
} @覆盖
保护无效的onDraw(帆布油画){
G = 20;
为(中间体XX = 1; XX&下; = XMAX; XX ++){
对于(INT YY = 1; YY< = YMAX; YY ++){
schirmzupap(AR,BR,BI,AI,XX,YY,XMAX,YMAX);
N = 1;
ZR0 = ZR;
zi0 =子;
而((N< G)和放大器;及(ZR * ZR +紫紫* 4;)){
ZRH = ZR;
ZR =(ZR * ZR)-zi *字+ ZR0;
紫= ZRH *子+子* ZRH + zi0;
Ñ++;
}
paint.setARGB(255,0,0,Math.round((N / G)* 255)); //< --------------------这是那里的颜色渐变应创建。但仅PROGRAMM画满蓝(0,0,255)。这是为什么?
canvas.drawPoint(XX,YY,油漆);
} }
无效();
}
私人无效schirmzupap(浮点AX,BX浮球,浮球AY,由浮球,诠释xschirm,诠释yschirm,诠释W,INT高){
ZR = xschirm *(BX-AX)/ W + AX;
紫= yschirm *(AY-BY)由/ H +;
} @覆盖
公共无效onSizeChanged(INT W,INT小时,INT oldW,诠释oldH){
XMAX = W-1;
YMAX = -1;
}
}
编辑:
这是它是如何由Eclipse仿真得出:
http://i53.tinypic.com/2mwfs7c.png
但我想它要绘制这样(用Delphi编程):
http://i53.tinypic.com/ehg2et.png
paint.setARGB(255,0,0,Math.round((N / G)* 255));
为 setARGB
是 A
lpha,研究
版,先按g
颖和 b
攻略 - 你只给阿尔法和蓝色的参数提供值。
在 B
参数,既 N
和先按g
是整型参数,所以,当你把他们,你会得到一个整数结果,的不的双。 (请参见<一个href=\"http://stackoverflow.com/questions/3144610/java-integer-division-how-do-you-produce-a-double\">Java整数除法,你如何制作一张双人床?),这意味着你只能得到的结果,这是不是你想要的整数部分。铸造 N
或先按g
来一张双人床和它应该工作。
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
paint.setARGB(255,0,0,Math.round((n/g)*255));
The arguments for setARGB
are a
lpha, r
ed, g
reen, and b
lue - 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.
这篇关于颜色梯度曼德尔布罗Android应用程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!