• Post Reply Bookmark Topic Watch Topic
  • New Topic

Copy a graphics on jframe  RSS feed

 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening reader
I want to know know to copy a rectangle on my jframe and paste it in another location on the same jframe.
any idea??
Thank you very much
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nathalie,

could you be a little more specific?

Are you thinking of some 'Snippit' program, where you can draw a rectangle
on your desktop and save it as an image? Or is this rectangle some JComponent
like a JPanel, or an Icon or so, that you want to be placed somewhere else?

It helps if you explain what your intentions are, so that we can, much better, form
an idea of what it is that must be achieved.

Greetz,
Piet
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my rectangle is an instance of Rectangle2D.float which i have previously drawn on my frame.
i want to select a piece of my jframe, then check that inside this selected piece of frame it contains my rectangle. if yes i can copy it else i cannot copy it.
i hope i'm clear in my explanation :/
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.

Well, that may be an easy problem to crack, or not.

First of all, how many JPanels do you have in your contentPane?
If you only have just one covering JPanel in that contentPane, or if
you only have that contentPane, that will make it much easier to
detect attempts to draw some rectangle R.

So, my first question is: how does your JFrame, or better, its contentPane look?
My second question: if we're only dealing with one JPanel, do you know how
to detect rectangles that are drawn over it by the user? (think of MouseListeners
and MouseMotionListeners, and how you would determine the bounds of
the rectagle that is drawn).

If we have the bounds of the drawn rectangle, then it should be easy to
determine if your Rectangle2D is intersecting it. Such a 2D thing has special methods
for that.

Well, sofar from me.

Greetz,
Piet
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my jpanel covers my entire frame I have another jpanel which contain my buttons.

Yes I know when the rectangle is drawn. I know how to have its bounds.

My problem is that I should place my code in a JMenuItem named copy when this is triggered when I select a rectangle. When paste is triggered then I paste it
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you are nearly done, I would say.

Suppose that the rectagle just drawn is called rjd.
And that you have a method, something like:

You now have a list of selected Shapes. Now decide how your copy & paste
process will go. For instance: if 'paste' is clicked, and your selected list
is non-empty, then on the next mouse click on your panel, you could
add a copy of the selected shape, or ask for which selected shape a
copy needs to be made.

I'm not sure what code you would like to add to your 'copy' menu entry.
Maybe, if you have more than one Shape selected, a press on 'copy'
might pull up a selection dialog, letting the user choose one particular
shape.

So, it is a matter of keeping the state of your program. Since I do not know
any of your program, I hope I gave you at least some ideas.

Greetz,
Piet
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet Souris I managed to have the list containing all the shapes that must be copied. Now my problem is how to copy it to a new location. i was trying to use setLocation() method but Rectangle2D does not have this method. can you please suggest me any other method similiar to setLocation.
thank you
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nathalie,

that is easy. Say you have a Rectangle2D.Double named rect.
Then it has public fields rect.x and rect.y.
So all that is needed to change the location is to do an update of rect.x and rect.y.

Try the following code:


Greetz,
Piet
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my program. i can make use of rect.setBound() with the new points.where the user want the shape to be copied??
I am on the right track?? or not??
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, according to the API, a Rectangle has a setBounds method.
But a Rectagle2D.Float/Double only has a 'setRect' methode, as far
as I can see. In your second post you were talking about
Rectagles2D.Float. Anyway, simply adjusting the x and y as I
did in my example program, seems easy enough.

Yes, you're very close, but are you making copies of rectangles,
or are you moving rectangles?

Greetz,
Piet

 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm making copies of it
 
Piet Souris
Rancher
Posts: 1943
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
h Nathalie,

okay.

Well, there's not much in a Rect2D class to copy. It has an x and an y field,
denoting the upper left corner, and it has a width and a height.

So, lets say you have a Rec2D called rec, and you have a List of all these
rect2D's, called listOfR2D.

Then if you want to 'copy' rec to some new location with upper left
corner xn, yn, then all that's needed is:

listOfR2D.add(new Rectangle2D.Double/Float(xn, yn, rec.width, rec.height).

and eventually do a repaint of the panel that is showing all those
recatngles.

Greetz,
Piet
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!