0

I am trying to use the fillRect method in Java to fill a rectangle with an image. Is there a way to do it using the fillRect method?

Here is the code I was working on:

    private int x = 0;
    private int y = 0;
    static int WIDTH = 1; 
    static int HEIGHT = 1; 
    private Image image = new ImageIcon("Golf_green.png").getImage();

    public BackGround() {
        WIDTH = 30;
        HEIGHT = 30;
    }

    public void paint(Graphics2D g) {
        g.drawImage(image, x, y, WIDTH, HEIGHT, "BLACK", , arg7, arg8, );
    }
DonaEb
  • 1
  • 1
  • 1
    The "add(new ImageViewer...)" within the paint method is a bad idea to start with. What framework? AWT/Swing/GWT/SWT/JavaFX? – MadProgrammer Dec 26 '14 at 08:45
  • I think you need to consult the JavaDocs and tutorials, as your drawImage call looks..."wrong" – MadProgrammer Dec 26 '14 at 08:47
  • You could use "BufferedImage#subImage" to "crop" a portion of the image and paint that – MadProgrammer Dec 26 '14 at 08:49
  • That rot above would not compile, let alone run.. 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get images for an example, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Dec 26 '14 at 08:59

1 Answers1

2

(fill rect with tiled image) is there a way to do it using the fillrect method?

No.

Instead, tile the image across the width and height of the viewing area. To get the effect that it is only for a particular rectangle, set that rectangle as the clip (prior to tiling).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433