Mouse shift event coding - to trigger a predetermined sequence of images to a GUI
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Programmers
I was hoping if anybody out there might know of a program or tutorial concerning mouse shift event listener gui programs, that would present a predetermined sequence of images to an area of the gui upon the mouse being shifted over that specific area of the gui.
At present I have code that will change an area's color if the mouse is shifted over it, but I was hoping to code something a bit more spectacular, ie: for it to activate not just the appearance of an image, but as said - a sequence of images, which will keep on sequencing until mouse cursor is shifted off.
If any of you know of such code, or where it may be found, I would be glad, having followed several red herrings (spinnners, html) in pursuit of this objective already.
Thanks for reading.
PS: Dear Joanne, and Ritchie,
Indeed, There is'nt much difference between this topic and the previous one.
The reason I rewrote it was because by the first (and so far only) response I got, it evidently had been interpreted as a Game programming design request, 'cause of my reference to a pack of cards, whereas it could have referred to any predetermined sequence of any images - the alphabet, roadsigns, sephamore, heiroglyphs - you name it.
The fact is that it isn't a program requiring game programming design, therefore I hope you appreciate I got the impression my first letter was giving the wrong impression of what I was looking for. I wrote a letter to the sender to that effect, explaining more concisely what I was seeking - something less advanced than game coding.
It was because of this I rewrote my post, also because of this I am reposting this request on the site, whether or not anyone can offer help.
If you, Joanne and Richie, as you've thus already evidenced, have a clear understanding of what I seek, perhaps you could help me out here, or suggest any extant coding that would manage this effect?
I'm guessing you will have an array of images, and then cycle through the array with calls to Thread.sleep in between each image presentation (so they don't race by in a blur).
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
Any Swing Component can have a MouseListener and MouseMotionListener added to it. You have already worked out how to get it to change colours, presumably with the mouseEntered method.
Suggest:
Your mouseEntered method starts off a new Thread (possibly with the SwingWorker class if using Java6)
Make the continuation of that thread dependent on a boolean "mouseInside" variable, maybe in a while loop.
Your thread executes the animation or sequence of images or whatever.
Your mouseExited method sets the mouseInside variable to false.
Marc has already told you to use Thread.sleep() (which declares InterruptedException) between successive images.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
put your slideshow in a Swing.timer mouseEntered starts the timer mouseExited stops the timer (and removes last image if req'd)
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
I'm not having a go at you here, but there is some etiquette that needs to be pointed out, so please look on this as constructive criticism that will save you and other people a lot of time in future.
Consider the case where you ask the same question in two separate threads. Someone comes along and answers in one of the threads. A bit later, someone else comes along and sees the unanswered thread first. They put a very similar answer in that thread. That second person has wasted their time because you already have the answer. If you get an answer that is not relevant to your question, by all means say so. Other people will then come along, see you still have a problem and maybe try and give a different answer. Rephrasing your question may even help here. But please do this in the same thread. If you really feel you need to start a new thread, put a note in the first thread that you no longer need answers there. Maybe even change the title to say the thread is closed. That is what I was doing when I posted a link in your second thread back to your first thread. I was just indicating to other people that a conversation had already started and that posting in that thread would be better than starting a new (possibly duplicate) conversation here.
This is now the third thread on this topic so it's probably a good idea if you go back to the other one that is still open and let everyone else know it is closed (assuming a moderator doesn't beat you to it).
Joanne
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
I think Joanne is right; I shall find the 1st of the three threads and close it
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Marc weber,Campbell Ritchie, Michael Dunn, Joanne Neal,
Thank you for your respective advice, which I have taken on board.
When I've finished the program I will post it to this thread, as I think when done the result will be fascinating.
Speak to you later,
Yours
Simon
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Programmers,
I was wondering if any of you that have already
helped with this subject, might give me your e-mail
address/es then I could send you the gifs that
comprise my program's gui, and when you run my
code, you will get a better idea of what I am
attempting to make ?
Re: Mark Webers entry:-
My program is still at an early stage, and I haven't got to the level of putting gifs into an
array which'd be slowed by Thread's sleep method -
though using an array did occur to me - or even
using the Math random number method - for display
of gifs from a list to be spontaneous, than
predetermined.
is there any code out there which illustrates
either (or combined) approach ?
In fact I have tried populating an Array with my
gifs, but compiler doesn't approve - so I suppose
I'm doing it wrong - but I don't have a text upon
such an initialisation - they all deal with
standard data type initialisations to arrays, okay
that puts me somewhere in the stone age, but we've
all got to start somewhere.
If I could (all the same) accomplish putting the
gifs into an array, I could then try Michael Dunns
advice to use a swing timer and Mouse Entered
action Listener, and Campbell Ritchie's advice to
subclass program of Thread, using a boolean
expressioned while statement for the array,
contingent on the mouseActionListener.
Anyway, here is the program, at present. If you
could give me an E -mail-able address, I can send
the gifs, and then it would make a bit more sense.
Thank you for your advice,
Hoping to hear,
Yours Simon
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
oh 'ell, upon checking my post, I realise I got the first program 'MainMethod' down wrong, it should've read:
Sorry 'bout that.
Yours
Simon.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
No, please don't go e-mailing anything. Read this FAQ.
And adding all those cards individually rather than using a for-loop looks a really awkward bit of programming. Do you actually want a 6*7 GridLayout? Shouldn't it be 11*7 or 7*11?
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie,
I understand your (or anyone else's) reservations about giving out their E-mail addresses, in these times of computer viral infections and the like, so I won't belabour the issue, though I know there is nothing amiss with the gifs I was considering sending, I don't expect you to take what I say as gospel.
I have tried changing the layout's axi from (6,7) to the more appropriate (11,7) or (7,11) but gui gives none of these instead obdurately sticking to an output of 13 across by 6 down.
I realise the code I used could be reduced, but as stated I don't know how to populate an array with gifs, and haven't found any code on the net to this effect.
I found some code, called "Animate.java" which runs with "Animate.asp", which I thought might be of use, though having changed the "Animate.asp" to take a few of my gifs, and having put them in the same file, and tried compiling "Animate.java" on both jdk1.4 and 1.6, I get :-
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cd c:\Java\Java Six
C:\Java\Java Six>javac Animate.java Note: Animate.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details.
C:\Java\Java Six>
Is there a way I can fix this?
I enclose the code to "Animate.java" and "Animate.asp" below. If it could be made to run, it'd be a start. Thank you for your help.
Yours Simon.
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Simon Evans: C:\Java\Java Six>javac Animate.java Note: Animate.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details.
C:\Java\Java Six>
Is there a way I can fix this?
This is only a warning. The code should still have compiled.
Just for reference - when you do get this warning, you should recompile with the -deprecation option. This will tell you which deprecated APIs are being used. The Javadoc should then tell you what to use in their place. [ August 19, 2008: Message edited by: Joanne Neal ]
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
You are using Thread.stop(); which is not just deprecated, but dangerously deprecated. Not quite sure what you use instead, it's such a long time since I had to Thread like that. But Java Tutorials to the rescue! Try there first. You might get away with simply setting the Thread to null.
To set up an array of Images, is just like setting up any other array . . .You may need an array of Buttons too. I thought you had 77 images; you have in fact got 78, hence 11*7. Sorry. That would be 6*13. Check the constructor parameters for GridLayout to see which is rows and which columns. Also whether you can actually get 13 into the space available.
The reason for not giving advice by e-mail is that all discussion ought to take place on the boards, in public, where everybody can take part and help.
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Campbell Ritchie: You are using Thread.stop(); which is not just deprecated, but dangerously deprecated.
Must get my eyes tested. I did do a quick check for that, knowing that it was one of the few times when a deprecated API definitely should be replaced, but I guess I missed it.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
Think nothing of it, Joanne; after all I missed that he was using 78 cards, not 77.
Thread.stop()? Can you remember what you use instead? Or do you simply put a while loop inside the run() method?
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Campbell Ritchie: Think nothing of it, Joanne; after all I missed that he was using 78 cards, not 77.
Thread.stop()? Can you remember what you use instead? Or do you simply put a while loop inside the run() method?
The thread will normally stop as soon as you exit the run method. However, the OP has an infinite loop in the run method, so at the moment it will never exit. The normal way to stop this sort of thread is to have a boolean variable in your Runnable class which you check the value of as part of the while loop. You then need another thread to change the value of this variable.
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie Joanne Neal,
I think the program 'Animate04' at //http://www.developer.com/java/other/article.php/1587091#Preface by Robert Baldwin, is close to what I am seeking, ie: worth nicking. Speak to you later. Yours Simon.
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie and Joanne Neal, and other programmers,
I have requisitioned R.G.Baldwins code for program Animate04 (now
Animate06) so that - for a start, it'll present two Jlabels to the
ContentPane, that will go through the array of gifs when the mouse is
within the ContentPane.
The next appropriate step I had considered would be that, in order to get
the Image to change when the mouse was only on the pertinent image (rather
than both when the Pane area is broached, as it does now)I ought change
the line :
getContentPane().addMouseListener
with code that'd replace the contentPane with the JLabel objects, label
and labeltwo, but that presents a difficulty - first it appears a Jlabel
object's area is not recognisable by the addMouseListener() method - or so
the compiler says.
Also would it not be true to reckon that the whole of the addMouseListener
method -all sixteen lines down to the
setDefaultCloseOperation(EXIT_ON_CLOSE);
line - would have to be added to each object that ought have a
MouseListener added to it - or might there be a more efficient way of
coding it?
As I was hoping for an eventual gridLayout of say 7 * 11 - or 6 * 13 (the
1*2 is just to keep it brief) - that'd entail a lot of (hopefully)
extraneous code (ie: 16 * 77 = 462, or so, lines).
You can use any gifs in this program - so long as they are named 0.gif up
to 4, to get an idea of what it does, and what it is intended to do.
I hope you can offer me any advice regarding where take it from here.
Thank you for your help,
Yours Simon
Code follows:-
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
just so you can compare to another way, here's a timer version. also should not require too many more lines of code to expand to your 78 images
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Programmers, I am writing to you separately, regarding previous correspondence,
Dear Michael Dunn, Thank you for 'Testing' it's very close to what I was looking for, if I have any criticism it is that you've done most of my work for me, as the next step I was going to take, after changing the GUI layout, was to randomize the gif sequences - but 'Testing' has already accomplished that for me - Much Thanks.
Dear Campbell Ritchie, I have been trying to incorporate the coding you suggested I use in initialising the ImageIcon array 'images' with a for do loop, into the program 'Testing' as provided by Michael Dunn (see above) as I wanted to use all 78 images for each array, (thus using 78 arrays of 78 images each in total) but it seems the compiler doesn't accept the line:
images[i] = new ImageIcon(String.format("%d.gif",i));
pointing to the String.format part of the line. It isn't due to the jdk's grading, as I ran it on jdk 1.6 and got the same response.
The code I am compiling is as follows (it is practically as Michael Dunn's 'Testing' program, at present) :-
Thank you for your help thus far.
Speak to you later,
Yours Simon.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
Is it working? That is the important thing. I am surprised you can get new ImageIcon("20.gif") and not the String.format bit. Try a few little programs with String.format like this, and see what happens:Try changing to
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie,
I am not sure if the program 'Tarot' is working, as it isn't compiling.
If I put code to program Tarot as writ above to the compiler (jdk 1.4 or 1.6) it returns the below:-
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\simon.BLUECHIP-KDOCDA>cd c:\backup\Project Tarot Two
C:\backup\Project Tarot Two>javac Tarot.java Tarot.java:14: cannot resolve symbol symbol : method format (java.lang.String,int) location: class java.lang.String images[i] = new ImageIcon(String.format("%d.gif",i)); ^ 1 error
C:\backup\Project Tarot Two>
Anyway I tried recoding it using:
String pictureName = String.format("%d.gif", i)); . . . new ImageIcon(pictureName);
but I am not sure what you mean by pictureName - I tried
newImageIcon([i].gif);
but the compiler didn't accept that either. Is there a tutorial out there on String formatting ? - so's I could get the hang of it.
Thank you for your help.
Yours,
Simon.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
No, I can't think of a String.format tutorial offhand. The method is here and it takes (String, Object ...) as its parameters. There is a little about "formatting numerical output" in the Java� Tutorials, and there is a bit in the Formatter class about what the tags mean.
The format method was introduced in Java5, so it won't work in Java 1.4, but ought to work in Java 6. I copied and pasted your class and the only error I got was "cannot find class Testing," but the compiler was happy with the bit about String.format. Are you sure about your PATH that you aren't using Java 1.4 inadvertently?
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie,
The compiler return you mentioned was due to my oversight re: the last method, which should've read:-
public void run() { new Tarot().buildGUI(); }
Okay, upon changing that, and putting program 'Tarot' to the jdk 1.6, again I got:-
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>set PATH=C:\ProgramFiles\Java\jdk1.6.0_0 5\bin;%PATH%
C:\Documents and Settings\Administrator>cd c:\Java\Java Six
C:\Java\Java Six>javac Tarot.java Tarot.java:14: cannot resolve symbol symbol : method format (java.lang.String,int) location: class java.lang.String images[i] = new ImageIcon(String.format("%d.gif",i)); ^ 1 error
C:\Java\Java Six>
- to my disappointment.
I seem to remember being told with a previous program ('HorseTraderOrder') that it may've been the jdk - but if the path given didn't point to a javac file, the compiler would say so, but it doesn't, and the only path offered it was (see above) to the 1.6 version.
Is there an alternative means of doing it?
How did programmers manage such programs before jdk1.6?
The only alternative would seem to be to write pages & pages of repetititious code, which as you said, looks bad.
Hoping to hear.
Thank you for your help.
Yours,
Simon
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
It looks like auto boxing doesn't work in this case. Try
Just for future reference, there's a Disable Smilies option (see lower left of edit page) that will stop inappropriate smilies appearing in your post.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
If we disable the smilies to get rid of the effect, you see there is an extraneous ; before the ) which the compiler doesn't like. Get rid of the ; before the ) and it ought to be all right.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
And if you are putting "" + i + ".gif" in () you don't need the String gifName = bit.
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie and Joanne Neale
I have made the alteration/s you recommended. The Program 'Tarot' now runs, but unfortunately, though it presents the GUI it
doesn't present the gifs - just a blank GUI.
i am not sure where the discrepancy in the code is. The 78 gifs referred to in the
code are in the same location as the few as referred to in program 'Testing' - so
that can't be where the problem is.
Hopefully you could suggest where it is going amiss? - you could try using any 78
And if you are using Java 1.6 why on earth can't you get String.format() to work.
Try this little class:
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie, Joanne Neale, Michael Dunn & other programmers, I'm sorry, looking at the program 'Tarot' again, I realise I left out the vital line : -
p.add(label); p.add(labeltwo);
Clever, ain't I? Anyway it compiles and runs okay now. I only have to alter the GridLayout, and it'll be working as desired. Thank you for your help. Yours Simon
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32621
4
posted
0
At long last. Well done
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie, and other programmers,
Thank you for your earlier replies.
I put the program StringFormatTester re:
to the compiler, and it returned the following:-
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\Administrator>cd c:\Java\Java Six
C:\Java\Java Six>javac StringFormatTest.java StringFormatTest.java:5: cannot resolve symbol symbol : method format (java.lang.String,java.lang.String,java.lang.String) location: class java.lang.String System.out.println(String.format("%s %s", "Simon", "Evans")); ^ 1 error
C:\Java\Java Six>
I have also put to it the code suggested by Joanne Neal, re:-
and compiler returns the following:-
C:\Java\Java Six>javac Establishjdk.java
C:\Java\Java Six>java Establishjdk 1.6.0_07
C:\Java\Java Six>
Although it doesn't matter at present, as I have got program 'Tarot' to run.
One thing that might be of relevance, is that if I run it on the jdk1.6 I only get a blank GUI, and its the same with the program 'Testing', but if I run either programs 'Testing' or 'Tarot' on the other jdk (1.4 I believe), then it runs alright and I get a GUI of rows of gif images, which randomly change when the mouse cursor passes over them, as was the desired effect.
I would likke to elaborate on program 'Tarot',so with your acceptance would like to continue with this thread.
I enclose the program as developed at the end of this post, it relates to the six row by seven columned 'Gypsy Spread'.
I want to develop a program derived of the 'Name Spread' which takes the first, middle and surname of a user, and then creates a spread for each of these to a single GUI.
This would require a JOptionPane for the user to input their names, and would return a GUI based on this information.
Hence if for instance I input : Simon Angus Evans, it'd return a GUI of gifs of five columns and three rows, though of course for someone with names of varied length it'd return a GUI of various lengthed rows, anyway I'll try crossing that bridge when I get to it, at present I keep getting an error return on the userinput part of the program - I can't see what I have coded wrongly that I didn't put into previous compiling - and running programs (ie: HorseTraderOrder) but thats the way it seems to be. This is what is confuting the compiler at present:-
which returns the following:-
C:\Java\Java Six>javac NameInput.java NameInput.java:10: array dimension missing String names = JOptionPane.showInputDialog("Dear User, Please enter the number o f names you have"); ^ 1 error
As mentioned above this is the code to my program 'Tarot' the relevant seventy eight gifs may be downloaded from any of the decks listed at :
There is still something wrong if you can't get a String#format call to compile in Java 6. Look at the details in the String class, and write "javac -version" to your command line.
You are writing " " + i + ".gif" for your Strings; you do realise that inserts a leading space?
I still think you are better with for loops to add Components, not add(panel21); add(panel22); I usually wouldn't add the same Listener to many Components, but in the case of "mousie" it actually seems to work nicely. And well done getting it to work in the end
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
Originally posted by Simon Evans:
which returns the following:-
C:\Java\Java Six>javac NameInput.java NameInput.java:10: array dimension missing String names = JOptionPane.showInputDialog("Dear User, Please enter the number o f names you have"); ^ 1 error
The error is actually on the previous line. Arrays are a fixed size which has to be specified when you create them. So you need something like String [] usersNames = new String[10];
If you don't know how large your array needs to be then you need to use a List of some kind. ArrayList will probably suit your purpose.
Simon Evans
Ranch Hand
Joined: Mar 27, 2008
Posts: 93
posted
0
Dear Campbell Ritchie, Joanne Neal & programmers
I have changed the relevant line in program 'Tarot' to:
for (int i = 0; i< images.length; i++) { images[i] = new ImageIcon( + i + ".gif"); };
It still produces a blank GUI to the jdk1.6's running of it, though.
I am not sure what its effect on the jdk1.4 is, as the computer holding it is 'down' for the moment, but as it ran okay without changing, it
doesn't apply, I suppose.
I have tried putting the text:- javac -version
to the console, but get:-
javac: invalid flag: -version
along with all the other associated gen that follows such returns.
I agree the program 'Tarot' could do with concertina ing, as the repeated code looks iffy, apart from being a pain to type,
I tried this instead of those three columns of repeated code, ie:-
for (int i = 0; i< 41; i++) { JLabel[i] = new JLabel(images[0]); p.add(JLabel[i]); JLabel[i].addMouseListener(mousie); }
but this got me :-
C:\Java\Java Six>javac FT.java FT.java:21: cannot resolve symbol symbol : variable JLabel location: class FT JLabel[i] = new JLabel(images[0]); ^ FT.java:22: cannot resolve symbol symbol : variable JLabel location: class FT p.add(JLabel[i]); ^ FT.java:23: cannot resolve symbol symbol : variable mousie location: class FT JLabel[i].addMouseListener(mousie); ^ FT.java:23: cannot resolve symbol symbol : variable JLabel location: class FT JLabel[i].addMouseListener(mousie); ^ 4 errors
C:\Java\Java Six>
Is my attempt anywhere near or light years out ? (I was wondering).
I have recoded program NameInput in accordance with Joanne's diagnosis, thank you. re:-
Thank you for your help. Hoping to hear. Yours Simon.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Mouse shift event coding - to trigger a predetermined sequence of images to a GUI