0

I cannot find any info on how to draw shapes on a graphic canvas, making sure what I draw is not the same color as the background

there are some solutions out there but they all use images drawing only with per pixel operations/loops or filters; I also tried different Composite operations, but none suit what I want

so lets say I do

g.setColor(color.white) // relevant in this case ? not sure
g.fillRect(...)

I want the rectangle to be in inverted colors of the background so it is always visible

sorry I cant provide more code, I really dont know how to achieve this

thanks

Phil
  • 708
  • 1
  • 11
  • 22
  • 1
    Just as a side note you should not use Canvas in a swing application. Canvas is part of the AWT which is a heavyweight toolkit Swing is a lightweight toolkit and the two usually should not be mixed. I would strongly recommend replacing your Canvas with a JPanel. – X_Wera Nov 10 '16 at 11:39
  • "I cannot find any info on how to draw shapes on a graphic canvas, making sure what I draw is not the same color as the background": oh yeah?? – gpasch Nov 14 '16 at 21:25

1 Answers1

4

Your paint method could retrieve the current color, and search for its complementary color:

        Color originalColor = g.getColor();

        g.setColor(complementaryColor(originalColor));
        g.fillRect(0, 0, 50, 50);

The complementaryColor method is inspired from this topic : Reverse opposing colors

Color complementaryColor(final Color bgColor) {

    Color complement = new Color(255 - bgColor.getRed(),
            255 - bgColor.getGreen(),
            255 - bgColor.getBlue());

    return complement;
}
Community
  • 1
  • 1
Arnaud
  • 17,229
  • 3
  • 31
  • 44