title: Fill rectangle with image in Java
body: <p>How can I fill the following rectangle using an image? Can anyone help me please?</p>
  
  <pre><code>public void paintComponent(Graphics g) {
           setOpaque(false);  
          //Paint a filled rectangle at user's chosen point.
          if (point != null) {
              g.drawRect(0, 0,
                         rectWidth - 1, rectHeight - 1);
              g.setColor(Color.yellow);
              g.fillRect(1, 1,
                         rectWidth - 2, rectHeight - 2);
              }}
  </code></pre>
  
  <p>I tried this code but I couldn't find a way to make it work:</p>
  
  <pre><code>File imageFile = new File("duck.jpg");
  BufferedImage img;
  Graphics2D graph = img.createGraphics();
  graph.setColor(Color.BLACK);
  graph.fill(new Rectangle(1, 2, rectWidth, rectHeight));
  graph.dispose();
  ImageIO.write(img, "jpg", new File("duck.jpg"));
  </code></pre>