001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid.worker.impl.mandel;
020:
021: import java.awt.*;
022: import java.awt.image.*;
023: import javax.swing.*;
024: import javax.imageio.*;
025:
026: public class MandelRenderingFrame {
027: public final static int COLOR_TABLE_TYPE_RED = 0;
028: public final static int COLOR_TABLE_TYPE_MISC = 1;
029:
030: private BufferedImage renderingFrameBuffer;
031: private Graphics2D renderingFrameBufferG2D;
032:
033: private int colorTableType;
034: private Color[] colorTable;
035:
036: public MandelRenderingFrame(int width, int height, int iter) {
037: colorTableType = COLOR_TABLE_TYPE_RED;
038:
039: renderingFrameBuffer = new BufferedImage(width, height,
040: BufferedImage.TYPE_3BYTE_BGR);
041: renderingFrameBufferG2D = renderingFrameBuffer.createGraphics();
042:
043: for (int y = 0; y < renderingFrameBuffer.getHeight(); y += 10) {
044: for (int x = 0; x < renderingFrameBuffer.getWidth(); x += 10) {
045: if (((x / 10 + y / 10) % 2) == 0)
046: renderingFrameBufferG2D.setColor(Color.BLACK);
047: else
048: renderingFrameBufferG2D.setColor(Color.WHITE);
049: renderingFrameBufferG2D.fillRect(x, y, 10, 10);
050: }
051: }
052:
053: this .setMaxIter(iter);
054: }
055:
056: public BufferedImage getBufferedImage() {
057: return renderingFrameBuffer;
058: }
059:
060: public Graphics2D getGraphics2D() {
061: return renderingFrameBufferG2D;
062: }
063:
064: public void setColorTableType(int type) {
065: colorTableType = type;
066: }
067:
068: public void setMaxIter(int maxIter) {
069: colorTable = new Color[maxIter];
070:
071: float r;
072: float g;
073: float b;
074:
075: float dr;
076: float dg;
077: float db;
078:
079: if (colorTableType == COLOR_TABLE_TYPE_MISC) {
080: r = 1.0f;
081: g = 0.0f;
082: b = 0.0f;
083:
084: dr = 0.23f;
085: dg = 0.37f;
086: db = 0.41f;
087: } else {
088: r = 1.0f;
089: g = 0.0f;
090: b = 0.0f;
091:
092: dr = 0.11f;
093: dg = 0.001f;
094: db = 0.001f;
095: }
096:
097: for (int i = 0; i < maxIter - 1; i++) {
098: colorTable[i] = new Color(r, g, b);
099:
100: r += dr;
101: g += dg;
102: b += db;
103:
104: if (r > 1.0)
105: r -= 1.0;
106: if (g > 1.0)
107: g -= 1.0;
108: if (b > 1.0)
109: b -= 1.0;
110: }
111:
112: colorTable[maxIter - 1] = Color.BLACK;
113: }
114:
115: public void addFragmentResult(MandelWorkRequest req,
116: MandelWorkResult res, MandelRenderingFrame frame) {
117: int height = renderingFrameBuffer.getHeight() - 1;
118:
119: for (int y = 0; y < req.getYStep(); y++) {
120: for (int x = 0; x < req.getXStep(); x++) {
121: renderingFrameBufferG2D.setColor(colorTable[((res
122: .getIter())[x][y])
123: % colorTable.length]);
124:
125: renderingFrameBufferG2D.fillRect(x + req.getXOffset(),
126: height - (y + req.getYOffset()), 1, 1);
127: }
128: }
129: }
130: }
|