• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to paste and move an image in java JImagePanel?

 
Greenhorn
Posts: 8
Netbeans IDE Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I have a Java program to manipulate image. But I am unable to copy a certain area of ​​JImagePanel to later move it . So can anyone give me a hint how to do this ? Basically , I just need to copy the selected area of JImagePanel and paste on it JImagePanel .

Sorry for my poor English. Thanks in advance.

just below the code that selects the area of JImagePanel image...





 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't know, but I suggest you try this section of the Java™ Tutorials. What is a JImagePanel? It appears not to be a standard Java® class.

I shall move this discussion to our GUIs forum.
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Luso,

well, basically it is easier than you might think.

In your mouseListener of your 'img_painel' you have the
coordinates of both where the mouse is pressed and where the mouse is released.
Now, with these two points at hand it is quite simple to create a new bufferedImage
(look at the BufferedImage API, you will find a very suitable method!).

Then, you could add a method 'setImage' to your 'JImagePanel' class, and set that
newly created image. Do a repaint et voilá.

Two remarks:

1) it is not necessary to create a new 'JImagePanel' each time you have a new image!

2) I can't check it out at the moment how you scale your images so as to fit them in your JImagePanel,
but if you scale them, your mouseListener will give you the wrong coordinates: you should scale
your mouse-coordinates so that they represent the coordinates of the original image.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the Screen Image class to create an image of any Swing component given the Rectangle you want to use.

Basically , I just need to copy the selected area of JImagePanel and paste on it JImagePanel



If you want to replace the current image on the panel with this new image, then your JImagePanel class will need a method like setImage(Image). Then once you create the Image as suggested above you invoke this method on your panel and repaint the panel.
 
Luso Moreira
Greenhorn
Posts: 8
Netbeans IDE Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, but I still can't solve .

I implemented a method for crop the area will to be copied. In this case I used coordinates of the mousePressed and mouseRelease.

Now how I can set this area in new object for drag on JImagePanel without to remove the initial image ?

take a look in code, please!


 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Luso,

good heavens, what a complicated code you have there!

Especially the parts where you create an Image by deriving
it from the original image. Is there any reason why you do it
the way you do?

In the meanwhile, I would like to make some suggestions. First,
use BufferedImages in stead of Images and ImageIcons.
So, instead of 'imagem = new ImageIcon(path).getImage();"
simply use "imagen = ImageIO.read(path)".

That has some major advantages. It simplifies your code, but most
important: it enables you to simply get a subimage by using:

Having this subimage, drawing both is now straightforward.
In your 'paintComponent' method of "JImagePane;', you first
draw imagem, and next you draw 'subImagem'.
The location where you draw this subimage is what you need to
get. You can use a MouseMotionListener on your panel to follow
the mouse.

Lastly: in

from where do you get the variables x, y, w and h?

Well, if you look at the paintComponent method of your panel,
you see that your imagem is always scaled to the full size of the panel.
Now, in your mouseListener or mouseMotionListener, you get
the mouse coordinates, relative to upper left corner of the panel.

Now, suppose the panel has dimensions Pw and Ph, and that your
image has dimension Iw and Ih, can you figure out what image coordinates
belong to the mouse coordinates Mx and My?

And when you have two of these mouse points (see mousePressed and
mouseReleased) can you determine the parameters for the 'SubImage'
method?
 
Luso Moreira
Greenhorn
Posts: 8
Netbeans IDE Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone!

I have had a little progress in my code . But still lack some important points .


1. The sub image (img_panelSecondary) is not working properly . The image should fill the entire area , and the panel should have the same dimension of the image.

2. The sub image (img_panelSecondary) should stay on the (img_panelPrincipal);

3. The mouseDragged is not working properly, but i think it's because of the wrong dimension of (img_panelSecondary);


Piet Souris, I had to use this methods of my code, because is a requirement of teacher.

thanks in advance.




 
Luso Moreira
Greenhorn
Posts: 8
Netbeans IDE Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for help me to solve this issue of my code;

Here is my last code to help all those who will need someday!

 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Luso,

You seem to have solved it completely, so well done!

Sorry I didn't reply, but I was working on your program yesterday evening.
I was facing three problems:
1) the offset of the crop was wrong
2) dragging made the crop always appear behind the original, in stead of in front
3) loading another image lead to havoc

I tried a couple of things (well, many things), but I couldn't get it to work properly.

Couple of weeks ago I was working on a more or less similar program.
It was something we had to write for some iOS programming course.
If you're interested in it, let me know.

Greetings,
Piet
 
Luso Moreira
Greenhorn
Posts: 8
Netbeans IDE Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Luso,

You seem to have solved it completely, so well done!

Sorry I didn't reply, but I was working on your program yesterday evening.
I was facing three problems:
1) the offset of the crop was wrong
2) dragging made the crop always appear behind the original, in stead of in front
3) loading another image lead to havoc

I tried a couple of things (well, many things), but I couldn't get it to work properly.

Couple of weeks ago I was working on a more or less similar program.
It was something we had to write for some iOS programming course.
If you're interested in it, let me know.

Greetings,
Piet



Hi Piet,

I finished my code, but I know it's was not better way to make this. (In Brazil we can call that of "gambiarra" )

On my vacations of university (in two weeks ) I'll try to work in this code. And I hope this time it works correctly.
Well, I'm very interested in your proposal.

Thank very much, dude.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic