001 /*
002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package de.kumpe.hadooptimizer.examples.functions;
017
018 import java.awt.Color;
019 import java.awt.FontMetrics;
020 import java.awt.Graphics;
021 import java.awt.Graphics2D;
022 import java.util.Collection;
023 import java.util.concurrent.CopyOnWriteArrayList;
024
025 import de.kumpe.hadooptimizer.EvaluationResult;
026 import de.kumpe.hadooptimizer.impl.ReportingHalterWrapper.Reporter;
027
028 public class ResultReporterPanel extends AxisPanel implements
029 Reporter<double[]> {
030 private static final long serialVersionUID = 1L;
031
032 private final Collection<EvaluationResult<double[]>> results = new CopyOnWriteArrayList<EvaluationResult<double[]>>();
033
034 @Override
035 public void report(
036 final Collection<EvaluationResult<double[]>> evaluationResults) {
037 results.add(evaluationResults.iterator().next());
038 repaint();
039 }
040
041 @Override
042 protected void paintComponent(final Graphics graphics) {
043 super.paintComponent(graphics);
044
045 final Graphics2D g = (Graphics2D) graphics;
046 final FontMetrics fm = g.getFontMetrics();
047
048 g.setColor(Color.RED);
049
050 for (final EvaluationResult<double[]> result : results) {
051 final double[] individual = result.getIndividual();
052 final int x = x(individual[0]);
053 final int y = y(individual[1]);
054
055 g.drawLine(x - 2, y - 2, x + 2, y + 2);
056 g.drawLine(x + 2, y - 2, x - 2, y + 2);
057
058 g.drawString(format(result.getEvaluation()), x + 3,
059 y + fm.getHeight());
060 }
061 }
062 }