Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2010, 04:19 PM   PM User | #1
earni23
New Coder

 
Join Date: Feb 2010
Posts: 95
Thanks: 14
Thanked 0 Times in 0 Posts
earni23 has a little shameless behaviour in the past
Applet code works in paint(...) but not in init()

Hi!
Why does the following code work :
Code:
    public class Game extends Applet
    {
       public void init() {}

       public void paint(Graphics g)
       {
           Image img = getImage(getCodeBase(), "Ball.png");
           BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
           Graphics2D g2 = bi.createGraphics();
           g2.drawImage(img, 50, 50, this);
           g.drawImage(bi, 0, 0, this);
       }
   }
But not this:
Code:
public class Game extends Applet
{
    private BufferedImage bi = null;

    public void init()
    {
        Image img = getImage(getCodeBase(), "Ball.png");
        bi = new BufferedImage(getWidth(), getHeight(),    BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bi.createGraphics();
        g2.drawImage(img, 50, 50, this);
    }

    public void paint(Graphics g)
    {
        
        g.drawImage(bi, 0, 0, this);
    }
}
earni23 is offline   Reply With Quote
Old 09-28-2010, 06:23 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
More or less it's because of the applet lifecycle. init() is called when the applet is initialized, but that does not mean it is ready to display anything. Typically paint() is called automatically when the applet is ready on the screen to render. Nothing can be displayed before that point so putting that code in the init() phase has no purpose.
brad211987 is offline   Reply With Quote
Old 09-28-2010, 06:59 PM   PM User | #3
earni23
New Coder

 
Join Date: Feb 2010
Posts: 95
Thanks: 14
Thanked 0 Times in 0 Posts
earni23 has a little shameless behaviour in the past
My code doesn´t display anything in init(), it only assigns the buffered image(bi). You should be able to assign values when initializing the applet or have I missed something.
earni23 is offline   Reply With Quote
Old 10-07-2010, 05:29 AM   PM User | #4
sujithpr
New Coder

 
Join Date: Aug 2009
Location: Cochin,India
Posts: 39
Thanks: 2
Thanked 1 Time in 1 Post
sujithpr is an unknown quantity at this point
May be because you are using Graphics object in the init() method..
init() start() and paint() are the life cycle methods of an applet are invoked automatically in that order.
All graphics related things are to be done in paint() methods ie. after applet is initialized and started.


Last edited by sujithpr; 10-07-2010 at 07:14 AM..
sujithpr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:19 PM.


Advertisement
Log in to turn off these ads.