• 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

Select ImageIcon in JPlane

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey .
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Piet for the further help .
 
Nils Anton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


reply
    Bookmark Topic Watch Topic
  • New Topic