Example usage for java.awt.geom Line2D.Double setLine

List of usage examples for java.awt.geom Line2D.Double setLine

Introduction

In this page you can find the example usage for java.awt.geom Line2D.Double setLine.

Prototype

public abstract void setLine(double x1, double y1, double x2, double y2);

Source Link

Document

Sets the location of the end points of this Line2D to the specified double coordinates.

Usage

From source file:JavaWorldPrintExample1.java

/**
 * Method: print//from  w  w w .ja  v  a2 s .c o  m
 * <p>
 * 
 * This class is responsible for rendering a page using the provided
 * parameters. The result will be a grid where each cell will be half an
 * inch by half an inch.
 * 
 * @param g
 *            a value of type Graphics
 * @param pageFormat
 *            a value of type PageFormat
 * @param page
 *            a value of type int
 * @return a value of type int
 */
public int print(Graphics g, PageFormat pageFormat, int page) {

    int i;
    Graphics2D g2d;
    Line2D.Double line = new Line2D.Double();

    //--- Validate the page number, we only print the first page
    if (page == 0) {

        //--- Create a graphic2D object a set the default parameters
        g2d = (Graphics2D) g;
        g2d.setColor(Color.black);

        //--- Translate the origin to be (0,0)
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

        //--- Print the vertical lines
        for (i = 0; i < pageFormat.getWidth(); i += INCH / 2) {
            line.setLine(i, 0, i, pageFormat.getHeight());
            g2d.draw(line);
        }

        //--- Print the horizontal lines
        for (i = 0; i < pageFormat.getHeight(); i += INCH / 2) {
            line.setLine(0, i, pageFormat.getWidth(), i);
            g2d.draw(line);
        }

        return (PAGE_EXISTS);
    } else
        return (NO_SUCH_PAGE);
}

From source file:LineGraphDrawable.java

/**
 * Draws the bar-graph into the given Graphics2D context in the given area.
 * This method will not draw a graph if the data given is null or empty.
 * //from  w ww  . ja  v  a 2  s . c  om
 * @param graphics
 *          the graphics context on which the bargraph should be rendered.
 * @param drawArea
 *          the area on which the bargraph should be drawn.
 */
public void draw(final Graphics2D graphics, final Rectangle2D drawArea) {
    if (graphics == null) {
        throw new NullPointerException();
    }
    if (drawArea == null) {
        throw new NullPointerException();
    }

    final int height = (int) drawArea.getHeight();
    if (height <= 0) {
        return;
    }

    final Graphics2D g2 = (Graphics2D) graphics.create();
    if (background != null) {
        g2.setPaint(background);
        g2.draw(drawArea);
    }

    if (data == null || data.length == 0) {
        g2.dispose();
        return;
    }

    g2.translate(drawArea.getX(), drawArea.getY());

    float d = getDivisor(data, height);
    final int spacing = getSpacing();
    final int w = (((int) drawArea.getWidth()) - (spacing * (data.length - 1))) / (data.length - 1);

    float min = Float.MAX_VALUE;
    for (int index = 0; index < data.length; index++) {
        Number i = data[index];
        if (i == null) {
            continue;
        }
        final float value = i.floatValue();
        if (value < min) {
            min = value;
        }
    }

    int x = 0;
    int y = -1;

    if (d == 0.0) {
        // special case -- a horizontal straight line
        d = 1.0f;
        y = -height / 2;
    }

    final Line2D.Double line = new Line2D.Double();
    for (int i = 0; i < data.length - 1; i++) {
        final int px1 = x;
        x += (w + spacing);
        final int px2 = x;

        g2.setPaint(color);

        final Number number = data[i];
        final Number nextNumber = data[i + 1];
        if (number == null && nextNumber == null) {
            final float delta = height - ((0 - min) / d);
            line.setLine(px1, y + delta, px2, y + delta);
        } else if (number == null) {
            line.setLine(px1, y + (height - ((0 - min) / d)), px2,
                    y + (height - ((nextNumber.floatValue() - min) / d)));
        } else if (nextNumber == null) {
            line.setLine(px1, y + (height - ((number.floatValue() - min) / d)), px2,
                    y + (height - ((0 - min) / d)));
        } else {
            line.setLine(px1, y + (height - ((number.floatValue() - min) / d)), px2,
                    y + (height - ((nextNumber.floatValue() - min) / d)));
        }
        g2.draw(line);

    }

    g2.dispose();

}

From source file:org.pentaho.reporting.designer.core.editor.report.AbstractRenderComponent.java

protected void paintGrid(final Graphics2D g2d) {
    if (WorkspaceSettings.getInstance().isShowGrid()) {
        final float scaleFactor = getRenderContext().getZoomModel().getZoomAsPercentage();
        final double gridSize = getGridSize() * scaleFactor;
        if (gridSize < 1) {
            return;
        }//from w ww .ja  va2  s.  c o m

        final int gridDivisions = Math.max(1, getGridDivisions());

        final Color primaryColor = WorkspaceSettings.getInstance().getGridColor();
        final Color secondaryColor = ColorUtility.convertToBrighter(primaryColor);
        // draw vertical lines
        g2d.setStroke(new BasicStroke(.1f));
        int horizontalLineCount = 0;
        final Line2D.Double line = new Line2D.Double();
        final double gridHeight = getHeight();
        final double gridWidth = getWidth();
        for (double w = gridSize; w < gridWidth; w += gridSize) {
            if (horizontalLineCount % gridDivisions == gridDivisions - 1) {
                g2d.setColor(primaryColor);
            } else {
                g2d.setColor(secondaryColor);
            }
            horizontalLineCount++;
            line.setLine(w, 0, w, gridHeight);
            g2d.draw(line);
        }

        // draw horizontal lines
        int verticalLineCount = 0;
        for (double h = gridSize; h < gridHeight; h += gridSize) {
            if (verticalLineCount % gridDivisions == gridDivisions - 1) {
                g2d.setColor(primaryColor);
            } else {
                g2d.setColor(secondaryColor);
            }
            verticalLineCount++;
            line.setLine(0, h, gridWidth, h);
            g2d.draw(line);
        }
    }
}

From source file:org.pentaho.reporting.designer.core.editor.report.AbstractRenderComponent.java

protected void paintElementAlignment(final Graphics2D g2d) {
    if (WorkspaceSettings.getInstance().isShowElementAlignmentHints()) {
        final float scaleFactor = getRenderContext().getZoomModel().getZoomAsPercentage();
        g2d.setColor(WorkspaceSettings.getInstance().getAlignmentHintColor());
        g2d.setStroke(new BasicStroke(.2f));

        final double gridHeight = getHeight();
        final double gridWidth = getWidth();
        final long[] hPositions;
        if (getHorizontalPositionsModel() == null) {
            final BreakPositionsList horizontalPositions = getHorizontalEdgePositions();
            hPositions = horizontalPositions.getKeys();
        } else {//from   w  w w.  j av a2s  . c om
            hPositions = getHorizontalPositionsModel().getBreaks();
        }
        final Line2D.Double line = new Line2D.Double();
        for (int i = 0; i < hPositions.length; i++) {
            final double position = StrictGeomUtility.toExternalValue(hPositions[i]);
            final double x = position * scaleFactor;
            line.setLine(x, 0, x, gridHeight);
            g2d.draw(line);
        }

        final Point2D offset = getOffset();
        final BreakPositionsList verticalPositions = getVerticalEdgePositions();
        final long[] vPositions = verticalPositions.getKeys();
        for (int i = 0; i < vPositions.length; i++) {
            final double position = StrictGeomUtility.toExternalValue(vPositions[i]) - offset.getY();
            final double y2 = position * scaleFactor;
            line.setLine(0, y2, gridWidth, y2);
            g2d.draw(line);
        }
    }

}