-1

I have a PNG image and I want to use it as a brush.

I want to paint something to JPanel.

How to load an image to use as a brush for JPanel?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KidLet
  • 173
  • 1
  • 2
  • 5
  • 3
    Welcome to SO! Read [whathaveyoutried.com](http://whathaveyoutried.com), the [FAQs](http://stackoverflow.com/faq) and the [stackoverflow question checklist](http://tinyurl.com/so-list), rephrase your question, showing us what you have tried and describe the problems you are facing. I am sure we'll help you out, than. – jlordo Jan 03 '13 at 00:56
  • 1
    `TexturePaint` might be a useful search term. – trashgod Jan 03 '13 at 00:59
  • 2
    1) This breaks down to two questions a) "How to load an image?" b) "How to draw an image to a panel?" 2) The answer to (a) can generally be found in `ImageIO.read(..)` 3) Do you want to use the image as the BG for the panel, or simply show it in a panel? If 'show' - use a `JLabel(ImageIcon(Image))`. – Andrew Thompson Jan 03 '13 at 01:00
  • http://stackoverflow.com/questions/4160904/show-png-image-in-a-jframe – Mel Nicholson Jan 03 '13 at 01:02
  • As an aside, what DYM by 'brush'? Is this a 'paint application'? – Andrew Thompson Jan 03 '13 at 01:06
  • Please have a look at this [example](http://stackoverflow.com/a/11372350/1057230), for a simple understanding of the topic :-) – nIcE cOw Jan 03 '13 at 03:45

1 Answers1

0
final Image image = new ImageIcon("C:/images/image.png").getImage();
JPanel panel = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(imageToPaint, 
            (desired x location), (desired y location), null);
    }
};
Name
  • 2,037
  • 3
  • 19
  • 28
  • 3
    -1 do not override `paint` and do not reinvent the wheel. – user1329572 Jan 03 '13 at 02:23
  • @user1329572 Besides, he said he wants to use the image as "brush". This implies he would like to use a canvas to draw graphics, and using *paint* is the most... canvasy way to display an image. – Name Jan 03 '13 at 03:13
  • 3
    No no no @JesusPlusPlus, paint method is not used for painting such a thingy. Always override `paintComponent()` for such a task. `paint()` is used only when you need to change the state of the `Graphics` object, so that, along with the contents of the said component, it's border as well as children can reciprocate with that changed state of the `Graphics` object. – nIcE cOw Jan 03 '13 at 03:42
  • 1
    @GagandeepBali Always good to learn something new, I changed my answer. Thanks for the response. – Name Jan 03 '13 at 14:48
  • 1
    @JesusPlusPlus : Happy to know that, KEEP SMILING :-) . I had negated my downvote, though since my daily limit is gone, I am not sure, if did worked or not, though I will again try tomorrow, when the vote meter will be refilled by stackoverflow again :-). Now the answer is providing valuable information with regard to the issue. Just make sure, that while overriding a method, keep the **Access Specifier** same, as much as possible, like it's `protected void paintComponent(Graphics g)` :-) – nIcE cOw Jan 03 '13 at 14:55