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.cmd.mandel;
020:
021: import java.io.*;
022: import java.awt.*;
023: import java.awt.image.*;
024: import javax.swing.*;
025:
026: import org.apache.log4j.*;
027:
028: import org.homedns.dade.jcgrid.*;
029: import org.homedns.dade.jcgrid.cmd.*;
030: import org.homedns.dade.jcgrid.worker.*;
031: import org.homedns.dade.jcgrid.worker.impl.mandel.*;
032:
033: public class guiMandelWorkerPannel extends javax.swing.JDialog {
034: private final static String className = guiMandelWorkerPannel.class
035: .getName();
036: private static Logger log = Logger.getLogger(className);
037: private static Logger logDetail = Logger.getLogger("DETAIL."
038: + className);
039:
040: private static final long serialVersionUID = 1L;
041:
042: //--------------------------------------------------------------------------
043:
044: private class WorkerFeedback implements GridWorkerFeedback {
045: private Color[] colorTable;
046: private String lastSessionName;
047:
048: public WorkerFeedback() {
049: }
050:
051: public void setMaxIter(int maxIter) {
052: colorTable = new Color[maxIter];
053:
054: float r;
055: float g;
056: float b;
057:
058: float dr;
059: float dg;
060: float db;
061:
062: r = 1.0f;
063: g = 0.0f;
064: b = 0.0f;
065:
066: dr = 0.11f;
067: dg = 0.001f;
068: db = 0.001f;
069:
070: for (int i = 0; i < maxIter - 1; i++) {
071: colorTable[i] = new Color(r, g, b);
072:
073: r += dr;
074: g += dg;
075: b += db;
076:
077: if (r > 1.0)
078: r -= 1.0;
079: if (g > 1.0)
080: g -= 1.0;
081: if (b > 1.0)
082: b -= 1.0;
083: }
084:
085: colorTable[maxIter - 1] = Color.BLACK;
086: }
087:
088: public void beginWorkingFor(String sessionName, WorkRequest req) {
089: lStatus.setText("working for " + sessionName);
090: lastSessionName = sessionName;
091:
092: MandelWorkRequest r = (MandelWorkRequest) req;
093: this .setMaxIter(r.getMaxIter());
094: }
095:
096: public void endWorkingFor(WorkResult res) {
097: lStatus.setText("done work for " + lastSessionName);
098:
099: Graphics graph = guiMandelWorkerPannel.this .pImage
100: .getGraphics();
101:
102: if (graph != null) {
103: MandelWorkResult r = (MandelWorkResult) res;
104:
105: graph.setPaintMode();
106: int w = guiMandelWorkerPannel.this .pImage.getWidth();
107: int h = guiMandelWorkerPannel.this .pImage.getHeight();
108: int fw = r.getIter().length;
109: int fh = r.getIter()[0].length;
110: for (int y = 0; y < fh; y++) {
111: for (int x = 0; x < fw; x++) {
112: graph.setColor(colorTable[((r.getIter())[x][y])
113: % colorTable.length]);
114:
115: graph.fillRect(w / 2 - fw / 2 + x, h / 2 - fh
116: / 2 + (fh - y), 1, 1);
117: }
118: }
119: }
120: }
121:
122: public void start() {
123: }
124:
125: public void stop() {
126: }
127: }
128:
129: //--------------------------------------------------------------------------
130:
131: private GridWorker worker;
132:
133: /** Creates new form guiMandelWorkerPannel */
134: public guiMandelWorkerPannel(GridNodeWorkerConfig workerConfig,
135: java.awt.Frame parent, boolean modal) {
136: super (parent, modal);
137:
138: initComponents();
139:
140: worker = new GridWorker();
141: worker.setNodeConfig(workerConfig);
142: worker.setWorker(new MandelWorker());
143: worker.setWorkerFeedback(new WorkerFeedback());
144:
145: try {
146: worker.start();
147: } catch (Exception ex) {
148: log.warn("Error starting the JCGrid worker", ex);
149:
150: JOptionPane.showMessageDialog(this ,
151: "Error starting the JCGrid worker.",
152: "Worker error", JOptionPane.ERROR_MESSAGE);
153:
154: worker = null;
155: }
156: }
157:
158: /** This method is called from within the constructor to
159: * initialize the form.
160: * WARNING: Do NOT modify this code. The content of this method is
161: * always regenerated by the Form Editor.
162: */
163: private void initComponents() {//GEN-BEGIN:initComponents
164: java.awt.GridBagConstraints gridBagConstraints;
165:
166: pStatus = new javax.swing.JPanel();
167: jLabel9 = new javax.swing.JLabel();
168: lStatus = new javax.swing.JLabel();
169: pImage = new javax.swing.JPanel();
170:
171: getContentPane().setLayout(new java.awt.GridBagLayout());
172:
173: setTitle("JCGrid MandelWorker");
174: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
175: pStatus.setLayout(new java.awt.GridBagLayout());
176:
177: pStatus.setBorder(new javax.swing.border.TitledBorder(
178: "Worker status"));
179: jLabel9.setText("Status:");
180: gridBagConstraints = new java.awt.GridBagConstraints();
181: gridBagConstraints.gridx = 0;
182: gridBagConstraints.gridy = 0;
183: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
184: gridBagConstraints.weightx = 1.0;
185: gridBagConstraints.weighty = 1.0;
186: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
187: pStatus.add(jLabel9, gridBagConstraints);
188:
189: lStatus.setText("...");
190: lStatus.setForeground(java.awt.Color.blue);
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridx = 1;
193: gridBagConstraints.gridy = 0;
194: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
195: gridBagConstraints.weightx = 1.0;
196: gridBagConstraints.weighty = 1.0;
197: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
198: pStatus.add(lStatus, gridBagConstraints);
199:
200: pImage.setBorder(new javax.swing.border.BevelBorder(
201: javax.swing.border.BevelBorder.LOWERED));
202: gridBagConstraints = new java.awt.GridBagConstraints();
203: gridBagConstraints.gridx = 0;
204: gridBagConstraints.gridy = 1;
205: gridBagConstraints.gridwidth = 2;
206: gridBagConstraints.ipadx = 48;
207: gridBagConstraints.ipady = 48;
208: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
209: pStatus.add(pImage, gridBagConstraints);
210:
211: gridBagConstraints = new java.awt.GridBagConstraints();
212: gridBagConstraints.gridx = 0;
213: gridBagConstraints.gridy = 1;
214: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
215: gridBagConstraints.weightx = 1.0;
216: gridBagConstraints.weighty = 1.0;
217: getContentPane().add(pStatus, gridBagConstraints);
218:
219: setBounds(100, 30, 300, 200);
220: }//GEN-END:initComponents
221:
222: // Variables declaration - do not modify//GEN-BEGIN:variables
223: private javax.swing.JLabel jLabel9;
224: private javax.swing.JLabel lStatus;
225: private javax.swing.JPanel pImage;
226: private javax.swing.JPanel pStatus;
227: // End of variables declaration//GEN-END:variables
228:
229: }
|