Alternating AWT graphics colour

26 Jun 2013 . posts . Comments
#AWT #Java #UI

It is possible to draw 2D colours in such a way that it inverts colours behind it, like in the screenshot below:

line colour

This can be done by enabling “XOR Colour mode” and by using…

Color.WHITE
```...this is because white has an RGB value of 255,255,255, and XOR’ing these values will cause the alternating effect.

The following code example shows this:

graphics.setXORMode(Color.WHITE); graphics.drawLine(x, delegate.getXAxisHeight(), x, delegate.getDiagramRect().height + delegate.getXAxisHeight()); graphics.setColor(Color.BLACK); ```Don’t forget to save the previous colour (using graphics.getColor())and then reset it after drawing this alternative-coloured line.


Me

Anthony is an Australian software engineer and mathematician. As a UTS MBT graduate, Anthony is the technology co-founder of a property and cycling-tech startup and enjoys teaching and learning coding with the Australian startup scene.