Results 1 to 9 of 9
Thread: Java delay between paint();
- 05-29-2012, 06:47 AM #1
Member
- Join Date
- May 2012
- Posts
- 27
- Rep Power
- 0
Java delay between paint();
i have to simulate a phone, and the part im having trouble on is painting the image when it turns on.
so i click the power button then a welcome screen comes on after a 3 second delay the image should change to the menu image.
thats the part of my code i need help with. it always goes straight to my phoneMenu.png image and skips the welcomeJava Code:powerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (powerButton.isSelected()) { try{ //grabs the image and has exception if there is no file. image = ImageIO.read(new File("phoneWelcome.png")); repaint(); } catch (IOException ev){ System.out.print("Missing Picture"); } wait.manySec(3); //class used to delay. try{ //grabs the image and has exception if there is no file. image = ImageIO.read(new File("phoneMenu.png")); repaint(); } catch (IOException ev){ System.out.print("Missing Picture"); } } else { System.out.println("button not selected"); try{ //grabs the image and has exception if there is no file. image = ImageIO.read(new File("Phone2.png")); repaint(); } catch (IOException ex){ System.out.print("Missing Picture"); } } } });
- 05-29-2012, 09:41 AM #2
Re: Java delay between paint();
:yawn:
Don't call sleep(...) directly or indirectly on the EDT. Use a Timer.
Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-30-2012, 08:14 AM #3
Member
- Join Date
- May 2012
- Posts
- 27
- Rep Power
- 0
Re: Java delay between paint();
mind giving me a hand on swing timers, not the code but advice on how to implement it.
My professor lectures off wikipedia for my gui class -.-
so im struggling to read the tutorials on oracle.
- 05-30-2012, 09:11 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Java delay between paint();
Swing uses one single thread (the EDT thread == Event Dispatch Thread) for all its painting and event dispatching (such as button presses handling); you actionPerformed( ... ) method runs in that thread and if your method sleeps it makes the EDT sleep and you don't want that because it has work to do; your method has to create another thread that should sleep a bit and make the EDT update the display a bit later. Timers can do that job.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-30-2012, 10:49 AM #5
-
Re: Java delay between paint();
- 05-31-2012, 01:12 AM #7
Member
- Join Date
- May 2012
- Posts
- 27
- Rep Power
- 0
Re: Java delay between paint();
Sorry about that, was just a little frustrated but i do appreciate the help.
I did get it to work, using the other oracle page Timer (Java Platform SE 7 )
Here is my code
I do have a couple questions, why does thread.sleep() not give the same results as the timer?Java Code:powerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (powerButton.isSelected()) { try{ //grabs the image and has exception if there is no file. image = ImageIO.read(new File("phoneWelcome.png")); repaint(); } catch (IOException ev){ System.out.print("Missing Picture"); } ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { try{ //grabs the image and has exception if there is no file. image = ImageIO.read(new File("phoneMenu.png")); repaint(); } catch (IOException ev){ System.out.print("Missing Picture"); } } }; Timer time = new Timer(3000, taskPerformer); time.start(); time.setRepeats(false); } else { System.out.println("button not selected"); try{ //grabs the image and has exception if there is no file. image = ImageIO.read(new File("Phone2.png")); repaint(); } catch (IOException ex){ System.out.print("Missing Picture"); } } } });
i figure it has to do with threads, but i dont even know what they are.
i looked around, and even parts of oracle site are iffy, like so: A thread is a thread of execution in a program.
also any suggestions on how i can make a lot of buttons appear along with my new image after the 3 second delay?
would just creating them inside the actionPreformed method work? or is there a more standard way of doing it?
- 05-31-2012, 05:53 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 70
- Rep Power
- 0
Re: Java delay between paint();
dude if you make them at the beginning of the class, or just declare them, you can add them at any time, but if I were you I would use card layout for the phone screen, then you can add and arrange the buttons at the beginning, then switch to the new screen.also any suggestions on how i can make a lot of buttons appear along with my new image after the 3 second delay?
would just creating them inside the actionPreformed method work? or is there a more standard way of doing it?
- 05-31-2012, 12:13 PM #9
Re: Java delay between paint();
Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
Threads and Swing
The Last Word in Swing Threads
Will the real Swing Single Threading Rule please stand up? « bitguru blog
Use a SwingWorker to run any time consuming task in the background. That's what the class was designed for.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Paint is invalid type for variable paint.
By minibronya in forum New To JavaReplies: 3Last Post: 05-25-2012, 05:52 AM -
Path Gradient Paint in Java 2D?
By rtc1 in forum Java 2DReplies: 2Last Post: 05-07-2010, 06:24 PM -
Java Paint Method?
By leapinlizard in forum Java 2DReplies: 2Last Post: 02-11-2010, 07:01 PM -
How to mesure ping delay in JAVA..?
By sacr83 in forum NetworkingReplies: 4Last Post: 06-15-2008, 06:37 AM -
How to write a JAVA program to mesure ping delay..?
By sacr83 in forum NetworkingReplies: 1Last Post: 02-15-2008, 04:07 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks