A brief analysis of the use of drawing patterns in Java

  • 2020-04-01 04:15:21
  • OfStack

Drawing mode refers to how to determine the color of the overlapping part when the drawing pattern overlaps with the drawing pattern. For example, post-drawn overrides previously drawn; Or the post-draw is mixed with the previously drawn two colors according to some rule. There are mainly two kinds of normal mode and xor mode: the normal mode is that the graph drawn after the overlay on the graph drawn earlier, so that the overlap of the previous graph is no longer visible. Xor mode treats drawing as coloring by graph. When drawing in xor mode, the color to be drawn, the color to be drawn and the color to be set in xor mode are calculated to obtain the actual drawing color. The methods to set the drawing mode are:
SetPaintMode () : sets the drawing mode to override mode (normal mode). Normal mode is the default mode for drawing.
SetXORMode (Color c) : sets the drawing mode to xormode. Parameter c is the drawing Color set to xormode.

Set the background color as B, use setXORMode() to set the color as C, and use a certain non-background color D drawing, XOR mode has the following rules to determine the actual drawing color:

B plus B is equal to C. I'm going to draw it in the background. D + D = B, when a graph is redrawn, the original graph is cleared. B + D = the mixture 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 = the mixture of D and E (when D and E are different).

XOR drawing pattern instance


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 to red
    g.setColor(Color.green);
    g.fillRect(20, 20, 80, 40); //The actual color is a blend of green + yellow = gray
    g.setColor(Color.yellow);
    g.fillRect(60, 20, 80, 40); //The second half is yellow+yellow=read, and the first half is yellow+ grey
    g.setColor(Color.green);
    g.fillRect(20, 70, 80, 40); //The actual color is a blend of green+yellow = gray.
    g.fillRect(60, 70, 80, 40);
    //The first half is (green+yellow)+gray = background, and the second half is green+yellow = gray
    g.setColor(Color.green);
    g.drawLine(80, 100, 180, 200); //This line is green+yellow = gray
    g.drawLine(100, 100, 200, 200); //Same as above
    
    g.drawLine(140, 140, 220, 220);
    g.setColor(Color.yellow); //Analyze the following line color changes that overlap with previous 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);
  }
}


Related articles: