All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.mines.jtk.mosaic.PlotPanel Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/****************************************************************************
Copyright 2005, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.mosaic;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.image.IndexColorModel;
import java.util.EnumSet;
import java.util.Set;
import static java.lang.Math.*;

import edu.mines.jtk.awt.ColorMap;
import edu.mines.jtk.awt.ColorMapped;
import edu.mines.jtk.awt.ColorMapListener;
import edu.mines.jtk.dsp.Sampling;
import edu.mines.jtk.util.Check;

/**
 * A plot panel is a panel that contains a mosaic of 2-D graphical views.
 * A plot panel may also contain a color bar and/or title. The plot panel's
 * mosaic may contain any number or rows and columns of tiles. Each tile 
 * may contain any number of tiled graphical views.
 * 

* The primary purpose of this class is ease-of-use. A plot panel handles * much of the work of constructing a mosaic of tiled graphical views. *

* One consequence of ease-of-use is that some of the methods provided * by this class are redundant. For example, some methods have (irow,icol) * parameters that specify the row and column indices of a tile. These * parameters are useful for mosaics with more than one tile. However, the * most common case is a mosaic with only one tile; and, for this case, a * corresponding method without (irow,icol) parameters is provided as well. * The latter method simply calls the former with (irow,icol) = (0,0). *

* An important property of a plot panel is the orientation of its axes. * Tiles have axes x1 and x2. By default, the x1 axis increases toward * the right and the x2 axis increases toward the top of each tile in a * mosaic. In this default X1RIGHT_X2UP orientation, the coordinates * (x1,x2) correspond to conventional (x,y) coordinates. An alternative * orientation is X1DOWN_X2RIGHT, which is useful when the x1 axis * corresponds to, say, a depth coordinate z. * @author Dave Hale, Colorado School of Mines * @version 2009.06.19 */ public class PlotPanel extends IPanel { private static final long serialVersionUID = 1L; /** * Placement of labeled axes in mosaic. The default axes placement * is two axes placed depending on the plot orientation. */ public enum AxesPlacement { LEFT_TOP, LEFT_BOTTOM, NONE } /** * Orientation of axes x1 and x2. For example, the default orientation * X1RIGHT_X2UP corresponds to x1 increasing horizontally from left to * right, and x2 increasing vertically from bottom to top. */ public enum Orientation { X1RIGHT_X2UP, X1DOWN_X2RIGHT } /** * Constructs a new plot panel with a mosaic of one tile. * Uses the default orientation X1RIGHT_X2UP. */ public PlotPanel() { this(1,1,Orientation.X1RIGHT_X2UP); } /** * Constructs a new plot panel with a mosaic of nrow by ncol tiles. * Uses the default orientation X1RIGHT_X2UP. * @param nrow the number of rows. * @param ncol the number of columns. */ public PlotPanel(int nrow, int ncol) { this(nrow,ncol,Orientation.X1RIGHT_X2UP); } /** * Constructs a new plot panel with a mosaic of one tile. * @param orientation the plot orientation. */ public PlotPanel(Orientation orientation) { this(1,1,orientation); } public PlotPanel(int nrow, int ncol, Orientation orientation) { this(nrow,ncol,orientation,axesPlacement(orientation)); } private static AxesPlacement axesPlacement(Orientation orientation) { AxesPlacement axesPlacement; if (orientation==Orientation.X1DOWN_X2RIGHT) { axesPlacement = AxesPlacement.LEFT_TOP; } else { axesPlacement = AxesPlacement.LEFT_BOTTOM; } return axesPlacement; } /** * Constructs a new plot panel with a mosaic of nrow by ncol tiles. * @param nrow the number of rows. * @param ncol the number of columns. * @param orientation the plot orientation. * @param axesPlacement the placement of axes. */ public PlotPanel( int nrow, int ncol, Orientation orientation, AxesPlacement axesPlacement) { super(); _orientation = orientation; _axesPlacement = axesPlacement; setLayout(new GridBagLayout()); Set axesPlacementSet; if (axesPlacement==AxesPlacement.LEFT_TOP) { axesPlacementSet = EnumSet.of( Mosaic.AxesPlacement.LEFT, Mosaic.AxesPlacement.TOP ); } else if (axesPlacement==AxesPlacement.LEFT_BOTTOM) { axesPlacementSet = EnumSet.of( Mosaic.AxesPlacement.LEFT, Mosaic.AxesPlacement.BOTTOM ); } else { axesPlacementSet = EnumSet.noneOf(Mosaic.AxesPlacement.class); } _mosaic = new Mosaic(nrow,ncol,axesPlacementSet); _colorMapHandler = new ColorMapHandler(); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 100; gbc.weighty = 100; gbc.fill = GridBagConstraints.BOTH; add(_mosaic,gbc); setPreferredSize(new Dimension(200+300*ncol,100+300*nrow)); revalidate(); } /** * Gets the mosaic. The mosaic contains one or more tiles. * @return the mosaic. */ public Mosaic getMosaic() { return _mosaic; } /** * Gets the tile with specified row and column indices. * @param irow the row index. * @param icol the column index. * @return the tile. */ public Tile getTile(int irow, int icol) { return _mosaic.getTile(irow,icol); } /** * Adds the color bar with no label. The color bar paints the color map * of the most recently added pixels view. To avoid confusion, a color * bar should perhaps not be added when this plot panel contains multiple * pixels views with different color maps.. * @return the color bar. */ public ColorBar addColorBar() { return addColorBar(null,null); } /** * Adds the color bar with specified label. * @param label the label; null, if none. * @return the color bar. */ public ColorBar addColorBar(String label) { return addColorBar(null,label); } /** * Adds a color bar with a specified color mapped object and no label. * @param cm the specified color mapped. * @return the color bar. */ public ColorBar addColorBar(ColorMapped cm) { return addColorBar(cm,null); } /** * Adds a color bar with a specified color mapped object and label. * If the specified color mapped object is null, then this plot panel * will try to find the best color map to display in the color bar. * @param cm the color mapped object. * @param label the label. * @return the color bar. */ public ColorBar addColorBar(ColorMapped cm, String label) { if (cm!=null) { _autoColorMapped = false; _colorMapped = cm; } else { _colorMapped = findBestColorMapped(); } if (_colorBar==null) { _colorBar = new ColorBar(label); _colorBar.setFont(getFont()); _colorBar.setForeground(getForeground()); _colorBar.setBackground(getBackground()); if (_colorBarFormat!=null) _colorBar.setFormat(_colorBarFormat); if (_colorBarWidthMinimum!=0) _colorBar.setWidthMinimum(_colorBarWidthMinimum); if (_colorMapped!=null) _colorMapped.getColorMap().addListener(_colorBar); add(_colorBar,makeColorBarConstraints()); } else { _colorBar.setLabel(label); } revalidate(); return _colorBar; } /** * Sets a minimum width (in pixels) for a color bar. * This method is useful when attempting to construct multiple plot * panels with the same layout. In this scenario, set this minimum * equal to the width of the widest color bar. Then all color bars * will have the same width. Those widths might otherwise vary as tic * and axes labels vary for the different panels. * @param widthMinimum the minimum width. */ public void setColorBarWidthMinimum(int widthMinimum) { _colorBarWidthMinimum = widthMinimum; if (_colorBar!=null) { _colorBar.setWidthMinimum(widthMinimum); revalidate(); } } /** * Sets the format for major tic annotation of the color bar. * The default format is "%1.4G", which yields a minimum of 1 digit, * with up to 4 digits of precision. Any trailing zeros and decimal * point are removed from tic annotation. * @param format the format. */ public void setColorBarFormat(String format) { _colorBarFormat = format; if (_colorBar!=null) { _colorBar.setFormat(format); revalidate(); } } /** * Removes the color bar. */ public void removeColorBar() { if (_colorBar!=null) { remove(_colorBar); revalidate(); _colorBar = null; } } /** * Adds the plot title. Equivalent to {@link #setTitle(String)}. * The title font is 1.5 times larger than the font of this panel. * @param title the title; null, if none. */ public void addTitle(String title) { setTitle(title); } /** * Sets the plot title. Equivalent to {@link #addTitle(String)}. * @param title the title; null, for no title. */ public void setTitle(String title) { if (title!=null) { if (_title==null) { _title = new Title(title); Font font = getFont(); font.deriveFont(1.5f*font.getSize2D()); _title.setFont(getFont()); _title.setForeground(getForeground()); _title.setBackground(getBackground()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.gridheight = 1; gbc.weightx = 0; gbc.weighty = 0; gbc.fill = GridBagConstraints.NONE; gbc.insets.top = 0; gbc.insets.bottom = 0; gbc.insets.left = 0; gbc.insets.right = 0; //gbc.insets.left = _mosaic.getWidthAxesLeft(); //gbc.insets.right = _mosaic.getWidthAxesRight(); add(_title,gbc); revalidate(); } else { _title.set(title); } } else if (_title!=null) { remove(_title); revalidate(); _title = null; } } /** * Removes the plot title. Equivalent to calling the method * {@link #setTitle(String)} with a null title. */ public void removeTitle() { setTitle(null); } /** * Sets limits for the both horizontal and vertical axes. * By default, limits are computed automatically by tiled graphical views. * This method can be used to override those default limits. * @param hmin the minimum value. * @param vmin the minimum value. * @param hmax the maximum value. * @param vmax the maximum value. */ public void setLimits(double hmin, double vmin, double hmax, double vmax) { setHLimits(hmin,hmax); setVLimits(vmin,vmax); } /** * Sets limits for the horizontal axis. * By default, limits are computed automatically by tiled graphical views. * This method can be used to override those default limits. * @param hmin the minimum value. * @param hmax the maximum value. */ public void setHLimits(double hmin, double hmax) { setHLimits(0,hmin,hmax); } /** * Sets limits for the vertical axis. * By default, limits are computed automatically by tiled graphical views. * This method can be used to override those default limits. * @param vmin the minimum value. * @param vmax the maximum value. */ public void setVLimits(double vmin, double vmax) { setVLimits(0,vmin,vmax); } /** * Sets limits for the horizontal axis in the specified column. * By default, limits are computed automatically by tiled graphical views. * This method can be used to override those default limits. * @param icol the column index. * @param hmin the minimum value. * @param hmax the maximum value. */ public void setHLimits(int icol, double hmin, double hmax) { Check.argument(hmin=0; --ncol) { for (int nrow=0; nrow=0 && cmBest==null; --itv) { TiledView tv = t.getTiledView(itv); if (tv instanceof ColorMapped) { ColorMapped cm = (ColorMapped)tv; if (isMultiColor(cm)) { cmBest = cm; } else if (cmSolid==null) { cmSolid = cm; } } } } } if (cmBest==null) cmBest = cmSolid; return cmBest; } else { return _colorMapped; } } /** * Determines if a specified color map has more than one color. * Note that we ignore any variation in alpha. */ private static boolean isMultiColor(ColorMapped cm) { ColorMap cmap = cm.getColorMap(); IndexColorModel icm = cmap.getColorModel(); int n = icm.getMapSize(); int rgb = icm.getRGB(0)&0x00ffffff; for (int i=1; i





© 2015 - 2024 Weber Informatics LLC | Privacy Policy