Drawing mode refers to how to determine the color of the overlapping part when the figures drawn later overlap with the figures drawn earlier. For example, the later drawing overwrites the earlier drawing; or the later drawing is mixed with the earlier drawing two colors according to some rule. There are mainly two types: normal mode and exclusive OR mode: normal mode is the graphics drawn later overlay on the graphics drawn earlier, so that the overlapping parts of the graphics that were previously sold are no longer visible. XOR mode treats drawing as shading by graphic. When drawing in XOR mode, a specific operation is used to obtain the actual drawing color that is currently being drawn, the original drawing color, and the color set in XOR mode. The methods to set the drawing mode are:
setPaintMode(): Set drawing mode to overlay mode (normal mode). Normal mode is the default mode for drawing.
setXORMode(Color c): Set the drawing mode to XOR mode and the parameter c is the drawing color set by XOR mode.
Suppose the background color is B, the color set with setXORMode() is C, and a non-background color D is also drawn. The XOR mode has the following rules to determine the actual drawing color:
If a region has been colored with D and then colored with E, the result is:
XOR drawing mode example
import javax.swing.*;import java.awt.*;public class Example7_4 extends JFrame{ public static void main(String args[]){ GraphicsDemo myGraph icsFrame = 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.setXOR Mode(Color.red); //Set XOR drawing Mode, color is red g.setColor(Color.green); g.fillRect(20, 20, 80, 40); //The actual color is green + yellow mixed color = gray g.setColor(Color.yellow); g .fillRect(60, 20, 80, 40); //The latter half is yellow+yellow=read, the first half is yellow+g.setColor(Color.green); g.fillRect(20, 70, 80, 40); //The actual color is the mixed color of green+yellow = gray. g.fillRect(60, 70, 80, 40); //The first half is (green+yellow)+gray = background color, the second half is green+yellow = gray g.setColor(Color.green); g.drawLine(80, 100, 180, 200); //The straight line is green+yellow = gray g.drawLine(100, 100, 200, 200); //Similar to above/ *Then draw partially overlapping straight lines. The middle section of the original straight line is gray + gray = background color, and the extended part is green + yellow = gray.*/ g.drawLine(140, 140, 220, 220); g.setColor(Color. yellow); //Analyze the following line color changes, overlap with the previous force g.drawLine(20, 30, 160, 30); g.drawLine(20, 75, 160, 75); }}class GraphicsDemod extends JFrame{ public GraphicsDemo(){ this.getContentPane().add(new ShapesPanel()); setTile("Basic Drawing Method Demonstration"); setSize(300, 300); setVisible(true); }}