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:com.github.cmisbox.ui.BaseFrame.java

public BaseFrame() {
    super(AWTUtilitiesWrapper.isTranslucencyCapable(BaseFrame.gc) ? BaseFrame.gc : null);
    this.log = LogFactory.getLog(this.getClass());

    this.gradient = false;
    this.setUndecorated(true);
    this.mainPanel = new JPanel(new GridBagLayout()) {

        private static final long serialVersionUID = 1035974033526970010L;

        protected void paintComponent(Graphics g) {
            if ((g instanceof Graphics2D) && BaseFrame.this.gradient) {
                final int R = 0;
                final int G = 0;
                final int B = 0;

                Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 192), this.getWidth(),
                        this.getHeight(), new Color(R, G, B, 255), true);
                Graphics2D g2d = (Graphics2D) g;
                g2d.setPaint(p);//www .  ja  v a 2  s  . com
                g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
            } else {
                super.paintComponent(g);
            }
        }
    };

    this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    GridBagConstraints gbc = new GridBagConstraints();

    this.mainPanel.setDoubleBuffered(false);
    this.mainPanel.setOpaque(false);
    this.mainPanel.setBorder(BorderFactory.createLineBorder(Color.white));

    JLabel title = new JLabel(this.getWindowTitle(), SwingConstants.CENTER);
    title.setForeground(Color.white);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 100;

    this.mainPanel.add(title, gbc);

    Image closeImg = this.getImage("images/application-exit.png", 32, 32);
    JLabel close = new JLabel(new ImageIcon(closeImg), SwingConstants.RIGHT);

    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = 2;
    gbc.weightx = 0;

    this.mainPanel.add(close, gbc);

    close.addMouseListener(this.closeAdapter);

    this.getContentPane().add(this.mainPanel, BorderLayout.CENTER);

    this.initComponents();

    this.pack();

    this.mainPanel.setOpaque(!this.gradient);
    if (!this.gradient) {
        this.mainPanel.setBackground(new Color(0, 0, 0, 208));
    }

    this.setLocationRelativeTo(null);
    AWTUtilitiesWrapper.setWindowOpaque(this, false);
    this.setVisible(true);
    this.setAlwaysOnTop(true);

}

From source file:CompositeEffects.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;

    // fill the background
    g.setPaint(new Color(175, 175, 175));
    g.fillRect(0, 0, getWidth(), getHeight());

    // Set text attributes
    g.setColor(Color.black);/*ww w . j  av  a  2 s .c  om*/
    g.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Draw the unmodified image
    g.translate(10, 10);
    g.drawImage(cover, 0, 0, this);
    g.drawString("SRC_OVER", 0, COVERHEIGHT + 15);

    // Draw the cover again, using AlphaComposite to make the opaque
    // colors of the image 50% translucent
    g.translate(COVERWIDTH + 10, 0);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    g.drawImage(cover, 0, 0, this);

    // Restore the pre-defined default Composite for the screen, so
    // opaque colors stay opaque.
    g.setComposite(AlphaComposite.SrcOver);
    // Label the effect
    g.drawString("SRC_OVER, 50%", 0, COVERHEIGHT + 15);

    // Now get an offscreen image to work with. In order to achieve
    // certain compositing effects, the drawing surface must support
    // transparency. Onscreen drawing surfaces cannot, so we have to do the
    // compositing in an offscreen image that is specially created to have
    // an "alpha channel", then copy the final result to the screen.
    BufferedImage offscreen = new BufferedImage(COVERWIDTH, COVERHEIGHT, BufferedImage.TYPE_INT_ARGB);

    // First, fill the image with a color gradient background that varies
    // left-to-right from opaque to transparent yellow
    Graphics2D osg = offscreen.createGraphics();
    osg.setPaint(new GradientPaint(0, 0, Color.yellow, COVERWIDTH, 0, new Color(255, 255, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);

    // Now copy the cover image on top of this, but use the DstOver rule
    // which draws it "underneath" the existing pixels, and allows the
    // image to show depending on the transparency of those pixels.
    osg.setComposite(AlphaComposite.DstOver);
    osg.drawImage(cover, 0, 0, this);

    // And display this composited image on the screen. Note that the
    // image is opaque and that none of the screen background shows through
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("DST_OVER", 0, COVERHEIGHT + 15);

    // Now start over and do a new effect with the off-screen image.
    // First, fill the offscreen image with a new color gradient. We
    // don't care about the colors themselves; we just want the
    // translucency of the background to vary. We use opaque black to
    // transparent black. Note that since we've already used this offscreen
    // image, we set the composite to Src, we can fill the image and
    // ignore anything that is already there.
    osg.setComposite(AlphaComposite.Src);
    osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);

    // Now set the compositing type to SrcIn, so colors come from the
    // source, but translucency comes from the destination
    osg.setComposite(AlphaComposite.SrcIn);

    // Draw our loaded image into the off-screen image, compositing it.
    osg.drawImage(cover, 0, 0, this);

    // And then copy our off-screen image to the screen. Note that the
    // image is translucent and some of the image shows through.
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("SRC_IN", 0, COVERHEIGHT + 15);

    // If we do the same thing but use SrcOut, then the resulting image
    // will have the inverted translucency values of the destination
    osg.setComposite(AlphaComposite.Src);
    osg.setPaint(new GradientPaint(0, 0, Color.black, COVERWIDTH, COVERHEIGHT, new Color(0, 0, 0, 0)));
    osg.fillRect(0, 0, COVERWIDTH, COVERHEIGHT);
    osg.setComposite(AlphaComposite.SrcOut);
    osg.drawImage(cover, 0, 0, this);
    g.translate(COVERWIDTH + 10, 0);
    g.drawImage(offscreen, 0, 0, this);
    g.drawString("SRC_OUT", 0, COVERHEIGHT + 15);

    // Here's a cool effect; it has nothing to do with compositing, but
    // uses an arbitrary shape to clip the image. It uses Area to combine
    // shapes into more complicated ones.
    g.translate(COVERWIDTH + 10, 0);
    Shape savedClip = g.getClip(); // Save current clipping region
    // Create a shape to use as the new clipping region.
    // Begin with an ellipse
    Area clip = new Area(new Ellipse2D.Float(0, 0, COVERWIDTH, COVERHEIGHT));
    // Intersect with a rectangle, truncating the ellipse.
    clip.intersect(new Area(new Rectangle(5, 5, COVERWIDTH - 10, COVERHEIGHT - 10)));
    // Then subtract an ellipse from the bottom of the truncated ellipse.
    clip.subtract(new Area(new Ellipse2D.Float(COVERWIDTH / 2 - 40, COVERHEIGHT - 20, 80, 40)));
    // Use the resulting shape as the new clipping region
    g.clip(clip);
    // Then draw the image through this clipping region
    g.drawImage(cover, 0, 0, this);
    // Restore the old clipping region so we can label the effect
    g.setClip(savedClip);
    g.drawString("Clipping", 0, COVERHEIGHT + 15);
}

From source file:org.n52.server.sos.generator.Generator.java

protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo)
        throws Exception {
    int width = options.getWidth();
    int height = options.getHeight();
    BufferedImage image = chart.createBufferedImage(width, height, renderingInfo);
    Graphics2D chartGraphics = image.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    try {/* www .j a v a  2  s  .  com*/
        return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null);
    } catch (IOException e) {
        throw new Exception("Could not save PNG!", e);
    }
}

From source file:org.n52.server.sos.generator.DiagramGenerator.java

/**
 * Produce legend.//from   ww  w  .  ja v a 2s . co  m
 * 
 * @param options
 *            the options
 * @param out
 *            the out
 * @throws OXFException
 *             the oXF exception
 */
public void createLegend(DesignOptions options, OutputStream out) throws OXFException, IOException {

    int topMargin = 10;
    int leftMargin = 30;
    int iconWidth = 15;
    int iconHeight = 15;
    int verticalSpaceBetweenEntries = 20;
    int horizontalSpaceBetweenIconAndText = 15;

    DesignDescriptionList ddList = buildUpDesignDescriptionList(options);
    int width = 800;
    int height = topMargin + (ddList.size() * (iconHeight + verticalSpaceBetweenEntries));

    BufferedImage legendImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D legendGraphics = legendImage.createGraphics();
    legendGraphics.setColor(Color.white);
    legendGraphics.fillRect(0, 0, width, height);

    int offset = 0;
    for (RenderingDesign dd : ddList.getAllDesigns()) {
        int yPos = topMargin + offset * iconHeight + offset * verticalSpaceBetweenEntries;

        // icon:
        legendGraphics.setColor(dd.getColor());
        legendGraphics.fillRect(leftMargin, yPos, iconWidth, iconHeight);

        // text:
        legendGraphics.setColor(Color.black);

        legendGraphics.drawString(dd.getFeature().getLabel() + " - " + dd.getLabel(),
                leftMargin + iconWidth + horizontalSpaceBetweenIconAndText, yPos + iconHeight);

        offset++;
    }

    // draw legend into image:
    JPEGEncodeParam p = new JPEGEncodeParam();
    p.setQuality(1f);
    ImageEncoder encoder = ImageCodec.createImageEncoder("jpeg", out, p);

    encoder.encode(legendImage);
}

From source file:edu.csun.ecs.cs.multitouchj.application.whiteboard.ui.InteractiveCanvas.java

public void clearCanvas(Color color) {
    synchronized (resizableBufferedImage) {
        BufferedImage bufferedImage = resizableBufferedImage.getBufferedImage();

        Graphics2D graphics = bufferedImage.createGraphics();
        graphics.setPaint(color);/*  www  .  j  a va  2 s.  c  om*/
        graphics.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
        graphics.dispose();

        setDirty(true);
    }
}

From source file:ClipArea.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    int w = getSize().width;
    int h = getSize().height;

    if (clip) {/*from www  . j av a2 s.  co m*/
        Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
        g2.setClip(e);

        g2.setColor(Color.yellow);
        g2.fillRect(0, 0, w, h);
    }

    if (clipFurther) {
        Rectangle r = new Rectangle(w / 2, h / 2, w / 2, h / 2);
        g2.clip(r);

        g2.setColor(Color.green);
        g2.fillRect(0, 0, w, h);
    }
}

From source file:org.n52.server.io.Generator.java

protected String createAndSaveImage(DesignOptions options, JFreeChart chart, ChartRenderingInfo renderingInfo)
        throws GeneratorException {
    int width = options.getWidth();
    int height = options.getHeight();
    BufferedImage image = chart.createBufferedImage(width, height, renderingInfo);
    Graphics2D chartGraphics = image.createGraphics();
    chartGraphics.setColor(Color.white);
    chartGraphics.fillRect(0, 0, width, height);
    chart.draw(chartGraphics, new Rectangle2D.Float(0, 0, width, height));

    try {//  www  .  j  av a  2s . c  o  m
        return ServletUtilities.saveChartAsPNG(chart, width, height, renderingInfo, null);
    } catch (IOException e) {
        throw new GeneratorException("Could not save PNG!", e);
    }
}

From source file:AntiAlias.java

/** Draw the example */
public void paint(Graphics g1) {
    Graphics2D g = (Graphics2D) g1;
    BufferedImage image = // Create an off-screen image
            new BufferedImage(65, 35, BufferedImage.TYPE_INT_RGB);
    Graphics2D ig = image.createGraphics(); // Get its Graphics for drawing

    // Set the background to a gradient fill. The varying color of
    // the background helps to demonstrate the anti-aliasing effect
    ig.setPaint(new GradientPaint(0, 0, Color.black, 65, 35, Color.white));
    ig.fillRect(0, 0, 65, 35);

    // Set drawing attributes for the foreground.
    // Most importantly, turn on anti-aliasing.
    ig.setStroke(new BasicStroke(2.0f)); // 2-pixel lines
    ig.setFont(new Font("Serif", Font.BOLD, 18)); // 18-point font
    ig.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Anti-alias!
            RenderingHints.VALUE_ANTIALIAS_ON);

    // Now draw pure blue text and a pure red oval
    ig.setColor(Color.blue);//from  ww  w .  j  a  v a 2s.co  m
    ig.drawString("Java", 9, 22);
    ig.setColor(Color.red);
    ig.drawOval(1, 1, 62, 32);

    // Finally, scale the image by a factor of 10 and display it
    // in the window. This will allow us to see the anti-aliased pixels
    g.drawImage(image, AffineTransform.getScaleInstance(10, 10), this);

    // Draw the image one more time at its original size, for comparison
    g.drawImage(image, 0, 0, this);
}

From source file:Main.java

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    GradientPaint gp1 = new GradientPaint(5, 5, Color.red, 20, 20, Color.yellow, true);

    gp1.createContext(ColorModel.getRGBdefault(), new Rectangle(0, 0, 30, 40), new Rectangle(0, 0, 30, 40),
            new AffineTransform(), null);

    System.out.println(gp1.getTransparency());
    g2d.setPaint(gp1);//from  w w  w  . j  a  va  2s . c o m
    g2d.fillRect(20, 20, 300, 40);

}

From source file:de.fhg.igd.iva.explorer.main.JStatBar.java

@Override
protected void paintComponent(Graphics g1) {
    super.paintComponent(g1);

    Graphics2D g = (Graphics2D) g1;

    int width = getWidth() - 1;
    int height = getHeight() - 1;

    int whiskerSize = 6;

    // fill background rect
    g.setColor(Color.WHITE);//from  w  w w  .jav a2  s .c  o  m
    g.fillRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY);

    int q10X = mapXToScreen(stats.getPercentile(10.0), width);
    int q25X = mapXToScreen(stats.getPercentile(25.0), width);
    int q50X = mapXToScreen(stats.getPercentile(50.0), width);
    int q75X = mapXToScreen(stats.getPercentile(75.0), width);
    int q90X = mapXToScreen(stats.getPercentile(90.0), width);

    g.setColor(Color.PINK);
    int leftX = Math.min(q25X, q75X);
    int rightX = Math.max(q25X, q75X);

    g.fillRect(insetX + leftX, insetY + 1, rightX - leftX, height - 2 * insetY - 1);
    g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2);
    g.drawLine(insetX + q10X, (height - whiskerSize) / 2, insetX + q10X, (height + whiskerSize) / 2);
    g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2);
    g.drawLine(insetX + q90X, (height - whiskerSize) / 2, insetX + q90X, (height + whiskerSize) / 2);

    g.setColor(Color.RED);
    g.drawLine(insetX + q50X, insetY + 1, insetX + q50X, height - insetY - 1);

    // draw outline border rect
    g.setColor(Color.BLACK);
    g.drawRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY);

    int buttonX = mapXToScreen(quality, width);
    drawButton(g, buttonX, height);
}