Using AWT, how do you import an image into a rectangle you have drawn? I want to assign the background to that particular rectangle only.
Asked
Active
Viewed 2,408 times
2
-
I recommend swing instead of awt. Then there is a good tutorial [here](http://docs.oracle.com/javase/tutorial/ui/overview/demo.html). regards – pyr0 Nov 19 '12 at 14:44
2 Answers
5
- Set the
Rectangle
as the clipping region. - Draw the image at a point so (part of) it is inside the clipping region.
- Clear the clip.
- Draw the rectangle.
As seen in this answer (the shape here is of text, but same principle applies).

Community
- 1
- 1

Andrew Thompson
- 168,117
- 40
- 217
- 433
0
The most common approach is to override the the paint
method in your component. Have a look at Graphics.drawImage(). Example: Drawing an Image
If you decide to move to Swing
, you can take advantage of Swing's optimizied painting model, overriding paintComponent
instead of paint
.

Reimeus
- 158,255
- 15
- 216
- 276