Home > Java > javaTutorial > body text

A brief analysis of the use of Java's drawing mode

高洛峰
Release: 2017-01-17 16:00:10
Original
1391 people have browsed it

Drawing mode refers to how to determine the color of the overlapping part when the graphic drawn later overlaps the graphic drawn earlier. For example, the color drawn later overwrites the color drawn earlier; or the two colors drawn later and those drawn earlier are mixed according to certain rules. There are two main modes: normal mode and XOR mode: Normal mode is that the graphics drawn later are overlaid on the graphics drawn earlier, so that the overlapping parts of the earlier graphics are no longer visible. XOR mode treats drawing as coloring by shape. When drawing in the XOR mode, specific operations are performed on the color currently being drawn, the color originally drawn, and the color set in the XOR mode to obtain the actual drawing color. The methods to set the drawing mode are:
setPaintMode(): Set the 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 parameter c is the drawing color set in XOR mode.

Suppose the background color is B, the color set with setXORMode() is C, and a non-background color D is used for drawing. 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 originally drawn graphic can be cleared.

B + D = Mixed color of B and D (when B and D are not the same).

If an area has been colored with D and then colored with E, the result is:

D + E = the mixed color of D and E (when D and E are not the same).

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); //背景色为黄色
    g.setXORMode(Color.red); //设置XOR绘图模式,颜色为红色
    g.setColor(Color.green);
    g.fillRect(20, 20, 80, 40); //实际颜色是green + yellow的混合色=灰色
    g.setColor(Color.yellow);
    g.fillRect(60, 20, 80, 40); //后一半是yellow+yellow=read,前一半是yellow+灰色
    g.setColor(Color.green);
    g.fillRect(20, 70, 80, 40); //实际颜色是green+yellow的混合色=灰色.
    g.fillRect(60, 70, 80, 40);
    //前一半是(green+yellow)+gray =背景色,后一半是green+yellow = gray
    g.setColor(Color.green);
    g.drawLine(80, 100, 180, 200); //该直线是green+yellow = gray
    g.drawLine(100, 100, 200, 200); //同上
    /*再绘制部分重叠的直线.原直线中间段是灰色+灰色=背景色,延长部分是green+yellow=gray.*/
    g.drawLine(140, 140, 220, 220);
    g.setColor(Color.yellow); //分析下列直线颜色变化,与早先的力有重叠
    g.drawLine(20, 30, 160, 30);
    g.drawLine(20, 75, 160, 75);
  }
}
class GraphicsDemod extends JFrame{
  public GraphicsDemo(){
    this.getContentPane().add(new ShapesPanel());
    setTile("基本绘图方法演示");
    setSize(300, 300);
    setVisible(true);
  }
}
Copy after login

For more related articles on the use of Java's drawing mode, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!