2

In Java, there is the Graphics2D.fillRect(x, y, width, height) function. In my program, I am looking for something similar, yet completely opposite.

I need to fill everything on the screen except this certain x, y, width, height, sort of like an anti-fillRect. Is there a built in function that I am overlooking, or can you point me in the right direction to create one?

Not required, but it would be a nice bonus if it could work with other Java 2D shapes.

Beaurocks16
  • 341
  • 1
  • 7
  • 16
  • Possibly set the background color? I can't think of a "pre built" function offhand but it would be trivial to write. Keep in mind you probably know the width and height of your surface (or can compute it) so this should be trivial either by drawing four rectangles or, frankly, drawing over a rectangle with your new, smaller, rectangle. (or just set the background color...) – Daniel B. Chapman Aug 25 '14 at 02:50
  • If, for example, you're looking to fill the outside of a rectangle with black, leaving the center white, you could instead use `setBackground(Color.BLACK)` and just fill the center with a white rectangle. – FThompson Aug 25 '14 at 02:51

1 Answers1

5

There are a few ways that might be achieved, the easiest might be to use java.awt.geom.Area which is a highly versatile Shape implementation, which allows you to add and subtract Areas from it, for example...

Clip

I've left the fill color slightly translucent to prove the point ;)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CutExample {

    public static void main(String[] args) {
        new CutExample();
    }

    public CutExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            try {
                img = ImageIO.read(...);
            } catch (IOException ex) {
                Logger.getLogger(CutExample.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getWidth()) / 2;
                g2d.drawImage(img, x, y, this);

                Area outter = new Area(new Rectangle(0, 0, getWidth(), getHeight()));
                x = (getWidth() / 2) - 100;
                y = (getHeight() / 2) - 100;
                Rectangle inner = new Rectangle(x, y, 200, 200);
                outter.subtract(new Area(inner));

                g2d.setColor(new Color(0, 0, 0, 192));
                g2d.fill(outter);
            }
            g2d.dispose();
        }

    }

}

I should also mention that you can use any other Shape you want...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366