Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Java delay between paint();

  1. #1
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default 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.
    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");
                     }
                     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");
                     }
                 }
             }        
            });
    thats the part of my code i need help with. it always goes straight to my phoneMenu.png image and skips the welcome

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Java delay between paint();

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Java delay between paint();

    Quote Originally Posted by ale626 View Post
    mind giving me a hand on swing timers, not the code but advice on how to implement it.
    I gave you the links to two sections of the Swing tutorial. If there's anything there you couldn't grasp, quote it and ask a specific question.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java delay between paint();

    Quote Originally Posted by ale626 View Post
    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.
    Sure, what part of the complete tutorial do you want us to re-write for you?

    But seriously, please ask a specific answerable question.

  7. #7
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default 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
    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 do have a couple questions, why does thread.sleep() not give the same results as the timer?
    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?

  8. #8
    PRW56 is offline Member
    Join Date
    Mar 2012
    Posts
    75
    Rep Power
    0

    Default Re: Java delay between paint();

    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?
    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Java delay between paint();

    Quote Originally Posted by ale626 View Post
    why does thread.sleep() not give the same results as the timer? I figure it has to do with threads, but i dont even know what they are.
    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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 05:52 AM
  2. Path Gradient Paint in Java 2D?
    By rtc1 in forum Java 2D
    Replies: 2
    Last Post: 05-07-2010, 06:24 PM
  3. Java Paint Method?
    By leapinlizard in forum Java 2D
    Replies: 2
    Last Post: 02-11-2010, 07:01 PM
  4. How to mesure ping delay in JAVA..?
    By sacr83 in forum Networking
    Replies: 4
    Last Post: 06-15-2008, 06:37 AM
  5. Replies: 1
    Last Post: 02-15-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •