Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Select ImageIcon in JPlane

 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Full disclosure: this problem is part of an exercise - so I'm only looking for tips & suggestions - yet I'm completely stuck, so I feel the need to ask for help.

I'm trying to create a window that displays images. You can turn over the images by clicking on them, and move them by holding them.

I wanted to use JFrame, JPanel, JLabels, and ImageIcons to solve the task, yet I'm now allowed to use JLabels - so I've attempted to use a paintComponent instead.

I currently got a JFrame, with a JPanel, and an image drawn on the JPanel. I've also been able add a MouseAdapter to the JPanel that gets the coordinates of the area being pressed. Yet I don't know how to use it to select my ImageIcon - maybe save the JPanel area the ImageIcon occupies, so I can check the mouse coordinates against it? - or something else?

Your help is highly appreciated!

/Cheers

CODE LINK:
https://www.dropbox.com/sh/9m5n64vqmmw5o8w/AACl_h1eO6seW42TULZ-jZGha?dl=0

CODE:


IMAGE EXAMPLE:
 
Carey Brown
Bartender
Posts: 2265
28
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up java.awt.Rectangle. See contains( Point p ).
 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey .
 
Piet Souris
Rancher
Posts: 1641
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Rectangle is certainly a good option.

In similar situations I usually create a dedicated class, say MyImageIcon extends ImageIcon,
that contains the coordinates of the upper left point.
You could then have a List<MyImageIcon>, and whenever there is a click on the panel,
inspect all your imageicons to see on which one is clicked. That need not be an actual
ImageIcon, you could also opt for the icon that is closest to the click.
You could also check for a double click, if you want to select an icon that is hidden beneath
another one.

I wouls also consider using a LinkedList, to easily hav two icons switch places.
A dedicated class is, I think, also handy if you want to work with rectangles, as Carey suggests.

One thing to NOT do is to load an image in a paintComponent method. That method is there to redraw
your panel as fast as possible, and so it should not load any image. Load your images at start up,
or have a dedicated method for it.
 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Piet for the further help .
 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took both your suggestion and used the rectangle class. I've also created a custom class, Card, that extends the imageIcon-class. When a Card is created a rectangle is also created, so when I click on a rectangle I can check which Card it's part of - and then flip that Card; change its imageIcon.

Thanks again for the help .

For those curious, here's my code:

CODE LINK:
https://www.dropbox.com/sh/9m5n64vqmmw5o8w/AACl_h1eO6seW42TULZ-jZGha?dl=0

CODE - ImageLump-class (WiP name):


CODE - Card-class:


IMAGE EXAMPLES:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!