use of java.awt.geom.Rectangle2D in project scriptographer by scriptographer.
the class Raster method getAverageColor.
/**
* @jshide
*/
public Color getAverageColor(Shape shape) {
// Rectangle2D rect = shape.getBounds2D();
GeneralPath path;
int width = getWidth();
int height = getHeight();
int startX = 0;
int startY = 0;
if (shape != null) {
Matrix inverse = getInverseMatrix();
if (inverse == null)
return null;
// Create a transformed path. This is faster than
// path.clone() / path.transform(at);
PathIterator pi = shape.getPathIterator(inverse.toAffineTransform());
path = new GeneralPath();
path.setWindingRule(pi.getWindingRule());
path.append(pi, false);
Rectangle2D bounds = path.getBounds2D();
// Fetch the sub image to iterate over and calculate average colors
// from
// Crop to the maximum size.
Rectangle2D.intersect(bounds, new Rectangle2D.Double(startX, startY, width, height), bounds);
width = (int) Math.ceil(bounds.getWidth());
height = (int) Math.ceil(bounds.getHeight());
// Are we completely outside the raster? If so, return null
if (width <= 0 || height <= 0)
return null;
startX = (int) Math.floor(bounds.getX());
startY = (int) Math.floor(bounds.getY());
} else {
path = null;
}
BufferedImage img = getSubImage(startX, startY, width, height);
// Raster check = new Raster(img);
// check.setPosition(rect.getCenterX(), rect.getCenterY());
WritableRaster raster = img.getRaster();
byte[] data = (byte[]) raster.getDataElements(0, 0, null);
float[] components = new float[data.length];
for (int i = 0; i < data.length; i++) components[i] = data[i] & 0xff;
long total = 1;
for (int y = 0; y < height; y++) {
for (int x = (y == 0) ? 1 : 0; x < width; x++) {
if (path == null || path.contains(x + startX, y + startY)) {
data = (byte[]) raster.getDataElements(x, height - 1 - y, data);
for (int i = 0; i < data.length; i++) components[i] += data[i] & 0xff;
total++;
}
}
}
total *= 255;
for (int i = 0; i < components.length; i++) components[i] = components[i] / total;
// Return colors
if (components.length == 4)
return new CMYKColor(components);
else if (components.length == 3)
return new RGBColor(components);
else
return new GrayColor(components);
}
use of java.awt.geom.Rectangle2D in project binnavi by google.
the class ZyGroupNodeRealizer method regenerate.
@Override
public void regenerate() {
m_updater.generateContent(this, m_content);
final Rectangle2D bounds = m_content.getBounds();
// For some forsaken reason setSize changes
// the position of the node. So we have to
// store and restore the position between setting
// the size.
final double x = getX();
final double y = getY();
setSize(bounds.getWidth(), bounds.getHeight());
setX(x);
setY(y);
for (final IZyNodeRealizerListener<?> listener : m_listeners) {
try {
listener.regenerated(this);
} catch (final Exception exception) {
CUtilityFunctions.logException(exception);
}
}
repaint();
}
use of java.awt.geom.Rectangle2D in project binnavi by google.
the class ZyNodeRealizer method positionToRow.
/**
* Converts a y coordinate into a line number of the node content.
*
* @param y The y coordinate.
* @return The number of the line shown at the y address or -1 if there is no line at the given
* coordinates.
*/
@Override
public int positionToRow(final double y) {
// TODO: This does not really work because line heights are not constant.
final ZyLabelContent content = getNodeContent();
final Rectangle2D contentBounds = getNodeContent().getBounds();
final double yratio = getHeight() / contentBounds.getHeight();
final int row = (int) ((y / yratio - content.getPaddingTop()) / content.getLineHeight());
return row >= content.getLineCount() ? -1 : row;
}
use of java.awt.geom.Rectangle2D in project binnavi by google.
the class ZyNormalNodeRealizer method paintNode.
@Override
public void paintNode(final Graphics2D gfx) {
super.paintNode(gfx);
final Rectangle2D contentBounds = getNodeContent().getBounds();
final double xratio = getWidth() / contentBounds.getWidth();
final double yratio = getHeight() / contentBounds.getHeight();
gfx.scale(xratio, yratio);
getNodeContent().draw(gfx, (getX() * 1) / xratio, (getY() * 1) / yratio);
gfx.scale(1 / xratio, 1 / yratio);
}
use of java.awt.geom.Rectangle2D in project binnavi by google.
the class MoveFunctions method centerNodes.
/**
* Centers the screen on a list of nodes.
*
* @param nodes The list of nodes in question.
*/
public static <NodeType extends ZyGraphNode<?> & ISelectableNode & IViewableNode & IYNode & IRawNodeAccessible, EdgeType extends ZyGraphEdge<?, ?, ?>> void centerNodes(final AbstractZyGraph<NodeType, ?> graph, final Set<NodeType> nodes) {
Preconditions.checkNotNull(nodes, "Error: Nodes argument is null");
Preconditions.checkArgument(!nodes.isEmpty(), "Error: Nodes argument is empty");
// To center all nodes, we calculate the bounding box that includes all nodes.
final Rectangle2D box = GraphHelpers.calculateBoundingBox(nodes);
// Center the graph to make sure all nodes are visible.
graph.getView().setCenter(box.getX() + (box.getWidth() / 2.), box.getY() + (box.getHeight() / 2.));
graph.updateViews();
}
Aggregations