import java.awt.*; public class MyCanvas extends Canvas { public void paint(Graphics g) { Rectangle clipBounds = g.getClipBounds(); g.setColor(Color.RED); g.drawRect( clipBounds.x + 10, clipBounds.y + 10, clipBounds.width - 20, clipBounds.height - 20); } }
import java.awt.*; import java.awt.image.BufferedImage; public class MyCanvas extends Canvas { public void paint(Graphics g) { Rectangle clipBounds = g.getClipBounds(); BufferedImage image = loadImage("myImage.jpg"); g.drawImage(image, clipBounds.x, clipBounds.y, clipBounds.width, clipBounds.height, null); } }In this example, we load an image and draw it within the current clipping area, which is determined by the getClipBounds() method. This limits the drawing area of the image to the bounds of the clip area. Package library: This example uses the java.awt.image package.