Trying to add tool tip to every image on JPanel - java

I am trying to add tool tip to every image I draw on the JPanel. But each time a new Image is added, the tool tip text gets over written because I am using this.setToolTipText(text); for every image.
As seen in the image, the second traffic light image shows tool tip that reads traffic light 3 because that was the last image added.
Can someone please tell me another way to add unique tool tips? I searched for possible solutions but could not find any.
//Drawing the blockage on the road
g.drawImage(blockageImage, bestMatchRUnit.getX(), bestMatchRUnit.getY(), 5, 5, this);
this.setToolTipText("Blockage: " + blockageIndex); //setting the tooltip
blockageIndex++;

The easiest way is to display a JLabel with an ImageIcon on your panel. Then you can add individual tool tip messages to each JLabel.
Or, the other approach is to override the getToolTipText(...) method of your JPanel. Then you will need to keep an ArrayList of a custom Object that contains two pieces of data:
the Rectangle to represent the size/location of each image
the tool tip for the image
Then in the getToolTipText(...) method you need to iterate through the ArrayList to find the image that contains the mouse point and the you need to return the tool tip message.

Related

How to define highlight-able, clickable custom objects in libgdx (Risk game)

I am making a game similar to Risk and struggling to find a way to implement the interaction with countries.
The basic idea is to create custom objects that are not rectangular and be able to change their colour by clicking them, highlight them with mouseover, or as the game progresses.
How would I go about having highlight-able countries that can be selected? The problem with sprites is their bounding boxes are rectangular, and if I define Box2D vertices and make polygons it gets really messy. Also, there are a lot of countries so a lot of the platformer style solutions don't fit.
How should I also change the colours of what is selected? Would it be best to have an individual sprite for every country and keep switching between them or is there a better way?
One way is to use polygons like you tried but I wonder why and what you mean it got messy. There are tools out there that let you draw vertices over a image and let you export that. You probably need to clean up the data a bit and import it into your app. It's also not very hard to make such an app yourself, have it import your image and start drawing and export to your favorite format. The more detailed you draw your polygons the more detail you get in your.
Perhaps an easier solution would be to use the opacity of each image of a country. Each country gets it's own image and you need to overlap the bounding rectangles to line them all up. When your mouse is hovering over one or more of these bounding boxes you check if the mouse is over a transparent pixel. If it is transparent you are obviously not hovering over the actual country. Some things to consider:
I would create the game in a pixel perfect manner so each pixel of your images is translated to a single pixel of the screen your outputting to.
To align your whole map I would create one big world map in your drawing application. Then save each country but remain the canvas size of the complete map. When packing these images with the LibGDX TexturePacker remove the whitespace (transparent pixels) and you will get an offset in your atlas. You can use this offset for each country to line them up and save precious texture space by removing all that whitespace.
Always check for a simple collision first before diving in deeper.
If you want to have "hover" functionality then don't do pixmap = texture.getTextureData().consumePixmap() each update since it's rather expensive. You might be better off creating your own 2D boolean array that represents the clickable area when you initialize the country object.

OpenCV - Identifying particular object in image using Java

I'm developing an opencv application in java where I need to detect the different marks on the product.I have attached the input image below
In that image I need to identify the non veg mark.
Since i'm new to it, I need help to know which concepts can be used for it.
I need to identify these marks on the input images
After quite a struggle I was able to come up with a rough solution.
First, I separated the veg and non-veg labels.
&
Now in order to get the perfect fit of the non-veg label over the image I resized it to a particular level:
small = cv2.resize(nveg, (0,0), fx=0.12, fy=0.12)
Now I performed Template matching as I stated in the comments' section. To learn more about this topic VISIT THIS PAGE.
Using it I obtained the 'maximum probable location' of the non-veg label in the image.
res = cv2.matchTemplate(food, small, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
In the following image pay attention to the bright spot in the position of the non-veg mark:
Now using the max_loc variable, I added the tuple values to the size of the resized non-veg label and framed it with a rectangle as in the following:
You can see the black spot on the non-veg mark when I labelled it using max_loc:
Hope this helped. :)

How do I expand a Libgdx image from 2 rectangles

I am working on a game using LibGDX, and right now, I am working on the menu screen. What I want to do, is have a small image, set a bounding box, and expand however large I need it. What I think would be optimal, would be to set 2 rectangles. One for width and one for height. If it needs to get bigger or smaller, it would take that rectangle, and duplicate it beside, or beneath the current one, depending if it is for the width or height. I believe there is a builtin class for this, but I cannot seem to find it.
You might want to take a look at NinePatch if I'm not misunderstanding your question. Link here: https://github.com/libgdx/libgdx/wiki/Ninepatches

Create image from text with unknown number of lines

I would like to convert a string of text into an image. The issue is, I want the text to wrap if it is wider than the length of the image, and the height of the image to be dynamically sized to perfectly fit the text, so that I know how much space the text takes up.
I'm working in Java and there are several things I have tried:
Rendering HTML in a JPanel and saving as a BufferedImage. The problem here was that most of the css I used was ignored by the JPanel and the image was unusable.
Using ImageMagick and img4Java. The two big failures with this solution was that I needed the command-line tool installed, which I can't do on our server. The second was that I couldn't easily convert the image to buffered image for use in the rest of the app.
Does anyone know a way to do this in Java?
Thanks!
In this example, an arbitrary panel is rendered into a BufferedImage and displayed in an adjacent panel at half-scale. The example uses a grid of labels, but you can use the wrap feature of JTextArea or the geometry supplied by TextLayout, examined here.
You might use a label containing HTML for the line-wrap, as shown here.
To get an image of that, see LabelRenderTest.

Java - how to add a listener in ImageJ plugin?

I am writing a plugin for ImageJ that scans the pixels of a desired line for brightness over a series of frames in an AVI video. The data is displayed in 2D graph for the active video frame, pixels on the x-axis and intensity on the y-axis.
I want the graph to update itself to show the data for the active frame in the StackWindow (Where the frame is displayed). This is controlled by the slider in the window. I looked into this and found information on the AdjustmentListener class, which I can probably use to check when the slider is adjusted. How do I do this?
Link to the StackWindow class
As far as I know there isn't a particularly elegant way of doing this. The best way is probably to iterate over everything returned by getComponents on your StackWindow and add an AdjustmentListener to that component if it's a Scrollbar. You can find a couple of examples here and here, although those examples are more general than what you need.

Categories