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.apache.xmlgraphics.ps.ImageEncodingHelperTestCase.java

private BufferedImage prepareImage(final BufferedImage image) {
    final Graphics2D ig = image.createGraphics();
    ig.scale(.5, .5);//from w  ww  .  jav a2 s.  c  om
    ig.setPaint(new Color(128, 0, 0));
    ig.fillRect(0, 0, 100, 50);
    ig.setPaint(Color.orange);
    ig.fillRect(100, 0, 100, 50);
    ig.setPaint(Color.yellow);
    ig.fillRect(0, 50, 100, 50);
    ig.setPaint(Color.red);
    ig.fillRect(100, 50, 100, 50);
    ig.setPaint(new Color(255, 127, 127));
    ig.fillRect(0, 100, 100, 50);
    ig.setPaint(Color.black);
    ig.draw(new Rectangle2D.Double(0.5, 0.5, 199, 149));
    ig.dispose();
    return image;
}

From source file:org.evors.rs.ui.sandpit.WorldViewer.java

@Override
public void draw() {
    Graphics2D g2 = (Graphics2D) getGraphics();
    camera.setWindowSize(new Vector2D(this.getWidth(), this.getHeight()));
    g2.setColor(Color.WHITE);//from w ww  . j a va2 s  . c o m
    g2.fillRect(0, 0, getWidth(), getHeight());
    AffineTransform prevTrans = g2.getTransform();
    g2.setTransform(camera.getTransform());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    grid.draw(g2);
    if (world != null) {
        SandpitRenderer.drawWorld(g2, world);
    }
    g2.setTransform(prevTrans);
}

From source file:web.diva.server.model.GroupColorUtil.java

public synchronized String getImageColor(String hashColor, String path, String colName) {
    try {/*from  w  w  w . j  a  va  2s .  c  om*/
        Color color = hex2Rgb(hashColor);
        BufferedImage image = new BufferedImage(8, 9, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.setBackground(color);
        graphics.setColor(color);
        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
        File f = new File(path, colName + ".png");
        if (!f.exists()) {
            f.createNewFile();
        }

        ImageIO.write(image, "PNG", f);
        // Reading a Image file from file system
        FileInputStream imageInFile = new FileInputStream(f);
        byte imageData[] = new byte[(int) f.length()];
        imageInFile.read(imageData);
        String base64 = Base64.encodeBase64String(imageData);
        base64 = "data:image/png;base64," + base64;

        return base64;
    } catch (IOException ioexp) {
        System.err.println(ioexp.getMessage());
    }
    return null;
}

From source file:org.optaplanner.examples.coachshuttlegathering.swingui.CoachShuttleGatheringWorldPanel.java

private Graphics2D createCanvas(double width, double height) {
    int canvasWidth = (int) Math.ceil(width) + 1;
    int canvasHeight = (int) Math.ceil(height) + 1;
    canvas = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = canvas.createGraphics();
    g.setColor(Color.WHITE);//from  w w  w . j a va2s. c  o  m
    g.fillRect(0, 0, canvasWidth, canvasHeight);
    return g;
}

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

private void drawButton(Graphics2D g, int dx, int height) {
    int barWidth = 4;
    g.setColor(Color.GRAY);/*from w  ww. ja  v  a2s . c o m*/
    g.fillRect(insetX + dx - barWidth / 2, 0, barWidth, height);
    g.draw3DRect(insetX + dx - barWidth / 2, 0, barWidth, height, true);
}

From source file:HighlightedButton.java

/**
 * Creates a new instance of HighlightedButton
 *///w w w .j  av a 2 s  .  c o  m
public HighlightedButton(String label) {
    super(label);

    // Get the Graphics for the image
    Graphics2D g2d = highlight.createGraphics();

    // Erase the image with a transparent background
    g2d.setComposite(AlphaComposite.Clear);
    g2d.fillRect(0, 0, HIGHLIGHT_SIZE, HIGHLIGHT_SIZE);
    g2d.setComposite(AlphaComposite.SrcOver);

    // Draw the highlight
    Point2D center = new Point2D.Float((float) HIGHLIGHT_SIZE / 2.0f, (float) HIGHLIGHT_SIZE / 2.0f);
    float radius = (float) HIGHLIGHT_SIZE / 2.0f;
    float[] dist = { 0.0f, .85f };
    Color[] colors = { Color.white, new Color(255, 255, 255, 0) };
    RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
    g2d.setPaint(paint);
    g2d.fillOval(0, 0, HIGHLIGHT_SIZE, HIGHLIGHT_SIZE);
    g2d.dispose();
}

From source file:picocash.components.HeaderPanel.java

@Override
public void paint(Graphics2D g, Object object, int width, int height) {
    g.setPaint(gradientPaint);//from   w  w  w.  ja  va2 s.  c  o m
    g.fillRect(0, 0, width, height);
    g.setColor(borderColor);
    g.drawRect(0, 0, width - 1, height - 1);
}

From source file:MainClass.java

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

    Graphics2D g2 = (Graphics2D) g;

    int w = getSize().width;
    int h = getSize().height;
    Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
    g2.setClip(e);/*  w w w .j  av a  2 s  .c om*/

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

}

From source file:bjerne.gallery.service.impl.ImageResizeServiceImpl.java

@Override
public void resizeImage(File origImage, File newImage, int width, int height) throws IOException {
    LOG.debug("Entering resizeImage(origImage={}, width={}, height={})", origImage, width, height);
    long startTime = System.currentTimeMillis();
    InputStream is = new BufferedInputStream(new FileInputStream(origImage));
    BufferedImage i = ImageIO.read(is);
    IOUtils.closeQuietly(is);/*from  www.j  ava  2 s . c om*/
    BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    int origWidth = i.getWidth();
    int origHeight = i.getHeight();
    LOG.debug("Original size of image - width: {}, height={}", origWidth, height);
    float widthFactor = ((float) origWidth) / ((float) width);
    float heightFactor = ((float) origHeight) / ((float) height);
    float maxFactor = Math.max(widthFactor, heightFactor);
    int newHeight, newWidth;
    if (maxFactor > 1) {
        newHeight = (int) (((float) origHeight) / maxFactor);
        newWidth = (int) (((float) origWidth) / maxFactor);
    } else {
        newHeight = origHeight;
        newWidth = origWidth;
    }
    LOG.debug("Size of scaled image will be: width={}, height={}", newWidth, newHeight);
    int startX = Math.max((width - newWidth) / 2, 0);
    int startY = Math.max((height - newHeight) / 2, 0);
    Graphics2D scaledGraphics = scaledImage.createGraphics();
    scaledGraphics.setColor(backgroundColor);
    scaledGraphics.fillRect(0, 0, width, height);
    scaledGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    scaledGraphics.drawImage(i, startX, startY, newWidth, newHeight, null);
    OutputStream resultImageOutputStream = new BufferedOutputStream(FileUtils.openOutputStream(newImage));
    String extension = FilenameUtils.getExtension(origImage.getName());
    ImageIO.write(scaledImage, extension, resultImageOutputStream);
    IOUtils.closeQuietly(resultImageOutputStream);
    long duration = System.currentTimeMillis() - startTime;
    LOG.debug("Time in milliseconds to scale {}: {}", newImage.toString(), duration);
}

From source file:org.gitools.ui.app.actions.file.ExportHeatmapImageAction.java

@Override
public void actionPerformed(ActionEvent e) {

    SaveFileWizard saveWiz = SaveFileWizard.createSimple("Export heatmap to image ...",
            FilenameUtils.getName(getSelectedEditor().getName()), Settings.get().getLastExportPath(),
            new FileFormat[] { FileFormats.PNG });

    WizardDialog dlg = new WizardDialog(Application.get(), saveWiz);
    dlg.open();/*from   ww  w.  j  a v  a2s. c  o m*/
    if (dlg.isCancelled()) {
        return;
    }

    Settings.get().setLastExportPath(saveWiz.getFolder());

    final File file = saveWiz.getPathAsFile();

    final String formatExtension = saveWiz.getFormat().getExtension();

    JobThread.execute(Application.get(), new JobRunnable() {
        @Override
        public void run(IProgressMonitor monitor) {
            try {
                monitor.begin("Exporting heatmap to image ...", 1);
                monitor.info("File: " + file.getName());

                Heatmap hm = getHeatmap();

                HeatmapDrawer drawer = new HeatmapDrawer(hm);
                drawer.setPictureMode(true);

                Dimension heatmapSize = drawer.getSize();

                final BufferedImage bi = new BufferedImage(heatmapSize.width, heatmapSize.height,
                        BufferedImage.TYPE_INT_RGB);
                Graphics2D g = bi.createGraphics();
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, heatmapSize.width, heatmapSize.height);
                drawer.draw(g, new Rectangle(new Point(), heatmapSize),
                        new Rectangle(new Point(), heatmapSize));

                ImageIO.write(bi, formatExtension, file);

            } catch (Exception ex) {
                monitor.exception(ex);
            }
        }
    });

    Application.get().showNotification("Image created.", 2000);
}