• 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

Is there something similar to HTML tag <map> in Java

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody after a while. My question is, is there something similar to HTML tag <map> in Java, because i want to work with images and i need to create an image-map with clickable areas, something similar to example below

I was trying to find answer by myself, but i fail, so i'm asking you for your advises. Thanks a lot.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what you are looking for. What does the <map> tag mean?
Do you want to be able to click on the area where Saturn is and get some sort of Saturnine action?
Maybe you can have the image layered over some buttons, but it can be awkward to maintain the layout. If the image is opaque, the buttons would be hidden.

I shall move you to our GUIs forum, which looks like where we usually discuss such questions.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a List of Shape objects. Shapes can be circle, rectangle, polygons etc. Then when you click on your image you iterate through each Shape in the List and use the contains(...) method to determine which Shape was clicked.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i'm trying to solve this problem for about 3 days now, and all what i was able to write and find is this short piece of code and some links like: link and link2. Please tell me, am i doing it well right now and what can i do more, because i'm stuck right now i don't know what to do next.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Custom painting is done by overriding paintComponent(), not paint().
2. Invoke super.paintComponent at the beginning to make sure the background is repainted first.
3. The painting code should iterate through the List to paint each Shape. If you have 50 shapes you don't want to hard coded the painting of each shape.

Take a look at the DrawOnComponent example from Custom Painting Approaches. It will show you the basics of doing the painting from objects in a List. The example is more complicated than yours because the List is populated dynamically, but the concept is the same once the Shapes have been added to the list.

Once you get the basic painting done, then you will need to add the MouseListener to the panel so you can add code to check when the user clicks on a Shape.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I'm overriding paintComponent().
2. I'm invoking super.paintComponents().
3. I iterate through my list to paint each Shape.
4. I have added MouseListener to my panel, or at least i think i did this, but it's not working.
Here is my changed code when i try to drawString when i click on rectangle, i don't know how to add conditions to check if you click on ellipse and polygon.

I hope for your advices once again. Greetings.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. I'm overriding paintComponent().
2. I'm invoking super.paintComponents().


I'm not an expert on Swing painting by any means, but these two together sound wrong to me.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And besides that, in general you have to be careful about what your X and Y values are relative to. Sometimes they are relative to the component, sometimes relative to the screen, and sometime they're relative to nothing on the display. In this example I'm pretty sure your mouse click's X and Y aren't relative to the same origin as your Rectangle's X and Y. I suggest you do some debugging to see what values you're getting for those things when you run the code.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't add the MouseListener to the panel.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Radek Gajdos wrote:1. I'm overriding paintComponent().
2. I'm invoking super.paintComponents(). . . .

Use paintComponent not paintComponents for both. And give paintComponent protected access, not public.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody, i've made some crucial changes in my code with some success, but it still doesn't work fluently, because sometimes i need to click many times to get the right output, or click on the surface and then on a shape the get the right output. I paste my code below. And as usual i hope for your suggestions.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a simple solution:

In the MouseListener, instead of setting a bunch of Boolean variables, just set the text of a String variable. Then in the paintComponent() method you simply paint the text in the String. Or even easier just add JLabel to the panel at the desired location and then in the MouseListener you just set the text of the label and it will repaint itself automatically.

For a better solution:

It is never a good idea to have a bunch of if/else statements. What happens when you have 20 or 30 Shapes to paint and click on? The code obviously becomes a mess.

Instead create a custom object that has two properties: 1) the Shape and 2) a click message. Then you store this custom object in the List. The paintComponent() method will access the object and then get the Shape to paint. In your MouseListener you can now just iterate through the List, get the Shape, use the contains() method and then set the message as suggested above when appropriate. Now whether you have 3 or 100 Shapes the code is the same.

For an example of this approach of creating a custom Object check out the DrawOnComponent example from Custom Painting Approaches. The ColoredRectangle custom object stores the Rectangle and the Color of the rectangle, so it is very similar to something you might do.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Thank you for your advices, special greetings for Rob Camick for his instructions and dedication, but i have one more question. In MouseListener, when i add else statement loop strats to act as it was in it only ellipse shape, whitout else statement loop act as i want. Anyone? Code lies below.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to set the default message before you enter the loop (so you don't need an else statement).

Also, when you do find a match you can break out of the loop since there is no need to check the remaining shapes.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've set the default message before i enter the loop and it works fine, but i really don't know how to break out of the loop when i find a match, so if somebody provide a code i will be obliged. I post code where i set the default message.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the "break" statement. Read your Java text book if you have never used it before.

Or you can read through the Control Flow part of the Java tutorial.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice guess, it was my first time using break statement, but i made it, or at least i think i did. If there is still some comments about my code i eager for hear them, if not i think the topic is closed, because i receive an answer, thanks a lot. Below, as always, my changed code.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why you changed the code? All you needed to do was add the break statement.

There is no need for the "foundIt" variable. Just set the message when you find a match. Don't create unnecessary variables.
 
Radek Gajdos
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, mea culpa, i think it's because i'm still learning new things.
 
reply
    Bookmark Topic Watch Topic
  • New Topic