⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mandebrottest.java

📁 Core Java 2中awt高级特性图像处理中显示Mandebrot Set的小例子
💻 JAVA
字号:
import java.awt.*;
import java.awt.image.*;

import javax.swing.*;

/**
 * 
 * @author 魏超 This programe demostrates how to build up an image from individual
 *         pixels.
 * 
 */

public class MandebrotTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame frame = new MandelbrotFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

/**
 * This frame shows an image with a Mandelbrot set.
 */
@SuppressWarnings("serial")
class MandelbrotFrame extends JFrame {
	public MandelbrotFrame() {
		setTitle("MandelbrotTest");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		BufferedImage image = makeMandelbrot(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		add(new JLabel(new ImageIcon(image)), BorderLayout.CENTER);
	}

	/**
	 * Makes the Mandelbrot image.
	 * 
	 * @param width
	 *            the width
	 * @param height
	 *            the height
	 * @return the image
	 */
	private 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;
		do {
			double xnew = x * x - y * y + a;
			double ynew = 2 * x * y + b;
			x = xnew;
			y = ynew;
			iterations++;
			if (iterations == MAX_ITERATIONS)
				return false;
		} while (x <= 2 && y <= 2);
		return true;
	}

	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 DEFAULT_WIDTH = 400;

	private static final int DEFAULT_HEIGHT = 400;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -