Demonstrate XOR mode. : Graphics « 2D Graphics « Java Tutorial






Demonstrate XOR mode.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;

public class XORModePaintWithMouse extends JFrame {
  int chsX = 100, chsY = 100;

  public static void main(String[] a) {
    XORModePaintWithMouse f = new XORModePaintWithMouse();
    f.setSize(300, 300);
    f.setVisible(true);
  }

  XORModePaintWithMouse() {
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent me) {
        int x = me.getX();
        int y = me.getY();
        chsX = x - 10;
        chsY = y - 10;
        repaint();
      }
    });
  }
  public void paint(Graphics g) {
    g.drawLine(0, 0, 100, 100);
    g.drawLine(0, 100, 100, 0);
    g.setColor(Color.blue);
    g.drawLine(40, 25, 250, 180);
    g.drawLine(75, 90, 400, 400);
    g.setColor(Color.green);


    // xor cross hairs
    g.setXORMode(Color.black);
    g.drawLine(chsX - 10, chsY, chsX + 10, chsY);
    g.drawLine(chsX, chsY - 10, chsX, chsY + 10);
    g.setPaintMode();
  }
}








16.2.Graphics
16.2.1.Draw rectangles
16.2.2.Fill a solid three-dimensional rectangle
16.2.3.Draw Ellipses
16.2.4.Draw 2D shapeDraw 2D shape
16.2.5.Draw Arcs
16.2.6.Draw PolygonDraw Polygon
16.2.7.Resizing output to fit the current size of a window.
16.2.8.Demonstrate XOR mode.Demonstrate XOR mode.