Example usage for java.awt Graphics2D draw

List of usage examples for java.awt Graphics2D draw

Introduction

In this page you can find the example usage for java.awt Graphics2D draw.

Prototype

public abstract void draw(Shape s);

Source Link

Document

Strokes the outline of a Shape using the settings of the current Graphics2D context.

Usage

From source file:org.openmeetings.app.data.record.BatikMethods.java

public void drawThickLine2D(Graphics2D g2d, double x1, double y1, double x2, double y2, int width, Color c,
        double xObj, double yObj, float alpha) throws Exception {
    g2d.setPaint(c);//from   www .j  a  va  2 s .co  m

    int[] rules = new int[8];

    //all possible Compositing Rules:
    rules[0] = AlphaComposite.SRC_OVER;
    rules[1] = AlphaComposite.DST_OVER;
    rules[2] = AlphaComposite.CLEAR;
    rules[3] = AlphaComposite.SRC;
    rules[4] = AlphaComposite.SRC_IN;
    rules[5] = AlphaComposite.DST_IN;
    rules[6] = AlphaComposite.SRC_OUT;
    rules[7] = AlphaComposite.DST_OUT;

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, alpha));
    g2d.setStroke(new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
    Line2D line = new Line2D.Double(x1 + xObj, y1 + yObj, x2 + xObj, y2 + yObj);
    g2d.draw(line);
}

From source file:TransferableScribblePane.java

/** We override this method to draw ourselves. */
public void paintComponent(Graphics g) {
    // Let the superclass do its painting first
    super.paintComponent(g);

    // Make a copy of the Graphics context so we can modify it
    Graphics2D g2 = (Graphics2D) (g.create());

    // Our superclass doesn't paint the background, so do this ourselves.
    g2.setColor(getBackground());// w  w w .j  a  v  a  2  s . c o m
    g2.fillRect(0, 0, getWidth(), getHeight());

    // Set the line width and color to use for the foreground
    g2.setStroke(stroke);
    g2.setColor(this.getForeground());

    // Now loop through the PolyLine shapes and draw them all
    int numlines = lines.size();
    for (int i = 0; i < numlines; i++) {
        PolyLine line = (PolyLine) lines.get(i);
        if (line == selectedLine) { // If it is the selected line
            g2.setStroke(selectedStroke); // Set dash pattern
            g2.draw(line); // Draw the line
            g2.setStroke(stroke); // Revert to solid lines
        } else
            g2.draw(line); // Otherwise just draw the line
    }
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param paintContext/*from   w  ww. j a v  a  2s  . c om*/
 */
protected void paintShapeZoomedIn(PPaintContext paintContext) {

    Shape shapeZoomedIn = getZoomedInShape();
    if (shapeZoomedIn != null) {
        Graphics2D g = paintContext.getGraphics();
        Rectangle bounds = shapeZoomedIn.getBounds();

        g.setPaint(getZoomedInFillPaint());
        if (isRenderPrimitiveRect())
            g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.fill(shapeZoomedIn);

        g.setStroke(STROKE_ZOOMED_IN);
        g.setPaint(getZoomedInDrawPaint());
        if (isRenderPrimitiveRect())
            g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.draw(shapeZoomedIn);
    }
}

From source file:org.squidy.designer.shape.ZoomShape.java

/**
 * @param paintContext//from  www  . ja v  a 2s . com
 */
protected void paintShapeZoomedOut(PPaintContext paintContext) {

    Shape shapeZoomedOut = getZoomedOutShape();
    if (shapeZoomedOut != null) {
        Graphics2D g = paintContext.getGraphics();
        Rectangle bounds = shapeZoomedOut.getBounds();

        g.setPaint(getZoomedOutFillPaint());
        if (isRenderPrimitiveRect())
            g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.fill(shapeZoomedOut);

        g.setStroke(STROKE_ZOOMED_OUT);
        g.setPaint(getZoomedOutDrawPaint());
        if (isRenderPrimitiveRect())
            g.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
        else
            g.draw(shapeZoomedOut);
    }
}

From source file:org.openmeetings.app.data.record.BatikMethods.java

public void paintEllipse2D(Graphics2D g2d, double x, double y, double width, double height, Color linecoler,
        int thickness, Color fillColor, float alpha) throws Exception {

    g2d.setStroke(new BasicStroke(thickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    int[] rules = new int[8];

    //all possible Compositing Rules:
    rules[0] = AlphaComposite.SRC_OVER;
    rules[1] = AlphaComposite.DST_OVER;
    rules[2] = AlphaComposite.CLEAR;
    rules[3] = AlphaComposite.SRC;
    rules[4] = AlphaComposite.SRC_IN;
    rules[5] = AlphaComposite.DST_IN;
    rules[6] = AlphaComposite.SRC_OUT;
    rules[7] = AlphaComposite.DST_OUT;

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, alpha));

    //int x, int y, int width, int height

    if (linecoler != null) {
        g2d.setPaint(linecoler);/* ww  w .  j a v  a2s.co  m*/
        g2d.draw(new Ellipse2D.Double(x, y, width, height));
    }

    if (fillColor != null) {
        g2d.setPaint(fillColor);
        g2d.fill(new Ellipse2D.Double(x, y, width, height));
    }

}

From source file:org.openmeetings.app.data.record.BatikMethods.java

public void paintRect2D(Graphics2D g2d, double x, double y, double width, double height, Color linecoler,
        int thickness, Color fillColor, float alpha) throws Exception {

    g2d.setStroke(new BasicStroke(thickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    int[] rules = new int[8];

    //all possible Compositing Rules:
    rules[0] = AlphaComposite.SRC_OVER;
    rules[1] = AlphaComposite.DST_OVER;
    rules[2] = AlphaComposite.CLEAR;
    rules[3] = AlphaComposite.SRC;
    rules[4] = AlphaComposite.SRC_IN;
    rules[5] = AlphaComposite.DST_IN;
    rules[6] = AlphaComposite.SRC_OUT;
    rules[7] = AlphaComposite.DST_OUT;

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, alpha));

    //int x, int y, int width, int height

    if (linecoler != null) {
        g2d.setPaint(linecoler);//  ww w .j a  v  a 2s  .  c  om
        g2d.draw(new Rectangle2D.Double(x, y, width, height));
    }

    if (fillColor != null) {
        g2d.setPaint(fillColor);
        g2d.fill(new Rectangle2D.Double(x, y, width, height));
    }

}

From source file:BookTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    PageFormat pageFormat = book.getPageFormat(currentPage);

    double xoff; // x offset of page start in window
    double yoff; // y offset of page start in window
    double scale; // scale factor to fit page in window
    double px = pageFormat.getWidth();
    double py = pageFormat.getHeight();
    double sx = getWidth() - 1;
    double sy = getHeight() - 1;
    if (px / py < sx / sy) // center horizontally
    {/*from w  ww.  j a va 2 s.  c o m*/
        scale = sy / py;
        xoff = 0.5 * (sx - scale * px);
        yoff = 0;
    } else
    // center vertically
    {
        scale = sx / px;
        xoff = 0;
        yoff = 0.5 * (sy - scale * py);
    }
    g2.translate((float) xoff, (float) yoff);
    g2.scale((float) scale, (float) scale);

    // draw page outline (ignoring margins)
    Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
    g2.setPaint(Color.white);
    g2.fill(page);
    g2.setPaint(Color.black);
    g2.draw(page);

    Printable printable = book.getPrintable(currentPage);
    try {
        printable.print(g2, pageFormat, currentPage);
    } catch (PrinterException e) {
        g2.draw(new Line2D.Double(0, 0, px, py));
        g2.draw(new Line2D.Double(px, 0, 0, py));
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.SpectraPanel.java

public void mouseDragged(MouseEvent e) {
    if (mouse_start_point != null && theDocument.getNoScans() > 0) {
        if (is_moving) {
            // moving
            double mz_delta = screenToDataX(mouse_start_point.getX() - e.getPoint().getX());
            if (mz_delta > 0.) {
                double old_upper_bound = thePlot.getDomainAxis().getUpperBound();
                double old_lower_bound = thePlot.getDomainAxis().getLowerBound();
                double new_upper_bound = Math.min(old_upper_bound + mz_delta,
                        theDocument.getPeakDataAt(current_ind).getMaxMZ());
                double new_lower_bound = old_lower_bound + new_upper_bound - old_upper_bound;

                thePlot.getDomainAxis().setRange(new Range(new_lower_bound, new_upper_bound));
            } else {
                double old_upper_bound = thePlot.getDomainAxis().getUpperBound();
                double old_lower_bound = thePlot.getDomainAxis().getLowerBound();
                double new_lower_bound = Math.max(old_lower_bound + mz_delta,
                        theDocument.getPeakDataAt(current_ind).getMinMZ());
                double new_upper_bound = old_upper_bound + new_lower_bound - old_lower_bound;

                thePlot.getDomainAxis().setRange(new Range(new_lower_bound, new_upper_bound));
            }// w  w  w  .  j ava  2 s . c  o  m

            mouse_start_point = e.getPoint();
        } else {
            // zooming                
            Graphics2D g2 = (Graphics2D) theChartPanel.getGraphics();
            g2.setXORMode(java.awt.Color.gray);

            // delete old rectangle
            if (zoom_rectangle != null)
                g2.draw(zoom_rectangle);

            // create new rectangle
            double start_x = Math.min(e.getX(), mouse_start_point.getX());
            double end_x = Math.max(e.getX(), mouse_start_point.getX());

            Rectangle2D data_area = theChartPanel.getScreenDataArea((int) start_x,
                    (int) mouse_start_point.getY());
            double xmax = Math.min(end_x, data_area.getMaxX());
            zoom_rectangle = new Rectangle2D.Double(start_x, data_area.getMinY(), xmax - start_x,
                    data_area.getHeight());

            // draw new rectangle
            g2.draw(zoom_rectangle);
            g2.dispose();
        }
    }
}

From source file:edu.dlnu.liuwenpeng.render.XYLineAndShapeRenderer.java

/**    
 * Draws the first pass shape.    //from   w w w .  j a v  a 2  s  .  co m
 *    
 * @param g2  the graphics device.    
 * @param pass  the pass.    
 * @param series  the series index.    
 * @param item  the item index.    
 * @param shape  the shape.    
 */
protected void drawFirstPassShape(Graphics2D g2, int pass, int series, int item, Shape shape) {
    g2.setStroke(getItemStroke(series, item));
    g2.setPaint(Color.red);
    g2.draw(shape);
}

From source file:edu.jhuapl.graphs.jfreechart.utils.SparselyLabeledCategoryAxis.java

@SuppressWarnings("unchecked")
@Override/*from   w  w w  .j  av  a  2  s  .  c  o  m*/
public void drawTickMarks(Graphics2D g2, double cursor, Rectangle2D dataArea, RectangleEdge edge,
        AxisState state) {
    Plot p = getPlot();

    if (p == null) {
        return;
    }

    CategoryPlot plot = (CategoryPlot) p;
    double il = getTickMarkInsideLength();
    double ol = getTickMarkOutsideLength();
    Line2D line = new Line2D.Double();
    List categories = plot.getCategoriesForAxis(this);
    int tickEvery = categories.size() / (maxLabeledTicks == 0 ? 1 : maxLabeledTicks);

    if (tickEvery < 1) {
        tickEvery = 1;
    }

    if (edge.equals(RectangleEdge.TOP)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double x = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(x, cursor, x, cursor + il);
                g2.draw(line);
                line.setLine(x, cursor, x, cursor - ol);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, x);
                }
            }

            i++;
        }

        state.cursorUp(ol);
    } else if (edge.equals(RectangleEdge.BOTTOM)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double x = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(x, cursor, x, cursor - il);
                g2.draw(line);
                line.setLine(x, cursor, x, cursor + ol);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, x);
                }
            }

            i++;
        }

        state.cursorDown(ol);
    } else if (edge.equals(RectangleEdge.LEFT)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double y = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(cursor, y, cursor + il, y);
                g2.draw(line);
                line.setLine(cursor, y, cursor - ol, y);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, y);
                }
            }

            i++;
        }

        state.cursorLeft(ol);
    } else if (edge.equals(RectangleEdge.RIGHT)) {
        Iterator iterator = categories.iterator();
        int i = 0;

        while (iterator.hasNext()) {
            Comparable key = (Comparable) iterator.next();

            if (i % tickEvery == 0) {
                double y = getCategoryMiddle(key, categories, dataArea, edge);
                g2.setPaint(getTickMarkPaint());
                g2.setStroke(getTickMarkStroke());
                line.setLine(cursor, y, cursor - il, y);
                g2.draw(line);
                line.setLine(cursor, y, cursor + ol, y);
                g2.draw(line);

                if (domainGridlinePaint != null) {
                    drawDomainGridline(g2, plot, dataArea, y);
                }
            }

            i++;
        }

        state.cursorRight(ol);
    }
}