Example usage for java.awt Graphics2D fillRect

List of usage examples for java.awt Graphics2D fillRect

Introduction

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

Prototype

public abstract void fillRect(int x, int y, int width, int height);

Source Link

Document

Fills the specified rectangle.

Usage

From source file:org.squidy.designer.zoom.NavigationShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

    Graphics2D g = (Graphics2D) paintContext.getGraphics();

    if (showNavigation) {// && !isHierarchicalZoomInProgress()) {

        PBounds bounds = getBoundsReference();

        int x = (int) bounds.getX();
        int y = (int) bounds.getY();
        int width = (int) bounds.getWidth();
        int height = (int) bounds.getHeight();

        g.setColor(Constants.Color.COLOR_SHAPE_BACKGROUND);
        if (isRenderPrimitiveRect())
            g.fillRect(x, y, width, 60);
        else {/*  w ww . ja va2  s.  c  o m*/
            g.clearRect(x, y, width, 60);
            g.fillRoundRect(x, y, width, 60, 25, 25);
        }

        g.setColor(Constants.Color.COLOR_SHAPE_BORDER);
        if (isRenderPrimitiveRect())
            g.drawRect(x, y, width, 60);
        else
            g.drawRoundRect(x, y, width, 60, 25, 25);

        g.setFont(fontBreadcrumb);

        if (titleBounds == null) {
            FontMetrics fm = g.getFontMetrics();
            titleBounds = new Rectangle2D.Double(bounds.getX() + 455 + titleGap,
                    bounds.getY() + 25 - fm.getHeight(), FontUtils.getWidthOfText(fm, getTitle()) + 10,
                    fm.getHeight() + 5);
        }

        // Font font = internalFont.deriveFont(3.2f);
        for (int i = 0; i < 3; i++) {
            if (isRenderPrimitiveRect())
                g.fillRect((int) (bounds.getX() + 430), (int) bounds.getY() + i * 15 + 10, 5, 10);
            else
                g.fillOval((int) (bounds.getX() + 430), (int) bounds.getY() + i * 15 + 10, 5, 10);
        }

        if (!ShapeUtils.isApparent(titleInputWrapper)) {
            g.drawString(getTitle(), (int) (bounds.getX() + 460 + titleGap), (int) (bounds.getY() + 25));
        }

        if (croppedBreadcrumb == null) {
            croppedBreadcrumb = FontUtils.createCroppedLabelIfNecessary(g.getFontMetrics(), getBreadcrumb(),
                    (int) bounds.getWidth() * 10 - 450);
        }
        g.drawString(croppedBreadcrumb, (int) (bounds.getX() + 460), (int) (bounds.getY() + 50));
    }
}

From source file:savant.view.swing.Frame.java

/**
 * Construct a new Frame for holding a track.
 *
 * @param df the DataFormat, so the frame can do any format-specific
 * initialisation (e.g. smaller height for sequence tracks)
 *///w w w  .ja v a  2s. c om
public Frame(DataFormat df) {
    super(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.TRACK));
    sequence = df == DataFormat.SEQUENCE;

    // Component which displays the legend component.
    legend = new JComponent() {
        @Override
        public Dimension getPreferredSize() {
            for (Track t : tracks) {
                Dimension d = t.getRenderer().getLegendSize(t.getDrawingMode());
                if (d != null) {
                    return d;
                }
            }
            return new Dimension(0, 0);
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

        @Override
        public void paintComponent(Graphics g) {
            for (Track t : tracks) {
                Dimension d = t.getRenderer().getLegendSize(t.getDrawingMode());
                if (d != null) {
                    Graphics2D g2 = (Graphics2D) g;
                    GradientPaint gp = new GradientPaint(0, 0, Color.WHITE, 0, 60, new Color(230, 230, 230));
                    g2.setPaint(gp);
                    g2.fillRect(0, 0, d.width, d.height);

                    g2.setColor(Color.BLACK);
                    g2.draw(new Rectangle2D.Double(0, 0, d.width - 1, d.height - 1));
                    t.getRenderer().drawLegend(g2, t.getDrawingMode());
                    return;
                }
            }
        }
    };
    legend.setVisible(false);

    frameLandscape = new JLayeredPane();

    //add graphPane -> jlp -> scrollPane
    jlp = new JLayeredPane();
    jlp.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridx = 0;
    gbc.gridy = 0;

    //scrollpane
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setWheelScrollingEnabled(false);
    scrollPane.setBorder(null);

    graphPane = new GraphPane(this);
    jlp.add(graphPane, gbc, 0);

    scrollPane.getViewport().add(jlp);

    //GRID FRAMEWORK AND COMPONENT ADDING...
    frameLandscape.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;

    //add sidepanel
    sidePanel = new JPanel() {
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(0, 0);
        }
    };
    sidePanel.setLayout(new GridBagLayout());
    sidePanel.setOpaque(false);
    sidePanel.setVisible(false);
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 1;
    c.gridy = 0;
    c.insets = new Insets(0, 0, 0, 16); // Leave 16 pixels so that we don't sit on top of the scroll-bar.
    frameLandscape.setLayer(sidePanel, JLayeredPane.PALETTE_LAYER);
    frameLandscape.add(sidePanel, c);

    addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            Dimension dim = getSize();
            if (dim != null) {
                // TODO: The following shouldn't be necessary, but it seems to be.
                int expectedWidth = frameLandscape.getWidth();
                if (expectedWidth != graphPane.getWidth()) {
                    Dimension goodSize = new Dimension(expectedWidth, graphPane.getHeight());
                    graphPane.setPreferredSize(goodSize);
                    graphPane.setSize(goodSize);
                }

                setLegendVisible(true);
            }
        }
    });

    //add graphPane to all cells
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.insets = new Insets(0, 0, 0, 0);

    frameLandscape.setLayer(scrollPane, JLayeredPane.DEFAULT_LAYER);
    frameLandscape.add(scrollPane, c);

    // Add our progress-panel.  If setTracks is called promptly, it will be cleared
    // away before it ever has a chance to draw.
    getContentPane().add(new ProgressPanel(null), BorderLayout.CENTER);
}

From source file:jhplot.HPlotChart.java

public void drawToGraphics2D(Graphics2D g, int width, int height) {
    // self.chartCoordsMap = {} #Maps a chart to its raw screen coords, used
    // for converting coords
    g.setColor(Color.white);/*from  w w w  .j a  v  a2  s  .  c om*/
    g.fillRect(0, 0, width, height);
    //
    // int boxWidth = width / this.chartarray[0].length;
    // int boxHeight = height / this.chartarray.length;

    int cols = 1;
    int rows = 1;
    int boxWidth = width / cols;
    int boxHeight = height / rows;

    //
    // # print "boxWidth ", boxWidth
    // # print "boxHeight ", boxHeight

    // for (int row = 0; row < chartarray.length; row++)
    int currentChartIndex = 0;

    for (int i2 = 0; i2 < rows; i2++) {
        for (int i1 = 0; i1 < cols; i1++) {

            currentChartIndex++;
            if (chart != null) {

                int rowsUsed = 1;
                int colsUsed = 1;
                int chartX = boxWidth * i1;
                int chartY = boxHeight * i2;
                int chartwidth = boxWidth;
                int chartheight = boxHeight;
                // #Get Horizontalspace
                // for (int c = col; c > -1; c--)
                // {
                // // for c in range(col, -1, -1):
                // // if self.chartArray[row][c] == None:
                // if(this.chartarray[row][c] == null)
                // rowsUsed++;
                //
                // // rowsUsed = rowsUsed + 1
                // // #print "adding row"
                // }
                chartwidth = boxWidth * rowsUsed;
                chartheight = boxHeight;
                chartX = chartX - (rowsUsed - 1) * boxWidth;
                //
                // # chart.configureDomainAxes()
                // # chart.configureRangeAxes()
                //
                // #Testing axes ranges not updated
                // from org.jfree.chart.event import PlotChangeEvent
                // chart[i1][i2].plotChanged(new
                // PlotChangeEvent(chart[i1][i2].getXYPlot()));
                chart.plotChanged(new PlotChangeEvent(chart.getPlot()));
                //
                ChartRenderingInfo info = new ChartRenderingInfo();
                //
                chart.draw(g, new java.awt.Rectangle(chartX, chartY, chartwidth, chartheight),
                        new Point(chartX, chartY), info);
                // self.chartToInfoMap[chart] = info
                //
                // self.chartCoordsMap[chart] = [chartX ,chartY,chartwidth,
                // chartheight]

            }
        }
    }

}

From source file:tilt.image.Picture.java

/**
 * Convert from the original png file to greyscale png. Save original.
 * @throws ImageException //from  w  ww  .j  a  v  a  2  s .c om
 */
void convertToGreyscale() throws ImageException {
    try {
        BufferedImage png = ImageIO.read(orig);
        BufferedImage grey = new BufferedImage(png.getWidth(), png.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = grey.getGraphics();
        g.drawImage(png, 0, 0, null);
        g.dispose();
        if (!isWholePicture()) {
            // clear excluded regions
            Graphics2D g2d = grey.createGraphics();
            g2d.setColor(Color.white);
            if (coords[0][0].doubleValue() > 0.0) {
                int w = grey.getWidth();
                int h = grey.getHeight();
                g2d.fillRect(0, 0, getPropVal(coords[0][0], w), h);
            }
            if (coords[1][0].doubleValue() < 100.0) {
                int x = getPropVal(coords[1][0], grey.getWidth());
                g2d.fillRect(x, 0, grey.getWidth() - x, grey.getHeight());
            }
            if (coords[2][1].doubleValue() < 100.0) {
                int y = getPropVal(coords[2][1], grey.getHeight());
                g2d.fillRect(0, y, grey.getWidth(), grey.getHeight() - y);
            }
            if (coords[0][1].doubleValue() > 0.0) {
                int y = getPropVal(coords[0][1], grey.getHeight());
                g2d.fillRect(0, 0, grey.getWidth(), y);
            }
            g2d.dispose();
        }
        greyscale = File.createTempFile(PictureRegistry.PREFIX, PictureRegistry.SUFFIX);
        ImageIO.write(grey, "png", greyscale);
    } catch (Exception e) {
        throw new ImageException(e);
    }
}

From source file:org.pentaho.di.core.gui.SwingDirectGC.java

private void drawImage(SwingUniversalImage img, int locationX, int locationY, int imageSize) {
    if (isDrawingPixelatedImages() && img.isBitmap()) {
        BufferedImage bi = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, imageSize, imageSize);
        g2.drawImage(img.getAsBitmapForSize(imageSize, imageSize), 0, 0, observer);
        g2.dispose();/*from w  w w .j  a  va 2 s . co  m*/

        for (int x = 0; x < bi.getWidth(observer); x++) {
            for (int y = 0; y < bi.getHeight(observer); y++) {
                int rgb = bi.getRGB(x, y);
                gc.setColor(new Color(rgb));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x,
                        locationY + yOffset + y);
            }
        }
    } else {
        gc.setBackground(Color.white);
        gc.clearRect(locationX, locationY, imageSize, imageSize);
        img.drawToGraphics(gc, locationX, locationY, imageSize, imageSize);
    }
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Draws an AWT image into a BufferedImage using an AWT Graphics2D implementation
 *
 * @param img the AWT image/* ww  w  .  j ava 2s . co m*/
 * @param bufferedImage the AWT buffered image
 * @param width the image width
 * @param height the image height
 * @param observer the image observer
 * @return true if the image was drawn
 */
private boolean drawBufferedImage(Image img, BufferedImage bufferedImage, int width, int height,
        ImageObserver observer) {

    java.awt.Graphics2D g2d = bufferedImage.createGraphics();
    try {
        g2d.setComposite(AlphaComposite.SrcOver);

        Color color = new Color(1, 1, 1, 0);
        g2d.setBackground(color);
        g2d.setPaint(color);

        g2d.fillRect(0, 0, width, height);

        int imageWidth = bufferedImage.getWidth();
        int imageHeight = bufferedImage.getHeight();
        Rectangle clipRect = new Rectangle(0, 0, imageWidth, imageHeight);
        g2d.clip(clipRect);

        g2d.setComposite(gc.getComposite());

        return g2d.drawImage(img, 0, 0, imageWidth, imageHeight, observer);
    } finally {
        g2d.dispose(); //drawn so dispose immediately to free system resource
    }
}

From source file:com.piketec.jenkins.plugins.tpt.publisher.PieChart.java

private void drawLegendLine(Graphics2D g2, int verticalOffset, double horizontalNumberOffset, Color col,
        String txt, String numberText, boolean textIsPlural, boolean withSubSegment, String subSegmentText,
        String subNumberText, boolean subTextIsPlural) {
    int left = 620;
    // col == null --> total --> kein Rechteck
    if (col != null) {
        g2.drawImage(keyShadow.getImage(), left, 30 + verticalOffset, keyShadow.getImageObserver());
        g2.setColor(col);/*from  www.  j av a  2 s . c  o m*/
        g2.fillRect(left + 13, 37 + verticalOffset, 45, 45);
        if (withSubSegment) {
            Polygon p = new Polygon(new int[] { left + 13 + 45, left + 13 + 45, left + 13 },
                    new int[] { verticalOffset + 37, verticalOffset + 37 + 45, verticalOffset + 37 + 45 }, 3);
            g2.setColor(col.darker());
            g2.fillPolygon(p);
        }
    }
    g2.setColor(Color.BLACK);
    StringBuffer sb = new StringBuffer(numberText);
    sb.append("  ").append(plural(textIsPlural, txt));
    if (withSubSegment) {
        sb.append(" with ");
        sb.append(subNumberText);
        sb.append(" ");
        sb.append(plural(subTextIsPlural, subSegmentText));
    }
    g2.drawString(sb.toString(), (int) (left + 80 + horizontalNumberOffset), 30 + 41 + verticalOffset);

}

From source file:com.igormaznitsa.mindmap.swing.panel.MindMapPanel.java

private static void drawBackground(final Graphics2D g, final MindMapPanelConfig cfg) {
    final Rectangle clipBounds = g.getClipBounds();

    if (cfg.isDrawBackground()) {
        g.setColor(cfg.getPaperColor());
        g.fillRect(clipBounds.x, clipBounds.y, clipBounds.width, clipBounds.height);

        if (cfg.isShowGrid()) {
            final double scaledGridStep = cfg.getGridStep() * cfg.getScale();

            final float minX = clipBounds.x;
            final float minY = clipBounds.y;
            final float maxX = clipBounds.x + clipBounds.width;
            final float maxY = clipBounds.y + clipBounds.height;

            g.setColor(cfg.getGridColor());

            for (float x = 0.0f; x < maxX; x += scaledGridStep) {
                if (x < minX) {
                    continue;
                }//ww w .  j  av  a  2 s  .  c  om
                final int intx = Math.round(x);
                g.drawLine(intx, (int) minY, intx, (int) maxY);
            }

            for (float y = 0.0f; y < maxY; y += scaledGridStep) {
                if (y < minY) {
                    continue;
                }
                final int inty = Math.round(y);
                g.drawLine((int) minX, inty, (int) maxX, inty);
            }
        }
    }
}

From source file:edu.gmu.cs.sim.util.media.chart.ChartGenerator.java

BufferedImage getBufferedImage() {
    // make a buffer
    if (buffer == null || buffer.getWidth(null) != chartPanel.getWidth()
            || buffer.getHeight(null) != chartPanel.getHeight()) {
        buffer = getGraphicsConfiguration().createCompatibleImage((int) chartPanel.getWidth(),
                (int) chartPanel.getHeight());
    }//from w w w .jav a 2 s .c o m

    // paint to the buffer
    Graphics2D g = (Graphics2D) (buffer.getGraphics());
    g.setColor(chartPanel.getBackground());
    g.fillRect(0, 0, buffer.getWidth(null), buffer.getHeight(null));
    chartPanel.paintComponent(g);
    g.dispose();
    return buffer;
}

From source file:org.tros.logo.swing.LogoPanel.java

private void canvascolor(final Color color) {
    Drawable command = new Drawable() {

        @Override//from  ww w .  jav  a 2 s.  c o m
        public void draw(Graphics2D g2, TurtleState turtleState) {
            g2.setColor(color);
            g2.fillRect(0, 0, turtleState.width > 0 ? (int) turtleState.width : getWidth(),
                    turtleState.height > 0 ? (int) turtleState.height : getHeight());
            g2.setColor(turtleState.penColor);
        }

        @Override
        public void addListener(DrawListener listener) {
        }

        @Override
        public void removeListener(DrawListener listener) {
        }

        @Override
        public Drawable cloneDrawable() {
            return this;
        }
    };
    submitCommand(command);
}