• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extracting part of an image using BufferedImage and drawImage?

 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying for a while now to try to extract part of an image in IntelliJ. It has to be specified by the values of x, y, width and height. But I'm honestly unsure how to go about this since whatever way I try, I always seem to get some kind of error. After hours spent on coding and research online, I think that I need to use BufferedImage as well as drawImage. However, I was not never taught in class how to use these two (or even how to go about such task...) and that's why I'm asking for help here. Hopefully, somebody will be able to help me with this one. If anything is unclear then please let me know and I'll try to explain in better.

Code provided:



What I'm thinking of using:



Also, the method has to pass the following tests:



Thanks in advance!
 
Carey Brown
Bartender
Posts: 2249
28
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you get the Picture and Pixel classes?
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Where did you get the Picture and Pixel classes?

Sorry but I'm not sure what you mean exactly. Would you like me to upload the whole file? Maybe that would make it easier?

And I welcome you all as well.
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can never remember anything about images. But there is quite a bit in the Java™ Tutorials: try here or here.

We tend to discuss such things in our GUIs forum, so I shall move you thither.
 
Carey Brown
Bartender
Posts: 2249
28
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshall Mathers wrote:
Carey Brown wrote:Where did you get the Picture and Pixel classes?

Sorry but I'm not sure what you mean exactly. Would you like me to upload the whole file? Maybe that would make it easier?

And I welcome you all as well.

In your code you use the Picture and Pixel classes. They are not part of the standard Java library. Are you importing a 3rd party library or did you write these classes your self?
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Marshall Mathers wrote:
Carey Brown wrote:Where did you get the Picture and Pixel classes?

Sorry but I'm not sure what you mean exactly. Would you like me to upload the whole file? Maybe that would make it easier?

And I welcome you all as well.

In your code you use the Picture and Pixel classes. They are not part of the standard Java library. Are you importing a 3rd party library or did you write these classes your self?


These are all the imports (they've been in the file which I got from the lecturer to do the task in):
import media.FileChooser;
import media.SimplePicture;
import java.awt.image.BufferedImage;

import media.FileChooser;
import media.Pixel;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;

import java.io.IOException;

import junit.framework.TestCase;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;

So I guess they are imported and have not been written.
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick googling on 'FileChooser.getMediaPath' led me to this webpage:

https://www2.cs.uic.edu/~i101/doc/

In the documentation, I see nothing about a method 'Picture.extract'
so I guess you have to make one yourself.
'drawImage' has nothing to do with that (that method belongs to
a Graphics object), but what you suggest is what should work.
Extract a BufferedImage from your Picture (see class
'SimplePicture' for more information), and if you look at the
Oracle API for a BufferedImage, you will see a method that
creates very easily an image of a part of your image.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:A quick googling on 'FileChooser.getMediaPath' led me to this webpage:

https://www2.cs.uic.edu/~i101/doc/

In the documentation, I see nothing about a method 'Picture.extract'
so I guess you have to make one yourself.
'drawImage' has nothing to do with that (that method belongs to
a Graphics object), but what you suggest is what should work.
Extract a BufferedImage from your Picture (see class
'SimplePicture' for more information), and if you look at the
Oracle API for a BufferedImage, you will see a method that
creates very easily an image of a part of your image.

Edit: oops! I seem to have pressed twice the 'Submit' button. Can this reply be
removed?

Thanks for the help it does explain some things a little bit but I still can't for some reason get the picture to actually display the extracted part. I got it once to actually display but that was the original picture and then after that I started tweaking and I can't even display the image anymore.

Here is the whole class if that helps:



So how would I implement the BufferedImage here now?

Because when I do that:

Then I just get a message that parameters have never been used.
 
Piet Souris
Rancher
Posts: 1634
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Let's first determine how many pixels your Picture 'pictObj' has.
Picture is a subclass of SimplePicture, so it has the methods 'getWidth()'
and 'getHeight()'. So:



Now, let's extract the BufferedImage inside it:



Now, suppose that we want to extract a part of buf, say, starting from
the left upper corner, with width half of the given width and height
half of the given height.



Now have a look at the BufferedImage API, at the Oracle site.
We see a method: getSubimage(int x, int y, int w, int h).

Can you now extract the left upper quart of 'pictObj'?

Now, having this left upper quart, as a separate BufferedImage,
you can draw that part in the 'paintComponent(Graphics g)' method
of the JPanel where you want to draw it. The method to use is indeed
'drawImage'.

This drawing can be done in more than one way, I guess the library
that you are using has some dedicated methods to draw images
(or Pictures, as they are called).
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Okay. Let's first determine how many pixels your Picture 'pictObj' has.
Picture is a subclass of SimplePicture, so it has the methods 'getWidth()'
and 'getHeight()'. So:



Now, let's extract the BufferedImage inside it:



Now, suppose that we want to extract a part of buf, say, starting from
the left upper corner, with width half of the given width and height
half of the given height.



Now have a look at the BufferedImage API, at the Oracle site.
We see a method: getSubimage(int x, int y, int w, int h).

Can you now extract the left upper quart of 'pictObj'?

Now, having this left upper quart, as a separate BufferedImage,
you can draw that part in the 'paintComponent(Graphics g)' method
of the JPanel where you want to draw it. The method to use is indeed
'drawImage'.

This drawing can be done in more than one way, I guess the library
that you are using has some dedicated methods to draw images
(or Pictures, as they are called).


Wow, thanks. I already prefer the way you explain things vs my lecturer. This does explain a lot.
However, I still wasn't able to achieve what I wanted using your instructions BUT it somehow got me to this:



The thing is, it only displays the original picture, instead of the original image and a cropped part of that image.
Hopefully, you can help me to identify the mistakes that have been made in this one to actually successfully crop the image. As well as to make sure that it passes the required tests.
Thanks.
 
Piet Souris
Rancher
Posts: 1634
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Marshall,

well, to be frank I can't see anything wrong with your method! As far as
I can tell it should work.

One reason for getting a croppedPicture that is equal to the original Picture
is when the parameters of your method


cover the complete original Picture. Another one could be that you somehow do
NOT print your croppedPicture, or that you print it below originalPicture.

So for now I would advise:

1) as the first thing of your above mentioned method, print out the values
of your parameters. Check if these are correct. Especially check that your
'endX' and 'endY' are not beyond the width and the height of your Picture.

2) can you show us how you call this method? I.e. I'd like to see how you
determine your parameters

3) can you show us how you draw your Pictures? Do you use a JPanel,
or maybe an AnimationPanel?

4) if all else fails, I would like to be able to get your code into my
IDE, to test things out. But that would involve me copying and pasting many
classes from this library. How did you obtain this library? I don't see
any download button or so.
 
Marshall Mathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Marshall,

well, to be frank I can't see anything wrong with your method! As far as
I can tell it should work.

One reason for getting a croppedPicture that is equal to the original Picture
is when the parameters of your method


cover the complete original Picture. Another one could be that you somehow do
NOT print your croppedPicture, or that you print it below originalPicture.

So for now I would advise:

1) as the first thing of your above mentioned method, print out the values
of your parameters. Check if these are correct. Especially check that your
'endX' and 'endY' are not beyond the width and the height of your Picture.

2) can you show us how you call this method? I.e. I'd like to see how you
determine your parameters

3) can you show us how you draw your Pictures? Do you use a JPanel,
or maybe an AnimationPanel?

4) if all else fails, I would like to be able to get your code into my
IDE, to test things out. But that would involve me copying and pasting many
classes from this library. How did you obtain this library? I don't see
any download button or so.


No worries anymore. I was just about to edit the last post to say that I somehow managed to solve the issue.
It does exactly what I wanted it to do- extracts part of an image as well as passes all the tests.
Thank you for the help and suggestions though.
 
Piet Souris
Rancher
Posts: 1634
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well done!

And you're welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!