Drawing mode refers to the case where the drawing drawn later overlaps with the drawing drawn earlier.How to determine the color of overlapping parts.For example, a later draw overrides an earlier draw;Or the two colors drawn later and mixed earlier are mixed according to some rules.There are mainly two modes:normal mode and XOR mode. Normal mode is that the graphics drawn later are overlaid on the graphics drawn earlier.Makes the overlapping parts of the previous traffic graphics no longer visible.XOR mode treats drawing as coloring by graphics.When drawing in XOR mode,Perform a specific operation on the color currently being drawn, the color originally drawn, and the color set by the XOR mode.Get the actual drawing color.The methods for setting the drawing mode are:

setpaintmode ():Sets the paint mode to overlay mode (normal mode). Normal mode is the default mode for drawing.

setxormode (color c):set the drawing mode to XOR mode,Parameter c is the drawing color set by XOR mode.

Set the background color to b, use setxormode () to set the color to c, and draw with a non-background color d. The xor mode has the following rules for determining the actual drawing color:

b + b=c, draw with background color,C color appears. d + d=b, when a graphic is redrawn, the original drawing can be cleared. b + d=mixed color of b and d (when b and d are different).

If an area has been colored with d and then with e, the result is:

d + e=mixed color of d and e (when d and e are different).

xor drawing mode example

import javax.swing. *;
import java.awt. *;
public class example7_4 extends jframe {
  public static void main (string args []) {
    graphicsdemo mygraphicsframe=new graphicsdemo ();
class shapespanel extends jpanel {
  sharpespanel () {
    setbackground (color.white);
  public void paintcomponent (graphics g) {
    super.paintcomponent (g);
    setbackground (color.yellow);//The background color is yellow
    g.setxormode (color.red);//Set xor drawing mode,Color is red
    g.setcolor (color.green);
    g.fillrect (20, 20, 80, 40);//The actual color is a mixture of green + yellow=gray
    g.setcolor (color.yellow);
    g.fillrect (60, 20, 80, 40);//The second half is yellow + yellow=read, the first half is yellow + gray
    g.setcolor (color.green);
    g.fillrect (20, 70, 80, 40);//The actual color is green + yellow mixed color=gray.
    g.fillrect (60, 70, 80, 40);
    //The first half is (green + yellow) + gray=background color, and the second half is green + yellow=gray
    g.setcolor (color.green);
    g.drawline (80, 100, 180, 200);//The line is green + yellow=gray
    g.drawline (100, 100, 200, 200);//same as above
    /* Draw some overlapping lines again.
The middle part of the original straight line is gray + gray=background color, and the extension is green + yellow=gray. * /
    g.drawline (140, 140, 220, 220);
    g.setcolor (color.yellow);//Analyze the following line color changes,Overlaps with earlier forces
    g.drawline (20, 30, 160, 30);
    g.drawline (20, 75, 160, 75);
class graphicsdemod extends jframe {
  public graphicsdemo () {
    this.getcontentpane (). add (new shapespanel ());
    settile ("Demonstration of basic drawing methods");
    setsize (300, 300);
    setvisible (true);
  • Previous Detailed method of setting font and color using Java
  • Next jQuery + Ajax + PHP "Like" rating function implementation code