2

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
newbie87
  • 25
  • 1
  • 5
  • 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 Answers2

5
  1. Set the Rectangle as the clipping region.
  2. Draw the image at a point so (part of) it is inside the clipping region.
  3. Clear the clip.
  4. 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.

See: Performing Custom Painting

Reimeus
  • 158,255
  • 15
  • 216
  • 276