-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathRasterImageFrame.java
More file actions
68 lines (61 loc) · 1.88 KB
/
Copy pathRasterImageFrame.java
File metadata and controls
68 lines (61 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package rasterImage;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
* This frame shows an image with a Mandelbrot set.
*/
public class RasterImageFrame extends JFrame
{
private static final double XMIN = -2;
private static final double XMAX = 2;
private static final double YMIN = -2;
private static final double YMAX = 2;
private static final int MAX_ITERATIONS = 16;
private static final int IMAGE_WIDTH = 400;
private static final int IMAGE_HEIGHT = 400;
public RasterImageFrame()
{
BufferedImage image = makeMandelbrot(IMAGE_WIDTH, IMAGE_HEIGHT);
add(new JLabel(new ImageIcon(image)));
pack();
}
/**
* Makes the Mandelbrot image.
* @param width the width
* @parah height the height
* @return the image
*/
public BufferedImage makeMandelbrot(int width, int height)
{
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = image.getRaster();
ColorModel model = image.getColorModel();
Color fractalColor = Color.red;
int argb = fractalColor.getRGB();
Object colorData = model.getDataElements(argb, null);
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
double a = XMIN + i * (XMAX - XMIN) / width;
double b = YMIN + j * (YMAX - YMIN) / height;
if (!escapesToInfinity(a, b)) raster.setDataElements(i, j, colorData);
}
return image;
}
private boolean escapesToInfinity(double a, double b)
{
double x = 0.0;
double y = 0.0;
int iterations = 0;
while (x <= 2 && y <= 2 && iterations < MAX_ITERATIONS)
{
double xnew = x * x - y * y + a;
double ynew = 2 * x * y + b;
x = xnew;
y = ynew;
iterations++;
}
return x > 2 || y > 2;
}
}