📄 mandelbrotset.java
字号:
/**
*
* Program : MandelbrotSet.java (Example Application Program)
*
* Author : Vijayakrishnan Menon
*
* Date : 11th May 2006
*
* Organization : Centre for Excellence in
* Computational Engineering and Networking (CEN),
* Amrita Viswa Vidyapeetham
*
**/
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.Graphics;
import JAMPack.JComm;
import JAMPack.JScheduler;
import JAMPack.JGrid;
import JAMPack.Task;
import JAMPack.Comm;
import JAMPack.JBootStrap;
public class MandelbrotSet implements Task {
private JGrid _grid;
public static final int X_SIZE = 700;
public static final int Y_SIZE = 700;
public void runTask(int rank,int size) {
double z0_R, z0_I, z1_R = 0, z1_I = 0, c_R, c_I;
double xCord, yCord, xCordStart, xInc, yInc;
int xScreen, yScreen, xScreenStart, xScreenEnd, intensity;
xInc = 4.00/(double)X_SIZE;
yInc = 4.00/(double)Y_SIZE;
xScreenStart = rank*X_SIZE/size;
xScreenEnd = (rank+1)*X_SIZE/size;
System.out.print("The value xS: "+xScreenStart+" xE: "+xScreenEnd);
int [][]pixelMap = new int [xScreenEnd-xScreenStart][Y_SIZE];
xCordStart = (-2.00 + xInc*(double)xScreenStart);
System.out.print("The value xCS: "+xCordStart);
Comm comm = new JComm();
for(xCord = xCordStart, xScreen = xScreenStart; xScreen < xScreenEnd; xCord += xInc, xScreen++)
{
c_R = xCord;
for(yCord = 2.00, yScreen = 0; yScreen < Y_SIZE ; yCord -= yInc, yScreen++)
{
z0_R = z0_I = 0;
c_I = yCord;
pixelMap[xScreen-xScreenStart][yScreen] = intensity = 0;
while(intensity < 128)
{
z1_R = (z0_R*z0_R) - (z0_I*z0_I) + c_R;
z1_I = (2*z0_R*z0_I) + c_I;
if (Math.sqrt((z1_R*z1_R) +(z1_I*z1_I)) >= 2.00 )
break;
z0_R = z1_R;
z0_I = z1_I;
intensity++;
}
pixelMap[xScreen-xScreenStart][yScreen] = intensity;
}
}
try {
if(rank == 0)
{
int pixelMajor[][] = new int[X_SIZE][Y_SIZE];
int iMajor=0;
for(int i=0;i<pixelMap.length;i++,iMajor++)
for(int j=0; j<pixelMap[i].length;j++)
pixelMajor[iMajor][j] = pixelMap[i][j];
if(size>1)
{
for(int nodes=1; nodes<size; nodes++)
{
int pixMap[][] = (int [][])comm.receive((Object)pixelMap,nodes,_grid,1);
iMajor++;
for(int i=0;i< pixelMap.length-1;i++,iMajor++)
for(int j=0; j<pixelMap[i].length;j++)
pixelMajor[iMajor][j] = pixMap[i][j];
}
}
JFrame x = new MFrame(pixelMajor);
}
else comm.send((Object)pixelMap,0,_grid,1);
}catch (Exception e) {e.printStackTrace();}
}
public void setGrid(JGrid grid){
this._grid = grid;
}
public static void main(String args[]) throws Exception {
JScheduler scheduler = JScheduler.getInstance("Hosts.txt");
Task task = JBootStrap.getBootStrapedTask("MandelbrotSet");
JScheduler.post(task,3);
}
}
class MFrame extends JFrame
{
BufferedImage fractal = new BufferedImage (MandelbrotSet.X_SIZE,MandelbrotSet.Y_SIZE,BufferedImage.TYPE_INT_RGB);
JComponent canvas;
WritableRaster raster;
public MFrame(int [][]pix)
{
try {
jbInit ();
this.setVisible (true);
raster = fractal.getRaster ();
for(int x=0;x<MandelbrotSet.X_SIZE;x++)
for(int y=0;y<MandelbrotSet.Y_SIZE;y++)
raster.setPixel (x,y,colorMap(pix[x][y]));
this.repaint ();
}catch (Exception e) { e.printStackTrace(); }
}
public void jbInit() throws Exception
{
this.setSize (MandelbrotSet.X_SIZE,MandelbrotSet.Y_SIZE);
canvas = new JComponent() {
public void paint(Graphics g)
{ g.drawImage(fractal,0,0,null); }
};
canvas.setSize (MandelbrotSet.X_SIZE,MandelbrotSet.Y_SIZE);
this.getContentPane ().add (canvas);
this.setDefaultCloseOperation (this.DISPOSE_ON_CLOSE);
}
public int[] colorMap(int i)
{
int []x = new int[3];
x[0] = i;
x[1] = i*12;
x[2] = 1 ;
return x;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -