Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Mouse over tooltips  (Read 1563 times)
0 Members and 1 Guest are viewing this topic.
Offline aufde

Senior Newbie





« Posted 2013-01-02 07:16:36 »

What would be the best way to implement mouse over tooltips? I've never done something like this, but my first thought was to have a rectangle for each mouse over area and use a mouse listener to determine which rectangle the mouse was in. I have a feeling this isn't the most efficient or easiest way to do it, but I'm not really sure how else to do it. Maybe use frames? I don't even know where to start looking on how to make something like this.

Here's what my UI looks like right now:
http://i.imgur.com/Cp7sZ.png

I would want to add mouse overs to each of the stats, inventory slots, equipment slots, HP/MP/XP bars. I would want the tooltip to have a border (probably just an image I would supply) and obviously some info on what your mouse is on.

Anyone have some examples of this? Or at least point me in the right direction? Thanks!
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2013-01-02 07:17:21 »

Check if the mouse hovers for a certain amount of time and display a rectangle with text in it? How else? Tongue

The best way to deal with the draw order is to have a component that is on top of everything else, and it checks which component the mouse is hovering over and displays that component's tooltip text. This is where OOP comes in:

1  
2  
3  
4  
5  
public abstract class UIComponent {
    ....

    public abstract String getTooltipText();
}


All UI components extend that class and have a certain tooltip text set (returns null if no tooltip text to show).

Offline aufde

Senior Newbie





« Reply #2 - Posted 2013-01-02 08:15:50 »

I think I only have one component right now that has everything in it. All of the graphics are handled as one and displayed on one canvas. I don't really know how to use your suggestion.

This is my first project, so the whole window and display functionality was copied from an example I found so I don't really understand it fully or know how I would go about changing it. I guess a better question would be for a tutorial on basic UI implementation because it's sounding like the way I did it isn't the right way.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #3 - Posted 2013-01-02 17:48:35 »

First, we have to theck, if the mouse is over the desired location (I made it a box...).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
mouseX = mouseXPosition;
mouseY = mouseYPosition;

x = squareXPosition;
y = squareYPosition;

width = squareWidth;
height = squareHeight;

if (x >= mouseX && y >= mouseY && mouseX <= x + width && mouseY <= y + heigth) {

//show the tooltip here...

}


Then do what ra4king said... Smiley EDIT: Also, you can put this code in a, method like checkIfOver(int mouseX, int mouseY);

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline aufde

Senior Newbie





« Reply #4 - Posted 2013-01-02 20:26:20 »

Ok, that makes more sense. I thought there was a java class that he was talking about that I didn't know about. I was kind of hoping there was because I wanted some prebuilt class to handle everything  Tongue

This is similar to what I was originally thinking needed to be done, except I was going to use the Rectangle class and use cointains(Point p) method to determine if the mouse is inside any of the areas. Would this run slower than the code you posted Peter?
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2013-01-02 20:29:16 »

No, there isnt any prebuilt class in the Java API to handle your problem. But about running slower, well, I think that the code I posted might run a bit faster (of course the code might contain some bugs) Smiley

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2013-01-02 20:30:09 »

For java2D and swing/awt, I think you can use FocusListener (not absolutely sure, just popped up in my head)
Or just mouseEntered() and mouseExited() from MouseListener, which detects if the mouse has entered/exited whatever component you add the MouseListener to.
Offline Best Username Ever

Junior Member





« Reply #7 - Posted 2013-01-02 22:02:36 »

Efficiency? How many thousands of rectangles are you planning to do hit testing on?
Offline aufde

Senior Newbie





« Reply #8 - Posted 2013-01-02 23:46:29 »

Efficiency? How many thousands of rectangles are you planning to do hit testing on?

So what you're saying is it doesn't matter right now?

lol, probably just 30-40 rectangles for the UI. For some reason efficiency is always on my mind when I'm programming. Just trying to build good habits for when I am doing a thousands of rectangles Smiley It never hurts to start good practices early.
Offline LunaticEdit

Senior Member


Medals: 8
Projects: 1



« Reply #9 - Posted 2013-01-03 00:20:34 »

If you're worried about it and plan on having a lot of rectangles all over the place, you implement a parent/child relationship like on most UIs. That way you check the top-level parents FIRST, and only if the mouse is in the parent's area do you check the children. For example if you have 4 windows, you simply need to check the active window -- and check ti make sure the mouse is actually inside the window (as apposed to checking every element on the screen),

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Best Username Ever

Junior Member





« Reply #10 - Posted 2013-01-03 01:03:46 »

No don't do that. It might make the program slower depending on its data. If it ever gets to the point where the processing time spent on checking tool tips justifies spending time on it to improve it over some other aspect of your program, then you might want to try something else but probably not that method. Sometimes bruteforcing is faster because technology has changed; memory access time hasn't kept up with processor speeds. (That's why arrays are better than linked-lists and grids are better than spatial trees.)

If you absolutely had to improve on the efficiency test (for hit testing tons of in-game objects maybe) you would try something different. You would either share the rectangles with the GPU or other threads and attack the problem in parallel. Or you would develop a way to "instantly" determine which rectangle to jump to (similar to a random access collection or hash table). For example, you might look at that 3x4 grid and decide you store its strings in a 12 element array instead of 12 random objects.

1  
2  
3  
int x = (mouseX - left) / cellWidth;
int y = (mouseY - top) / cellHeight;
if(x >= 0 && x < 4 && y >= 0 && y < 3) { toolTipText = texts[y * 4 + x]; }


However, you would only want to invest your personal time in doing this if you had a lot of rectangles. Less than a thousand? Probably not going to make a meaningful dent in your execution time.

And lets not forget, tool tips don't pop up the instant your put your mouse over them, or else you would see a lot of flickering boxes on your screen while moving your mouse around. It's not an aspect of a game that needs to be optimized and even if it were slow, then you could span the look up over several game updates if you absolutely had to. (In another low priority thread even.)
Offline aufde

Senior Newbie





« Reply #11 - Posted 2013-01-03 09:30:52 »

Well.... I'm glad I only have 40 or so rectangles because I understood very little of what you said  Tongue

My game is going to be a simple one, so hopefully I can slowly work up to that kind of complexity. Some day though  Cheesy

Good new is I've gotten tooltips working on a couple of elements already. I've made it so it picks the background box based on the number of lines in the text, but it still doesn't look as good as I would like. Would there be a relatively easy way to make the box fit to the tooltip? Right now I just have the tooltip box as a sprite that is pulled like the rest of the graphics in the game and I have one for a tooltip with 1, 2, 3, 4, 5, or 6 lines. It chooses the right one based on the number of \n characters in the string. Is there a better way to do it? One that I'll hopefully understand?
Offline Varkas
« Reply #12 - Posted 2013-01-03 14:40:11 »

Why don't you use the Swing components? They have an inbuilt tooltip functionaly and are highly customizable. I'm always a bit puzzled when I see people spending time to implement functions again that already come with the Java libraries.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline aufde

Senior Newbie





« Reply #13 - Posted 2013-01-04 21:53:14 »

That's what I was asking, if there was anything that did tooltips already in java. Could you give me a real simple tooltip with swing? I don't know where to start looking and I know there's a lot of stuff that goes into swing programming. Or if you know of a good tutorial, let me know. Thanks
Offline Best Username Ever

Junior Member





« Reply #14 - Posted 2013-01-04 22:26:33 »

That would rely on your boxes being Swing components. (Each one of them floating in the middle of the screen.) You might be able to use one for the entire screen and update the text depending on where the mouse is, but you would still need to know which box the mouse is over.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (208 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!