-1

I have a graphics object and I want to color all the area except some rectangles. For e.g.

enter image description here

I want to color all area except these black areas. Can I do that? There can be many rectangles in the image.

alex2410
  • 10,904
  • 3
  • 25
  • 41
Vaibhav Agarwal
  • 1,124
  • 4
  • 16
  • 25
  • Are you also coloring the black rectangles? If you color the entire graphic before drawing them then it'd probably work the way you want. – Corey Ogburn Dec 19 '13 at 22:37
  • No, I want to color all the area `except` Black areas. – Vaibhav Agarwal Dec 19 '13 at 23:11
  • -1, @VaibhavAgarwal, you where given an answer in your last posting on this topic (http://stackoverflow.com/q/20671551/131872). Why don't you take the time to look at the answers given and accept an answer when people help you? The answer given here is almost identical to the answer given in your last question. – camickr Dec 20 '13 at 00:37

1 Answers1

2

I recommend you to fill all area with White color, and then draw Black rectangles on that, because it's simplier that draw figure with holes. For example like next:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawExample extends JPanel{
    List<Rectangle> rctangles = new ArrayList<>();

    public static void main(String[] args)  {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawExample drawExample = new DrawExample();
        drawExample.addRect(new Rectangle(20,20,25,25));
        drawExample.addRect(new Rectangle(50,50,25,25));
        frame.add(drawExample);
        frame.setSize(200,200);
        frame.setVisible(true);

    }

    private void addRect(Rectangle rectangle) {
        rctangles.add(rectangle);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        for(Rectangle r : rctangles){
            g.fillRect(r.x, r.y, r.width,r.height);
        }
    }

}

enter image description here

alex2410
  • 10,904
  • 3
  • 25
  • 41