Java AWT Tutorials

adplus-dvertising
Working With Paint Mode Showing Pointer
Previous Home Next

In this example we are going to set paint mode. Paint mode determines how object are drawn in a window. By default ,new output to a window overwrites any pre existing context. However it is possible have a new object XORed onto the window by using set XORed() mode.

To control the Choice Box we are using three type of package. One is java.awt.* second is java.applet.* and third is java.awt.event.*; .The java.awt.* is used for GUI interface. AWT classes are contain in this package. The java.applet.* package is used to create an applet and java.awt.event.*; defined within the java.awt.*; package is a subclass of EventObject. It is a super class of all AWT-based event.

void setXORedMode(Color xorColor): xorColor specifies the color that will be XORed to the window when an object will be drawn.

To return on overwrite mode call: void setPaintMode():

In our example, the following program display the cross hairs that track the mouse pointer. The cross hairs are XORed onto the Window and are always visible no matter what the underlying color is.

Example

//Working with paint mode
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="XOR" width=300 height=200>
</applet>
*/
public class XOR extends Applet{
int chsX=100, chsY=100;
public XOR(){
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent me){
int x=me.getX();
int y=me.getY();
chsX=x-20;
chsY=y-20;
repaint();
}});}
public void paint(Graphics g){
g.setColor(Color.green);
g.drawLine(0,0,200,200);
g.setColor(Color.black);
g.fillRect(20,20,70,70);
g.setColor(Color.red);
g.drawLine(10,100,50,20);
g.setColor(Color.green);
g.drawRect(50,0,50,50);
g.setXORMode(Color.black);
g.drawLine(chsX-10,chsY,chsX+10,chsY);
g.drawLine(chsX,chsY-10,chsX,chsY+10);
g.setPaintMode();
}
}

Output Of Example

Download Source Code

Previous Home Next