Hello, I'm trying to fill a shape using TexturePaint in java. However when i'm running the codes i'm having the following errors: Exception in thread "main" java.lang.Error: Unresolved compilation problems: ImageIO cannot be resolved IOException cannot be resolved to a type at Textures.

import java.awt.Graphics;
        import java.awt.Graphics2D;
        import java.awt.Rectangle;
        import java.awt.TexturePaint;
        import java.awt.image.BufferedImage;

        import java.io.IOException;

        import javax.imageio.ImageIO;

        import javax.swing.JFrame;
        import javax.swing.JPanel;

        public class Test1 extends JPanel {

            BufferedImage img;
            TexturePaint imgTp;

            public Test1() {

                try {
                    slate = ImageIO.read(this.getClass().getResource("greycar.jpg"));
                } catch (IOException e) {

            public void paintComponent(Graphics g) {

                    Graphics2D g2d = (Graphics2D) g;

                    imgTp = new TexturePaint(slate, new Rectangle(0, 0, 90, 60));

                    g2d.fillRect(25, 30, 80, 60);


            public static void main(String[] args) {
                Test1 t=new Test1();

                JFrame frame = new JFrame("Texture paint");

                frame.setSize(360, 120);

Recommended Answers

All 2 Replies

This error is may be due to that you have not declared the "slate" variable in your code.

As adikimicky pointed out, you're missing a declaration of your variable:

BufferedImage img;

presumably should be

BufferedImage img, slate;

since slate is undefined in your class, this error should appear:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    slate cannot be resolved to a variable
    slate cannot be resolved to a variable
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.