Analysis of the use of Java drawing mode _java

Source: Internet
Author: User

Drawing mode is how to determine the color of overlapping parts when a later drawing overlaps with a previously drawn shape. For example, the later drawn overlay was previously drawn, or the two colors that were drawn earlier are mixed with some rule. There are two main types: normal mode and different or pattern, which is that the pattern that is drawn after is covered by the previously drawn shape, so that the overlapping portions of the previously sold graphics are no longer visible. XOR mode considers a drawing to be shaded by a graphic. When drawing in an XOR pattern, you can get the actual drawing color by making a specific calculation of the color currently being drawn, the color that was originally painted, and the color set by the XOR or pattern. The methods for setting drawing patterns are:
Setpaintmode (): Sets the drawing mode to overwrite mode (normal mode). Normal mode is the default mode for drawings.
Setxormode (color C): Sets the drawing mode to be different or mode, and parameter C is a different or pattern-setting drawing color.

Set the background color to B, set the color to C with Setxormode (), and use a non background color d drawing, and the XOR mode has the following rules for determining the actual drawing color:

    • B + b = c, with the background color drawing, the C color appears.
    • D + d = B, when a graphic is repaint to clear the original drawing.
    • B + d = mix color of B and D (when B,d is different).

If a range is colored with D and then colored with E, the result is:

    • D + E = d and e blend colors (when d,e is different).

XOR Drawing Mode Instance

Import javax.swing.*;
Import java.awt.*; public class Example7_4 extends jframe{public static void Main (String args[]) {Graphicsdemo mygraphicsframe = new G
  Raphicsdemo ();
  } 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);
    Sets the XOR drawing mode, the 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+ Gray G.setcolor (Color.green); G.fillrect (20, 70, 80, 40);
    The actual color is the mixed color of the Green+yellow = Gray.
    G.fillrect (60, 70, 80, 40);
    The first half is (green+yellow) +gray = background color, the latter 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); Ditto/* To draw partially overlapping lines. The middle segment of the original line is Gray + Gray = background color, and the extension part is green+yellow=gray.*/g. DrawLine (140, 140, 220, 220); G.setcolor (Color.yellow);
    Analyze the following line color changes, overlapping with previous forces G.drawline (20, 30, 160, 30);
  G.drawline (20, 75, 160, 75);
    The class Graphicsdemod extends jframe{public Graphicsdemo () {This.getcontentpane (). Add (New Shapespanel ());
    SetTile ("Basic Drawing Method Demo");
    SetSize (300, 300);
  SetVisible (TRUE); }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.