Introduction
Clipping is a technique that restricts the drawing area to a small portion of the screen. Clipping regions are areas where you can allow drawing. They make it easier for you to indicate which parts of your illustration are important and how they interact with each other. Any drawing outside the clipping region is clipped off and not displayed. Animation is a technique that allows an object to be moved on the screen.
The Graphics class method clipRect() creates rectangular clipping regions. For example, g.clipReact(50,50,100,75) sets a clipping rectangle of width 100 and height 75 with a top left corner at 50,50. Once the clipping rectangle is set, any drawing done outside this rectangular region is clipped off. For example, the line resulting from g.drawline(200,150,25,25) will be completely clipped off. If a call to a graphics method results in a drawing that extends outside the clipping rectangle, only that part of the drawing that lies inside the clipping region is displayed.
clipRect() method
clipRect() method to set a clipping region and to illustrate the clipping technique by drawing outside the clipping region.
Source Code
/*<APPLET CODE="Clipper.class" WIDTH=300 HEIGHT=150>
</APPLET>
*/
import java.awt.*;
import java.applet.*;
public class Clipper extends Applet
{
public void paint(Graphics g)
{
g.clipRect(10,10,150,100);
g.setFont(new Font("TimesRoman", Font.ITALIC,28));
g.fillOval(100,60,80,80);
g.drawString("Ashish Bhatnagar",50,30);
}
}
Output
Compile Clipper.java using Javac and view it using the appletviewer.
In line no. 5, the clipRect method sets the clipping rectangle of the Graphics object g. In the next three lines, a string and a circle, both of which lie partially outside the clipping region, are drawn. The output of the program shows that only that part of the drawing which lies inside the clipping region is displayed.
To understand clipping better, try out the following
-
Comment the line that sets the clipping rectangle. On running the program, the string and circle are displayed in their entirety.
-
Use the drawRect() method to draw a rectangle around the clipping region. To do this, insert the statement g,drawRect(10,10,150,100); just before the clipping region is set.
Animation in AWT
An animation that can move an object on the screen is also a technique by which an object is moved. The object to be moved can be a simple drawing or a complex image loaded from an image file. This object, in animation jargon, is called a frame of animation.
Animation typically involves the following steps.
-
A portion of the screen is where the object is drawn.
-
The object’s location is changed, and the object at the old location is erased.
-
The object is repainted in its new location.
By repeating the above steps, an illusion of an object moving from one part of the screen to another can be created.
Note. A striking difference between a real-world object and an animated object is that when the animated object moves, the object at the old position has to be erased, or it leaves a trail of objects behind.
From the above discussion, it should be clear that animation typically involves a loop in which the objects get painted, erased, and again repainted; the loop starts when the animation begins and lasts till the object reaches its destination.
As long as the loop lasts, the animation consumes all the processor time allocated to the program. Since the animation part of the program monopolizes the processor time, everything else in the program comes to a standstill.
Hence during animation, the program would not be able to respond either to user events like a mouse click or to system events like paint. This, certainly, is not acceptable. What is required is a magic solution that would allow running the animation loop without affecting the rest of the program. Java provides a solution for the above problem by supporting threads.
Summary
Clipping is a technique by which the drawing area is restricted to a small region. The clipRect() method of the Graphics class sets a clipping rectangle. Animation is a technique by which an illusion of an object moving on the screen is created.