We help IT Professionals succeed at work.

Using TexturePaint with Images in Java

ironwill96 asked
Last Modified: 2012-08-13
I have tried looking at the SUN Tutorials on using TexturePaint and loading images into a BufferedImage object and then using that to form the TexturePaint object.  None of this has worked for me, and its not really very intuitive.  One would think that you could do:  TexturePaint one = new TexturePaint("filepath.jpg"); and that Java could handle it.  Shown below is my test code that isn't working right now.

       Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image img = toolkit.getImage("sandtexture.jpg");
        BufferedImage sandTexture = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
        Graphics2D writeImage = sandTexture.createGraphics();
        writeImage.drawImage(img, 0, 0, this);
        Rectangle r = new Rectangle(0,0, 20, 20);
        TexturePaint sand = new TexturePaint(sandTexture, r);

g2 is a Graphics 2D Object.
sandArea is a GeneralPath polygon object.
This code is in my public void paint(Graphics g) method.
This code is in a class that extends JFrame, not JApplet - which is why I could not use the built-in getImage methods of JApplet.

Any suggestions on why this produces a "black screen" where the texture should be would be appreciated.  The only thing I can figure is that the problem might be with the off-screen buffer size of my image (20, 20).  The image itself has dimensions much higher, do I need to do a scale transform before loading it?  Also, the writeImage.drawImage() method has parameters that don't seem to make sense.  The last parameter (I have set to this) is for a Transform operation I believe.  I set it to this because a website with sample code did it that way.

Watch Question


I found the answer to this question can close this.  For the benefit of others, here is how you do it:

Image img = Toolkit.getDefaultToolkit().getImage("picture.gif");
try {
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(img, 0);
} catch (Exception e) {}
int width = img.getWidth(this);
int height = img.getHeight(this);
BufferedImage bi = new BufferedImage(width, height,
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(img, 0, 0, null);

If a moderator would prefer that I delete this question, I can do that too.  Just let me know.
Top Expert 2016

If you post a 0 pointer to Community Support, they'll delete it for you and give you your points back
This one is on us!
(Get your first solution completely free - no credit card required)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.