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:Main.java

static public void main(String args[]) throws Exception {
    int width = 200, height = 200;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.fillRect(0, 0, width - 1, height - 1);

    Iterator imageWriters = ImageIO.getImageWritersByFormatName("GIF");
    ImageWriter imageWriter = (ImageWriter) imageWriters.next();
    File file = new File("filename.gif");
    ImageOutputStream ios = ImageIO.createImageOutputStream(file);
    imageWriter.setOutput(ios);//from   w w  w .j  av a2 s  .c om
    imageWriter.write(bi);
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    int width = 200, height = 200;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.fillRect(0, 0, width - 1, height - 1);

    BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    ig2.setPaint(Color.lightGray);
    ig2.setStroke(stroke);/*from ww  w  .j  a  va 2 s  . c om*/
    ig2.draw(new Ellipse2D.Double(0, 0, 100, 100));

    ImageIO.write(bi, "GIF", new File("a.gif"));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_USHORT_GRAY);
    Graphics2D g2d = bi.createGraphics();
    g2d.setColor(Color.WHITE);// w w  w  .  j a v  a 2s.co  m
    g2d.fillRect(0, 0, 100, 100);
    g2d.setColor(new Color(255, 123, 0));

    g2d.drawLine(100, 100, 1, 1);

    g2d.dispose();
    BufferedImage scaledImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_USHORT_GRAY);

    Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    graphics2D.drawImage(bi, 0, 0, 1000, 1000, null);
    graphics2D.dispose();
    ImageIO.write(scaledImage, "png", new File("c:/Java_Dev/Test.png"));
    bi.flush();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    BufferedImage img = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = img.createGraphics();
    g.setColor(Color.white);/* ww  w  .j  a  va 2  s .co m*/
    g.fillRect(0, 0, 20, 20);
    g.setColor(Color.black);
    g.fillRect(5, 5, 10, 10);

    Color[] mapping = new Color[] { Color.black, Color.white, // replace black with white 
            Color.white, Color.green // and white with green
    };

    ImageIO.write(img, "png", new File("original.png"));
    swapColors(img, mapping);
    ImageIO.write(img, "png", new File("swapped.png"));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int width = 100;
    int height = 100;

    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = bufferedImage.createGraphics();

    g2d.setColor(Color.white);//from   ww  w. j a v a  2  s .  c o  m
    g2d.fillRect(0, 0, width, height);
    g2d.setColor(Color.black);
    g2d.fillOval(0, 0, width, height);

    g2d.dispose();
    RenderedImage rendImage = bufferedImage;

    File file = new File("newimage.png");
    ImageIO.write(rendImage, "png", file);

    file = new File("newimage.jpg");
    ImageIO.write(rendImage, "jpg", file);
}

From source file:MakeFades.java

public static void main(String[] args) throws IOException, NumberFormatException {

    // Create from and to colors based on those arguments
    Color from = Color.RED; // transparent
    Color to = Color.BLACK; // opaque

    // Loop through the sizes and directions, and create an image for each
    for (int s = 0; s < sizes.length; s++) {
        for (int d = 0; d < directions.length; d++) {
            // This is the size of the image
            int size = sizes[s];

            // Create a GradientPaint for this direction and size
            Paint paint = new GradientPaint(directions[d][0] * size, directions[d][1] * size, from,
                    directions[d][2] * size, directions[d][3] * size, to);

            // Start with a blank image that supports transparency
            BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

            // Now use fill the image with our color gradient
            Graphics2D g = image.createGraphics();
            g.setPaint(paint);/*ww w .ja  va  2  s.c om*/
            g.fillRect(0, 0, size, size);

            // This is the name of the file we'll write the image to
            File file = new File("fade-to-" + sizeNames[s] + "-" + directionNames[d] + ".png");

            // Save the image in PNG format using the javax.imageio API
            javax.imageio.ImageIO.write(image, "png", file);

            // Show the user our progress by printing the filename
            System.out.println(file);
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SplashScreen splash = SplashScreen.getSplashScreen();
    Graphics2D g = (Graphics2D) splash.createGraphics();
    Dimension dim = splash.getSize();
    for (int i = 0; i < 100; i++) {
        g.setColor(Color.RED);/* w ww  .  ja  v  a 2  s  .c o  m*/
        g.fillRect(50, 50, dim.width - 100, dim.height - 100);
        splash.update();
        try {
            Thread.sleep(250);
        } catch (InterruptedException ignored) {
        }
    }
    JFrame frame = new JFrame("Splash Me2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Hello, Splash", JLabel.CENTER);
    frame.add(label, BorderLayout.CENTER);
    frame.setSize(300, 95);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SplashScreen splash = SplashScreen.getSplashScreen();
    Graphics2D g = (Graphics2D) splash.createGraphics();
    System.out.println(splash.getBounds());
    Dimension dim = splash.getSize();
    for (int i = 0; i < 100; i++) {
        g.setColor(Color.RED);/*from   w w w  .j a va2 s .c om*/
        g.fillRect(50, 50, dim.width - 100, dim.height - 100);
        splash.update();
        try {
            Thread.sleep(250);
        } catch (InterruptedException ignored) {
        }
    }
    JFrame frame = new JFrame("Splash Me2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Hello, Splash", JLabel.CENTER);
    frame.add(label, BorderLayout.CENTER);
    frame.setSize(300, 95);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
            Graphics2D imageGraphics = image.createGraphics();
            GradientPaint gp = new GradientPaint(20f, 20f, Color.red, 380f, 280f, Color.orange);
            imageGraphics.setPaint(gp);/*from w ww  .ja va 2s  .  com*/
            imageGraphics.fillRect(0, 0, 400, 300);

            JLabel textLabel = new JLabel("java2s.com");
            textLabel.setSize(textLabel.getPreferredSize());

            Dimension d = textLabel.getPreferredSize();
            BufferedImage bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            g.setColor(new Color(255, 200, 255, 128));
            g.fillRoundRect(0, 0, bi.getWidth(f), bi.getHeight(f), 15, 10);
            g.setColor(Color.black);
            textLabel.paint(g);
            Graphics g2 = image.getGraphics();
            g2.drawImage(bi, 20, 20, f);

            ImageIcon ii = new ImageIcon(image);
            JLabel imageLabel = new JLabel(ii);

            f.getContentPane().add(imageLabel);
            f.pack();

            f.setVisible(true);
        }
    });
}

From source file:Draw2DTest.java

public static void main(String[] args) {
    final Graphics2DRenderer renderer = new Graphics2DRenderer();
    Shell shell = new Shell();
    shell.setSize(350, 350);/*from  w w  w  .  jav a2s  . c  o  m*/

    shell.open();
    shell.setText("Draw2d Hello World");
    LightweightSystem lws = new LightweightSystem(shell);
    IFigure figure = new Figure() {
        protected void paintClientArea(org.eclipse.draw2d.Graphics graphics) {
            Dimension controlSize = getSize();

            renderer.prepareRendering(graphics);
            // prepares the Graphics2D renderer

            // gets the Graphics2D context and switch on the antialiasing
            Graphics2D g2d = renderer.getGraphics2D();
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // paints the background with a color gradient
            g2d.setPaint(new GradientPaint(0.0f, 0.0f, java.awt.Color.yellow, (float) controlSize.width,
                    (float) controlSize.width, java.awt.Color.white));
            g2d.fillRect(0, 0, controlSize.width, controlSize.width);

            // draws rotated text
            g2d.setFont(new java.awt.Font("SansSerif", java.awt.Font.BOLD, 16));
            g2d.setColor(java.awt.Color.blue);

            g2d.translate(controlSize.width / 2, controlSize.width / 2);
            int nbOfSlices = 18;
            for (int i = 0; i < nbOfSlices; i++) {
                g2d.drawString("Angle = " + (i * 360 / nbOfSlices) + "\u00B0", 30, 0);
                g2d.rotate(-2 * Math.PI / nbOfSlices);
            }

            // now that we are done with Java2D, renders Graphics2D
            // operation
            // on the SWT graphics context
            renderer.render(graphics);

            // now we can continue with pure SWT paint operations
            graphics.drawOval(0, 0, controlSize.width, controlSize.width);
        }
    };
    lws.setContents(figure);
    Display display = Display.getDefault();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    renderer.dispose();
}