Results 1 to 5 of 5
  1. #1
    robs is offline Member
    Join Date
    Feb 2011
    Posts
    58
    Rep Power
    0

    Default Can you repaint during the init method of an applet?

    Hi,

    I have an applet which uses images. Sometimes the images take a while to load so instead of the white screen which appears during the init() method I would like to display a progress bar while they are loading. However, I can't get anything to paint until after init() has run. Is this because you simply can't or am I doing something wrong? Should I be creating all my panels and loading my images after the init() method has finished?

    Thanks in advance,

    robs

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

    Default Re: Can you repaint during the init method of an applet?

    Why not simply load the images in a background thread while displaying your progress bar?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Can you repaint during the init method of an applet?

    An Applet isn't shown until the init() method returns.

    Using a background Thread to load the images is a good idea. Just make sure you update the progress bar on the EDT.
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

  4. #4
    robs is offline Member
    Join Date
    Feb 2011
    Posts
    58
    Rep Power
    0

    Default Re: Can you repaint during the init method of an applet?

    Thank you both for your replies and for the link. It will certainly save me a lot of time knowing that the applet isn't shown until the init() method returns. It does lead me to wonder what I should be putting in it though. At the moment I create and set up all my panels, load images, load sounds and set variable values. If I want to replace the white box with a progress bar couldn't I put everything from my init() method into the start() method and leave init() blank?

    Edit: Panels, load images and load sounds are in my createGUI() method which is called by the init() method.
    Last edited by robs; 01-15-2012 at 10:39 PM.

  5. #5
    robs is offline Member
    Join Date
    Feb 2011
    Posts
    58
    Rep Power
    0

    Default Re: Can you repaint during the init method of an applet?

    I've managed to load the images in a worker thread and the progress bar updates. My problem now is that I don't know where I should load all my panels (which use the images and so have to be loaded after them. I guess I should create another method called loadPanels() and keep using the progress bar to track it, in which case I think I should probably call it from the done() method in my image worker class. I don't know if I should create another worker for it though, it doesn't seem right to run it on the EDT. Also, although my progress bar is working, I'm updating it in the worker thread not in the EDT as you specified. I'm really sorry for the confusion, as I'm sure you can tell all of this is new to me, I would really appreciate any help. Here is my code so far.


    Java Code:
    public void init(){
        try {
            javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't successfully complete");
        }
            worker.execute();
    }
    
    public void createGUI(){
    //Here I set the values of my variables
    Container con = getContentPane();
     con.setBackground(new Color(153, 153, 204));
      layeredPane = getLayeredPane();//use root pane's layered pane
    JPanel jpanel = new JPanel() {
    
        public Insets getInsets()
        {
            return new Insets(40, 30, 20, 30);
        }
    
    }
    ;
    jpanel.setLayout(new BoxLayout(jpanel, 1));
    layeredPane.add(jpanel, JLayeredPane.DRAG_LAYER);
    jpanel.setBounds(200, 50, 300, 100);
    Dimension dimension1 = new Dimension(400, 20);
    progressLabel = new JLabel("Loading, please wait...");
    //progressLabel.setAlignmentX(0.5F);
    progressLabel.setMaximumSize(dimension1);
    progressLabel.setPreferredSize(dimension1);
    jpanel.add(progressLabel);
    jpanel.add(Box.createRigidArea(new Dimension(1, 20)));
    repaint();
    progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    progressLabel.setLabelFor(progressBar);
    progressBar.setAlignmentX(0.5F);
    progressBar.setMinimum(0);
    progressBar.setValue(0);
    progressBar.getAccessibleContext().setAccessibleName("Java2D loading progress");
    jpanel.add(progressBar);
    layeredPane.setVisible(true); 
    progressLabel.setText("Loading images");
    }
    
    SwingWorker worker = new SwingWorker<Image[], Void>() {
        @Override
        public Image[] doInBackground() {
            final Image[] innerImgs = new Image[names.length];
            progressBar.setMaximum(names.length);
            for (int i = 0; i < names.length; i++) {
                myGetImage(names[i]);
                progressBar.setValue(progressBar.getValue() + 1);
            }
            return innerImgs;
        }
    
        @Override
        public void done() {
            //Remove the "Loading images" label.
            try {
                imgs = get();
            } catch (InterruptedException ignore) {}
            catch (java.util.concurrent.ExecutionException e) {
                String why = null;
                Throwable cause = e.getCause();
                if (cause != null) {
                    why = cause.getMessage();
                } else {
                    why = e.getMessage();
                }
                System.err.println("Error retrieving file: " + why);
            }
        }
    };

Similar Threads

  1. Replies: 3
    Last Post: 02-04-2011, 06:30 AM
  2. Applet Image Problem (repaint(); location?)
    By dyelax in forum Java Applets
    Replies: 24
    Last Post: 12-01-2010, 07:29 PM
  3. repaint method
    By manojp476 in forum CLDC and MIDP
    Replies: 10
    Last Post: 08-12-2010, 03:12 PM
  4. init() method displaying html
    By reddzer in forum Java Servlet
    Replies: 0
    Last Post: 11-10-2007, 07:20 PM
  5. Replies: 2
    Last Post: 06-12-2007, 04:33 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
  •