Write a method to clip an image to a triangle or star shape.

What will be an ideal response?

```
public Picture clipToTriangle ( )
{
int width = this . getWidth ( ) ;
int height = this . getHeight ( ) ;
Picture result = new Picture (width , height ) ;
// get the graphics2D object for this picture
Graphics g = result.getGraphics ( ) ;
Graphics2D g2 = (Graphics2D) g ;
// create a path to use for clipping
GeneralPath path =
new GeneralPath (new Line2D . Double ( 0 , height ,
width / 2 , 0 ) ) ;
path . append (new Line2D . Double ( width / 2 , 0 ,
width , height ) , true ) ;
path . append (new Line2D . Double (width , height ,
0 , height ) , true ) ;
// use the path for clipping
g2 . setClip ( path ) ;
// draw the image
g2 . drawImage ( this . getImage ( ) , 0 , 0 , width ,
height , null ) ;
// return the result
return result ;
}
```

Computer Science & Information Technology

You might also like to view...

An SQL statement is an instruction using Structured Query Language

Indicate whether the statement is true or false

Computer Science & Information Technology

To help reduce the size of your PowerPoint presentation, you can ________ a video file

A) compress B) expand C) hide D) crop

Computer Science & Information Technology