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:gov.nih.nci.caintegrator.application.graphing.BoxAndWhiskerDotsRenderer.java

/**
 * Draws a triangle to indicate the presence of far-out values.
 * /*from   ww w.  j ava2 s  .c o  m*/
 * @param aRadius  the radius.
 * @param g2  the graphics device.
 * @param xx  the x coordinate.
 * @param m  the y coordinate.
 */
private void drawLowFarOut(double aRadius, Graphics2D g2, double xx, double m) {
    double side = aRadius * 2;
    g2.draw(new Line2D.Double(xx - side, m - side, xx + side, m - side));
    g2.draw(new Line2D.Double(xx - side, m - side, xx, m));
    g2.draw(new Line2D.Double(xx + side, m - side, xx, m));
}

From source file:org.jfree.experimental.chart.plot.dial.SimpleDialFrame.java

/**
 * Draws the frame.  This method is called by the {@link DialPlot} class,
 * you shouldn't need to call it directly.
 *
 * @param g2  the graphics target (<code>null</code> not permitted).
 * @param plot  the plot (<code>null</code> not permitted).
 * @param frame  the frame (<code>null</code> not permitted).
 * @param view  the view (<code>null</code> not permitted).
 *//*w  w w.  ja va  2  s  .c  o  m*/
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    Shape window = getWindow(frame);

    Rectangle2D f = DialPlot.rectangleByRadius(frame, this.radius + 0.02, this.radius + 0.02);
    Ellipse2D e = new Ellipse2D.Double(f.getX(), f.getY(), f.getWidth(), f.getHeight());

    Area area = new Area(e);
    Area area2 = new Area(window);
    area.subtract(area2);
    g2.setPaint(this.backgroundPaint);
    g2.fill(area);

    g2.setStroke(this.stroke);
    g2.setPaint(this.foregroundPaint);
    g2.draw(window);
    g2.draw(e);
}

From source file:ScribbleDragAndDrop.java

/**
 * The component draws itself by drawing each of the Scribble objects.
 *//*from w  ww .ja v a  2 s . c  om*/
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setStroke(linestyle); // Specify wide lines

    int numScribbles = scribbles.size();
    for (int i = 0; i < numScribbles; i++) {
        Scribble s = (Scribble) scribbles.get(i);
        g2.draw(s); // Draw the scribble
    }
}

From source file:org.deeplearning4j.examples.multigpu.video.VideoGenerator.java

private static int[] generateVideo(String path, int nFrames, int width, int height, int numShapes, Random r,
        boolean backgroundNoise, int numDistractorsPerFrame) throws Exception {

    //First: decide where transitions between one shape and another are
    double[] rns = new double[numShapes];
    double sum = 0;
    for (int i = 0; i < numShapes; i++) {
        rns[i] = r.nextDouble();//from   w w w  .j a v a  2  s .co m
        sum += rns[i];
    }
    for (int i = 0; i < numShapes; i++)
        rns[i] /= sum;

    int[] startFrames = new int[numShapes];
    startFrames[0] = 0;
    for (int i = 1; i < numShapes; i++) {
        startFrames[i] = (int) (startFrames[i - 1] + MIN_FRAMES + rns[i] * (nFrames - numShapes * MIN_FRAMES));
    }

    //Randomly generate shape positions, velocities, colors, and type
    int[] shapeTypes = new int[numShapes];
    int[] initialX = new int[numShapes];
    int[] initialY = new int[numShapes];
    double[] velocityX = new double[numShapes];
    double[] velocityY = new double[numShapes];
    Color[] color = new Color[numShapes];
    for (int i = 0; i < numShapes; i++) {
        shapeTypes[i] = r.nextInt(NUM_SHAPES);
        initialX[i] = SHAPE_MIN_DIST_FROM_EDGE + r.nextInt(width - SHAPE_SIZE - 2 * SHAPE_MIN_DIST_FROM_EDGE);
        initialY[i] = SHAPE_MIN_DIST_FROM_EDGE + r.nextInt(height - SHAPE_SIZE - 2 * SHAPE_MIN_DIST_FROM_EDGE);
        velocityX[i] = -1 + 2 * r.nextDouble();
        velocityY[i] = -1 + 2 * r.nextDouble();
        color[i] = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
    }

    //Generate a sequence of BufferedImages with the given shapes, and write them to the video
    SequenceEncoder enc = new SequenceEncoder(new File(path));
    int currShape = 0;
    int[] labels = new int[nFrames];
    for (int i = 0; i < nFrames; i++) {
        if (currShape < numShapes - 1 && i >= startFrames[currShape + 1])
            currShape++;

        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setBackground(Color.BLACK);

        if (backgroundNoise) {
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    bi.setRGB(x, y, new Color(r.nextFloat() * MAX_NOISE_VALUE, r.nextFloat() * MAX_NOISE_VALUE,
                            r.nextFloat() * MAX_NOISE_VALUE).getRGB());
                }
            }
        }

        g2d.setColor(color[currShape]);

        //Position of shape this frame
        int currX = (int) (initialX[currShape]
                + (i - startFrames[currShape]) * velocityX[currShape] * MAX_VELOCITY);
        int currY = (int) (initialY[currShape]
                + (i - startFrames[currShape]) * velocityY[currShape] * MAX_VELOCITY);

        //Render the shape
        switch (shapeTypes[currShape]) {
        case 0:
            //Circle
            g2d.fill(new Ellipse2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE));
            break;
        case 1:
            //Square
            g2d.fill(new Rectangle2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE));
            break;
        case 2:
            //Arc
            g2d.fill(new Arc2D.Double(currX, currY, SHAPE_SIZE, SHAPE_SIZE, 315, 225, Arc2D.PIE));
            break;
        case 3:
            //Line
            g2d.setStroke(lineStroke);
            g2d.draw(new Line2D.Double(currX, currY, currX + SHAPE_SIZE, currY + SHAPE_SIZE));
            break;
        default:
            throw new RuntimeException();
        }

        //Add some distractor shapes, which are present for one frame only
        for (int j = 0; j < numDistractorsPerFrame; j++) {
            int distractorShapeIdx = r.nextInt(NUM_SHAPES);

            int distractorX = DISTRACTOR_MIN_DIST_FROM_EDGE + r.nextInt(width - SHAPE_SIZE);
            int distractorY = DISTRACTOR_MIN_DIST_FROM_EDGE + r.nextInt(height - SHAPE_SIZE);

            g2d.setColor(new Color(r.nextFloat(), r.nextFloat(), r.nextFloat()));

            switch (distractorShapeIdx) {
            case 0:
                g2d.fill(new Ellipse2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE));
                break;
            case 1:
                g2d.fill(new Rectangle2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE));
                break;
            case 2:
                g2d.fill(new Arc2D.Double(distractorX, distractorY, SHAPE_SIZE, SHAPE_SIZE, 315, 225,
                        Arc2D.PIE));
                break;
            case 3:
                g2d.setStroke(lineStroke);
                g2d.draw(new Line2D.Double(distractorX, distractorY, distractorX + SHAPE_SIZE,
                        distractorY + SHAPE_SIZE));
                break;
            default:
                throw new RuntimeException();
            }
        }

        enc.encodeImage(bi);
        g2d.dispose();
        labels[i] = shapeTypes[currShape];
    }
    enc.finish(); //write .mp4

    return labels;
}

From source file:org.jfree.experimental.chart.plot.dial.StandardDialFrame.java

/**
 * Draws the frame.//from www .  j  a v a  2 s . c om
 * 
 * @param g2  the graphics target.
 * @param plot  the plot.
 * @param frame  the dial's reference frame.
 * @param view  the dial's view rectangle.
 */
public void draw(Graphics2D g2, DialPlot plot, Rectangle2D frame, Rectangle2D view) {

    Shape window = getWindow(frame);
    Shape outerWindow = getOuterWindow(frame);

    Area area1 = new Area(outerWindow);
    Area area2 = new Area(window);
    area1.subtract(area2);
    g2.setPaint(Color.lightGray);
    g2.fill(area1);

    g2.setStroke(this.stroke);
    g2.setPaint(this.foregroundPaint);
    g2.draw(window);
    g2.draw(outerWindow);

}

From source file:pdfreader.PDFReader.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    for (Rectangle rect : squares) {
        g2.draw(rect);
    }/*from  w ww  .j a  va 2 s  .  c  o m*/
}

From source file:LineStyles.java

/** This method draws the example figure */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    // Use anti-aliasing to avoid "jaggies" in the lines
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Define the shape to draw
    GeneralPath shape = new GeneralPath();
    shape.moveTo(xpoints[0], ypoints[0]); // start at point 0
    shape.lineTo(xpoints[1], ypoints[1]); // draw a line to point 1
    shape.lineTo(xpoints[2], ypoints[2]); // and then on to point 2

    // Move the origin to the right and down, creating a margin
    g.translate(20, 40);/* w  ww .jav  a 2s . c o m*/

    // Now loop, drawing our shape with the three different line styles
    for (int i = 0; i < linestyles.length; i++) {
        g.setColor(Color.gray); // Draw a gray line
        g.setStroke(linestyles[i]); // Select the line style to use
        g.draw(shape); // Draw the shape

        g.setColor(Color.black); // Now use black
        g.setStroke(thindashed); // And the thin dashed line
        g.draw(shape); // And draw the shape again.

        // Highlight the location of the vertexes of the shape
        // This accentuates the cap and join styles we're demonstrating
        for (int j = 0; j < xpoints.length; j++)
            g.fillRect(xpoints[j] - 2, ypoints[j] - 2, 5, 5);

        g.drawString(capNames[i], 5, 105); // Label the cap style
        g.drawString(joinNames[i], 5, 120); // Label the join style

        g.translate(150, 0); // Move over to the right before looping again
    }
}

From source file:org.geotools.renderer.chart.GeometryRenderer.java

void drawGeometry(Geometry g, Graphics2D g2, int series, int item, Rectangle2D dataArea, XYPlot plot,
        ValueAxis domainAxis, ValueAxis rangeAxis) {

    if (g instanceof GeometryCollection) {
        GeometryCollection gc = (GeometryCollection) g;
        for (int i = 0; i < gc.getNumGeometries(); i++) {
            drawGeometry(gc.getGeometryN(i), g2, series, item, dataArea, plot, domainAxis, rangeAxis);
        }//from  w ww  .j a  v  a 2  s  .c o  m
    } else if (g instanceof Point) {
        drawCoordinate(((Point) g).getCoordinate(), g2, series, item, dataArea, plot, domainAxis, rangeAxis);
    } else if (g instanceof LineString) {
        g2.draw(new TranslatedLiteShape(g, dataArea, plot, domainAxis, rangeAxis));
    } else {

        if (fillPolygons) {
            Paint p = getSeriesPaint(series);
            if (p instanceof Color) {
                Color c = (Color) p;
                p = new Color(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f, polygonFillOpacity);
            }
            g2.setPaint(p);
            g2.fill(new TranslatedLiteShape(g, dataArea, plot, domainAxis, rangeAxis));

        }
        g2.setPaint(getSeriesPaint(series));
        g2.draw(new TranslatedLiteShape(g, dataArea, plot, domainAxis, rangeAxis));
    }
}

From source file:StrokeTest.java

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    GeneralPath path = new GeneralPath();
    path.moveTo((float) points[0].getX(), (float) points[0].getY());
    for (int i = 1; i < points.length; i++)
        path.lineTo((float) points[i].getX(), (float) points[i].getY());
    BasicStroke stroke;//from   w  ww. j  a va2 s  . co  m
    if (dash) {
        float miterLimit = 10.0F;
        float[] dashPattern = { 10F, 10F, 10F, 10F, 10F, 10F, 30F, 10F, 30F, 10F, 30F, 10F, 10F, 10F, 10F, 10F,
                10F, 30F };
        float dashPhase = 0;
        stroke = new BasicStroke(width, cap, join, miterLimit, dashPattern, dashPhase);
    } else
        stroke = new BasicStroke(width, cap, join);
    g2.setStroke(stroke);
    g2.draw(path);
}

From source file:org.openfaces.component.chart.impl.renderers.LineFillRenderer.java

private void drawItemShape(Graphics2D g2, int row, int column, Shape shape) {
    if (getItemShapeFilled(row, column)) {
        g2.setPaint(getUseFillPaint() ? getItemFillPaint(row, column) : getItemPaint(row, column));

        g2.fill(shape);/*  www  .j  av a2s  . c  om*/
    }

    if (getDrawOutlines()) {
        g2.setPaint(getUseOutlinePaint() ? getItemOutlinePaint(row, column) : getItemPaint(row, column));

        g2.setStroke(getItemOutlineStroke(row, column));

        g2.draw(shape);
    }
}