List of usage examples for java.awt.geom Rectangle2D getWidth
public abstract double getWidth();
From source file:com.neophob.sematrix.core.generator.Textwriter.java
/** * create image.//from w w w . j a v a 2 s .com * * @param text the text */ public void createTextImage(String text) { //only load if needed if (StringUtils.equals(text, this.text)) { return; } this.text = text; BufferedImage img = new BufferedImage(TEXT_BUFFER_X_SIZE, internalBufferYSize, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = img.createGraphics(); FontRenderContext frc = g2.getFontRenderContext(); TextLayout layout = new TextLayout(text, font, frc); Rectangle2D rect = layout.getBounds(); int h = (int) (0.5f + rect.getHeight()); //head and tailing space maxXPos = (int) (0.5f + rect.getWidth()) + 2 * internalBufferXSize; int ypos = internalBufferYSize - (internalBufferYSize - h) / 2; img = new BufferedImage(maxXPos, internalBufferYSize, BufferedImage.TYPE_BYTE_GRAY); g2 = img.createGraphics(); g2.setColor(new Color(128)); g2.setFont(font); g2.setClip(0, 0, maxXPos, internalBufferYSize); g2.drawString(text, internalBufferXSize, ypos); DataBufferByte dbi = (DataBufferByte) img.getRaster().getDataBuffer(); byte[] textBuffer = dbi.getData(); g2.dispose(); xofs = 0; textAsImage = new int[maxXPos * internalBufferYSize]; for (int i = 0; i < textAsImage.length; i++) { if (textBuffer[i] > 10) { textAsImage[i] = 127; } else { textAsImage[i] = 0; } } //clear internalbuffer Arrays.fill(this.internalBuffer, 0); }
From source file:com.igormaznitsa.jhexed.swing.editor.ui.exporters.PNGImageExporter.java
public BufferedImage generateImage() throws IOException { final int DEFAULT_CELL_WIDTH = 48; final int DEFAULT_CELL_HEIGHT = 48; final int imgWidth = this.docOptions.getImage() == null ? DEFAULT_CELL_WIDTH * this.docOptions.getColumns() : Math.round(this.docOptions.getImage().getSVGWidth()); final int imgHeight = this.docOptions.getImage() == null ? DEFAULT_CELL_HEIGHT * this.docOptions.getRows() : Math.round(this.docOptions.getImage().getSVGHeight()); final BufferedImage result; if (exportData.isBackgroundImageExport() && this.docOptions.getImage() != null) { result = this.docOptions.getImage().rasterize(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB); } else {//from w w w. j av a2s. c o m result = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB); } final Graphics2D gfx = result.createGraphics(); gfx.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gfx.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); final HexEngine<Graphics2D> engine = new HexEngine<Graphics2D>(DEFAULT_CELL_WIDTH, DEFAULT_CELL_HEIGHT, this.docOptions.getHexOrientation()); final List<HexFieldLayer> reversedNormalizedStack = new ArrayList<HexFieldLayer>(); for (int i = this.exportData.getLayers().size() - 1; i >= 0; i--) { final LayerExportRecord rec = this.exportData.getLayers().get(i); if (rec.isAllowed()) { reversedNormalizedStack.add(rec.getLayer()); } } if (Thread.currentThread().isInterrupted()) return null; final HexFieldValue[] stackOfValues = new HexFieldValue[reversedNormalizedStack.size()]; engine.setModel(new HexEngineModel<HexFieldValue[]>() { @Override public int getColumnNumber() { return docOptions.getColumns(); } @Override public int getRowNumber() { return docOptions.getRows(); } @Override public HexFieldValue[] getValueAt(final int col, final int row) { Arrays.fill(stackOfValues, null); for (int index = 0; index < reversedNormalizedStack.size(); index++) { stackOfValues[index] = reversedNormalizedStack.get(index).getHexValueAtPos(col, row); } return stackOfValues; } @Override public HexFieldValue[] getValueAt(final HexPosition pos) { return this.getValueAt(pos.getColumn(), pos.getRow()); } @Override public void setValueAt(int col, int row, HexFieldValue[] value) { } @Override public void setValueAt(HexPosition pos, HexFieldValue[] value) { } @Override public boolean isPositionValid(final int col, final int row) { return col >= 0 && col < docOptions.getColumns() && row >= 0 && row < docOptions.getRows(); } @Override public boolean isPositionValid(final HexPosition pos) { return this.isPositionValid(pos.getColumn(), pos.getRow()); } @Override public void attachedToEngine(final HexEngine<?> engine) { } @Override public void detachedFromEngine(final HexEngine<?> engine) { } }); final HexRect2D visibleSize = engine.getVisibleSize(); final float xcoeff = (float) result.getWidth() / visibleSize.getWidth(); final float ycoeff = (float) result.getHeight() / visibleSize.getHeight(); engine.setScale(xcoeff, ycoeff); final Image[][] cachedIcons = new Image[this.exportData.getLayers().size()][]; engine.setRenderer(new ColorHexRender() { private final Stroke stroke = new BasicStroke(docOptions.getLineWidth()); @Override public Stroke getStroke() { return this.stroke; } @Override public Color getFillColor(HexEngineModel<?> model, int col, int row) { return null; } @Override public Color getBorderColor(HexEngineModel<?> model, int col, int row) { return exportData.isExportHexBorders() ? docOptions.getColor() : null; } @Override public void drawExtra(HexEngine<Graphics2D> engine, Graphics2D g, int col, int row, Color borderColor, Color fillColor) { } @Override public void drawUnderBorder(final HexEngine<Graphics2D> engine, final Graphics2D g, final int col, final int row, final Color borderColor, final Color fillColor) { final HexFieldValue[] stackValues = (HexFieldValue[]) engine.getModel().getValueAt(col, row); for (int i = 0; i < stackValues.length; i++) { final HexFieldValue valueToDraw = stackValues[i]; if (valueToDraw == null) { continue; } g.drawImage(cachedIcons[i][valueToDraw.getIndex()], 0, 0, null); } } }); final Path2D hexShape = ((ColorHexRender) engine.getRenderer()).getHexPath(); final int cellWidth = hexShape.getBounds().width; final int cellHeight = hexShape.getBounds().height; for (int layerIndex = 0; layerIndex < reversedNormalizedStack.size() && !Thread.currentThread().isInterrupted(); layerIndex++) { final HexFieldLayer theLayer = reversedNormalizedStack.get(layerIndex); final Image[] cacheLineForLayer = new Image[theLayer.getHexValuesNumber()]; for (int valueIndex = 1; valueIndex < theLayer.getHexValuesNumber(); valueIndex++) { cacheLineForLayer[valueIndex] = theLayer.getHexValueForIndex(valueIndex).makeIcon(cellWidth, cellHeight, hexShape, true); } cachedIcons[layerIndex] = cacheLineForLayer; } engine.drawWithThreadInterruptionCheck(gfx); if (Thread.currentThread().isInterrupted()) return null; if (this.exportData.isCellCommentariesExport()) { final Iterator<Entry<HexPosition, String>> iterator = this.cellComments.iterator(); gfx.setFont(new Font("Arial", Font.BOLD, 12)); while (iterator.hasNext() && !Thread.currentThread().isInterrupted()) { final Entry<HexPosition, String> item = iterator.next(); final HexPosition pos = item.getKey(); final String text = item.getValue(); final float x = engine.calculateX(pos.getColumn(), pos.getRow()); final float y = engine.calculateY(pos.getColumn(), pos.getRow()); final Rectangle2D textBounds = gfx.getFontMetrics().getStringBounds(text, gfx); final float dx = x - ((float) textBounds.getWidth() - engine.getCellWidth()) / 2; gfx.setColor(Color.BLACK); gfx.drawString(text, dx, y); gfx.setColor(Color.WHITE); gfx.drawString(text, dx - 2, y - 2); } } gfx.dispose(); return result; }
From source file:org.squidy.designer.zoom.impl.PortShape.java
public PortShape() { setBounds(Constants.DEFAULT_PORT_BOUNDS); addInputEventListener(new PBasicInputEventHandler() { /*//from ww w . j a v a2 s . co m * (non-Javadoc) * * @see * edu.umd.cs.piccolo.event.PBasicInputEventHandler#mousePressed * (edu.umd.cs.piccolo .event.PInputEvent) */ @Override public void mousePressed(PInputEvent event) { super.mousePressed(event); if (!event.isHandled()) { isCreatingEdge = true; Rectangle2D bounds = localToGlobal(getBounds()); startX = bounds.getX() + bounds.getWidth() / 2;// .getCenterX(); startY = bounds.getY() + bounds.getWidth() / 2;// .getCenterY(); currentX = startX; currentY = startY; // moveToFront(); event.setHandled(true); } } /* * (non-Javadoc) * * @see * edu.umd.cs.piccolo.event.PBasicInputEventHandler#mouseReleased * (edu.umd.cs.piccolo.event.PInputEvent) */ @Override public void mouseReleased(PInputEvent event) { super.mouseReleased(event); isCreatingEdge = false; ConnectionManager connectionManager = ShapeUtils.getConnectionManager(PortShape.this); Point2D point = event.getPosition(); if (connectionManager.hasConnectionAtDifferentNodeAtPoint(PortShape.this, point)) { if (LOG.isDebugEnabled()) { LOG.debug("Connection port found at " + point); } ConnectorShape<?, ?> source = (ConnectorShape<?, ?>) getParent(); ConnectorShape<?, ?> target = (ConnectorShape<?, ?>) connectionManager .getConnectionAtPoint(point).getParent(); if (!source.getParent().equals(target.getParent())) { // ConnectorShape<?, ?> tmp = source; // source = target; // target = tmp; } PipeShape pipeShape; try { pipeShape = PipeShape.create(source, target); } catch (Exception e) { publishNotification(new TemporaryNotification(e.getMessage())); return; } VisualShape<VisualShape<?>> parentShape; if (!source.getParent().equals(target.getParent())) { if (target.getParent().equals(source)) { parentShape = (VisualShape<VisualShape<?>>) source; } else { parentShape = (VisualShape<VisualShape<?>>) target; } } else { parentShape = (VisualShape<VisualShape<?>>) source.getParent(); } parentShape.addVisualShape(pipeShape); pipeShape.invalidateFullBounds(); } else { // TODO [RR]: Repaint from bounds (do not repaint full node bounds) getParent().getParent().invalidatePaint(); } } /* * (non-Javadoc) * * @see * edu.umd.cs.piccolo.event.PBasicInputEventHandler#mouseDragged * (edu.umd.cs.piccolo.event.PInputEvent) */ @Override public void mouseDragged(PInputEvent event) { super.mouseDragged(event); Point2D point = event.getPosition(); currentX = point.getX(); currentY = point.getY(); event.getCamera().invalidatePaint(); event.setHandled(true); } /* * (non-Javadoc) * * @see * edu.umd.cs.piccolo.event.PBasicInputEventHandler#mouseEntered * (edu.umd.cs.piccolo.event.PInputEvent) */ @Override public void mouseEntered(PInputEvent event) { super.mouseEntered(event); innerColor = Color.GRAY; } /* * (non-Javadoc) * * @see * edu.umd.cs.piccolo.event.PBasicInputEventHandler#mouseExited( * edu.umd.cs.piccolo.event.PInputEvent) */ @Override public void mouseExited(PInputEvent event) { super.mouseExited(event); innerColor = Color.LIGHT_GRAY; } }); }
From source file:org.tsho.dmc2.core.chart.BasinRenderer.java
public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) { basinComponent.setDataobject(null);/*from ww w . java2 s. co m*/ attractorsSamplePoints = new Vector(); state = STATE_RUNNING; gridWidth = (int) dataArea.getWidth(); gridHeight = (int) dataArea.getHeight(); this.imageX = (int) dataArea.getX() + 1; this.imageY = (int) dataArea.getY(); this.image = new BufferedImage(gridWidth, gridHeight, BufferedImage.TYPE_INT_RGB); this.g2 = g2; WritableRaster raster = image.getRaster(); ValueAxis domainAxis = plot.getDomainAxis(); ValueAxis rangeAxis = plot.getRangeAxis(); double maxCoordinate = Math.abs(domainAxis.getUpperBound()); if (Math.abs(domainAxis.getLowerBound()) > maxCoordinate) maxCoordinate = Math.abs(domainAxis.getLowerBound()); if (Math.abs(rangeAxis.getLowerBound()) > maxCoordinate) maxCoordinate = Math.abs(rangeAxis.getLowerBound()); if (Math.abs(rangeAxis.getUpperBound()) > maxCoordinate) maxCoordinate = Math.abs(rangeAxis.getUpperBound()); if (infinity < maxCoordinate) infinity = maxCoordinate + 1; grid = new Grid(new double[] { domainAxis.getLowerBound(), domainAxis.getUpperBound(), rangeAxis.getLowerBound(), rangeAxis.getUpperBound() }, gridHeight, gridWidth); imageData = ((DataBufferInt) raster.getDataBuffer()).getData(); rate = gridHeight * gridWidth / 100; attractorsSamplePoints = new Vector(); BasinsAlgorithm bA = null; if (type == FAST_ALGORITHM) bA = new FastBasinsAlgorithm(this); else if (type == SLOW_ALGORITHM) bA = new SlowBasinsAlgorithm(this); bA.run(); /** Now that grid is computed, pass it to the BasinComponent for possible storing */ int[] tmp = grid.getData(); grid.setData((int[]) tmp.clone());//from now on, grid data is disconnected from image data basinComponent.setDataobject(grid); drawImage(); state = STATE_FINISHED; }
From source file:org.pentaho.reporting.libraries.pixie.wmf.WmfFile.java
public synchronized void draw(final Graphics2D graphics, final Rectangle2D bounds) { // this adjusts imageWidth and imageHeight scaleToFit((float) bounds.getWidth(), (float) bounds.getHeight()); // adjust translation if needed ... graphics.translate(bounds.getX(), bounds.getY()); // adjust to the image origin graphics.translate(-imageX, -imageY); this.graphics = graphics; for (int i = 0; i < records.size(); i++) { try {//from w w w .jav a 2s .co m final MfCmd command = (MfCmd) records.get(i); command.setScale((float) imageWidth / (float) maxWidth, (float) imageHeight / (float) maxHeight); command.replay(this); } catch (Exception e) { logger.warn("Error while processing image record #" + i, e); } } resetStates(); }
From source file:Clip.java
/** * Indicates if this Clip intersects the given rectangle expanded * by the additional margin pace./*from ww w . j a va2 s .c o m*/ * @param r the rectangle to test for intersect * @param margin additional margin "bleed" to include in the intersection * @return true if the clip intersects the expanded region, false otherwise */ public boolean intersects(Rectangle2D r, double margin) { double tw = clip[6] - clip[0]; double th = clip[7] - clip[1]; double rw = r.getWidth(); double rh = r.getHeight(); if (rw < 0 || rh < 0 || tw < 0 || th < 0) { return false; } double tx = clip[0]; double ty = clip[1]; double rx = r.getX() - margin; double ry = r.getY() - margin; rw += rx + 2 * margin; rh += ry + 2 * margin; tw += tx; th += ty; // overflow || intersect return ((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry)); }
From source file:com.rapidminer.gui.plotter.charts.ChartPanelShiftController.java
/** * Pan / Shifts a plot if the arrow keys are pressed. *///from w ww. j ava 2s . c o m public void keyPressed(KeyEvent e) { if (!plotSupported) { return; } int keyCode = e.getKeyCode(); // we're only interested in arrows (code 37,38,39,40) if ((keyCode < 37) || (keyCode > 40)) { return; } // The axes we're gonna shift ValueAxis[] axes = null; boolean domainShift = false; // used for PAN_FIXED // Calculations for the domain axis if ((keyCode == KeyEvent.VK_LEFT) || (keyCode == KeyEvent.VK_RIGHT)) { axes = getPlotAxis(chartPanel.getChart(), !axesSwaped); domainShift = true; } // Calculations for the range axis else { axes = getPlotAxis(chartPanel.getChart(), axesSwaped); } // Delta is the amount we'll shift in axes units. double[] delta = new double[axes.length]; // Let's calculate 'delta', the amount by which we'll shift the plot for (int i = 0; i < axes.length; i++) { switch (shiftType) { case SHIFT_PERCENTUAL: delta[i] = (axes[i].getUpperBound() - axes[i].getLowerBound()) / 100.0; break; case SHIFT_FIXED: delta[i] = (domainShift ? fixedDomainShiftUnits : fixedRangeShiftUnits); break; case SHIFT_PIXEL: // also the default default: // Let's find out what's the range for 1 pixel. final Rectangle2D scaledDataArea = chartPanel.getScreenDataArea(); delta[i] = axes[i].getRange().getLength() / (scaledDataArea.getWidth()); break; } } // Shift modifier multiplies delta by 10 if (e.isShiftDown()) { for (int i = 0; i < delta.length; i++) { delta[i] *= 10; } } for (int i = 0; i < axes.length; i++) { switch (keyCode) { case KeyEvent.VK_LEFT: case KeyEvent.VK_DOWN: axes[i].setRange(axes[i].getLowerBound() - delta[i], axes[i].getUpperBound() - delta[i]); break; case KeyEvent.VK_UP: case KeyEvent.VK_RIGHT: axes[i].setRange(axes[i].getLowerBound() + delta[i], axes[i].getUpperBound() + delta[i]); break; } } }
From source file:de.tor.tribes.ui.algo.TimeFrameVisualizer.java
private void renderPopup(HashMap<String, Object> pPopupInfo, Graphics2D pG2D) { Point location = (Point) pPopupInfo.get("popup.location"); String label = (String) pPopupInfo.get("popup.label"); if (location == null || label == null) { return;//from w w w . j a v a 2 s . c o m } pG2D.setColor(new Color(255, 255, 204)); Rectangle2D labelBounds = pG2D.getFontMetrics().getStringBounds(label, pG2D); pG2D.fillRect(location.x, location.y - (int) labelBounds.getHeight(), (int) labelBounds.getWidth() + 5, (int) labelBounds.getHeight() + 5); pG2D.setColor(Color.BLACK); pG2D.drawRect(location.x, location.y - (int) labelBounds.getHeight(), (int) labelBounds.getWidth() + 5, (int) labelBounds.getHeight() + 5); pG2D.drawString(label, location.x + 2, location.y); }
From source file:net.sf.jasperreports.customizers.shape.AbstractShapeCustomizer.java
/** * Uses the points to build a polyline/*w w w . j a v a2 s. c o m*/ * * @param baseShape the points of the polyline * @return a polyline shape or null if it can't be build from the current configuration */ protected Shape buildPolyline(ShapePoints baseShape) { Path2D path = null; List<Point> points = baseShape.getPoints(); if (points != null && !points.isEmpty()) { float scaleFactorX = 1.0f; float scaleFactorY = 1.0f; Rectangle2D bounds = getBounds(baseShape); Integer width = getWidth(); Integer height = getHeight(); if (width != null) { scaleFactorX = (float) (width / bounds.getWidth()); } if (height != null) { scaleFactorY = (float) (height / bounds.getHeight()); } path = new Path2D.Double(); if (points.size() > 1) { Point offset = getOffset(bounds); Point point = points.get(0); path.moveTo((point.getX() - offset.getX()) * scaleFactorX, (point.getY() - offset.getY()) * scaleFactorY); for (int i = 1; i < points.size(); i++) { point = points.get(i); path.lineTo((point.getX() - offset.getX()) * scaleFactorX, (point.getY() - offset.getY()) * scaleFactorY); } } } return path; }
From source file:net.sf.jasperreports.customizers.shape.AbstractShapeCustomizer.java
/** * Builds a polygon shape./*from ww w. j a va 2 s. co m*/ * * @param shapePoints the points of the polygon * @return the polygon or null if it can't be build from the current configuration */ protected Shape buildPolygon(ShapePoints shapePoints) { Polygon polygon = null; List<Point> points = shapePoints.getPoints(); if (points != null && !points.isEmpty()) { float scaleFactorX = 1.0f; float scaleFactorY = 1.0f; Rectangle2D bounds = getBounds(shapePoints); Integer width = getWidth(); Integer height = getHeight(); if (width != null) { scaleFactorX = (float) width / (float) bounds.getWidth(); } if (height != null) { scaleFactorY = (float) height / (float) bounds.getHeight(); } Point offset = getOffset(bounds); int[] pointsX = new int[points.size()]; int[] pointsY = new int[points.size()]; for (int i = 0; i < points.size(); i++) { Point point = points.get(i); pointsX[i] = Math.round((point.getX() - offset.getX()) * scaleFactorX); pointsY[i] = Math.round((point.getY() - offset.getY()) * scaleFactorY); } polygon = new Polygon(pointsX, pointsY, points.size()); } return polygon; }