Results 1 to 10 of 10
  1. #1
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Implementing Condition within paint() method is not working

    Greetings EveryOne :D

    While starting to work with Applets, I tried this code and it works fine, the loaded image gets displayed:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class AppletExample extends Applet
    {
        Image IMG;
            
        public void init()
        {
            setSize(700, 457);
            
            IMG = getImage(getDocumentBase(), "kitten.jpg");
        }
        
        public void paint(Graphics G)
        {
            G.drawImage(IMG, 0, 0, this);
        }
    }
    But when adding an "if" statement to pain() method the image don't get displayed at all even that the code within "if" blocks gets executed, here is the code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class AppletExample extends Applet
    {
        Image IMG;
        
        boolean DrawImgForFirstTime = true;
        
        public void init()
        {
            setSize(700, 457);
            
            IMG = getImage(getDocumentBase(), "kitten.jpg");
        }
        
        public void paint(Graphics G)
        {
            if(DrawImgForFirstTime == true)
            {
                System.out.println("DrawImgForFirstTime = "+DrawImgForFirstTime);
                G.drawImage(IMG, 0, 0, this);
                DrawImgForFirstTime = false;
            }        
            
        }
    }
    Can someone tell me why the image doesn't get displayed in this case.

    Thank You In Advance.

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

    Default Re: Implementing Condition within paint() method is not working

    Why would you want to have that if in there in the first place? It looks like trouble to me as you don't have full control over when or if paint is called, and it can and often is called twice at the start up of your code, drawing and then erasing your image.

  3. #3
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Implementing Condition within paint() method is not working

    You are right Mr. Fubarable, we don't have control of how many times paint() is called before the image is revealed on the screen, I realized that after doing this experiment:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class AppletExample extends Applet
    {
        int count;
        Image IMG;
        
        boolean DrawImgForFirstTime = true;
        
        public void init()
        {
            setSize(700, 457);
            
            IMG = getImage(getDocumentBase(), "kitten.jpg");
        }
        
        public void paint(Graphics G)
        {
            if(count <= 7)
            {
                count++;
                System.out.println("count = "+count);
                G.drawImage(IMG, 0, 0, this);
            }        
            
        }
    }
    You may never guess after how many times "count" gets incremented before the image show up.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Implementing Condition within paint() method is not working

    Check which dimensions your image at the beginning have (i guess -1 and -1) so drawImage will return false --> not painted, but you set the DrawImageForFirstTime to false! (paint will be invoked many times....)
    ->
    Java Code:
    		if (DrawImgForFirstTime) {
    			if(G.drawImage(IMG, 0, 0, this)){
    				System.out.println("DrawImgForFirstTime = " + DrawImgForFirstTime);
    				DrawImgForFirstTime = false;				
    			}
    		}
    could mybe solve your problem

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

    Default Re: Implementing Condition within paint() method is not working

    eRaaaa: even so, why would one want to draw the image only once in that way? It almost smells as if the original poster is trying to have program logic inside of his paint method, something that should never be done.

  6. #6
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Implementing Condition within paint() method is not working

    Thank You, Thank you Mr. eRaaaa, your approach does solve the problem indeed.
    I would like some more details why it behaves that way, maybe a link or something to read more about this for a deeper understand to avoid this kind of problem in the future. Thank you again Mr. eRaaaa ;)

  7. #7
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Implementing Condition within paint() method is not working

    eRaaaa: even so, why would one want to draw the image only once in that way? It almost smells as if the original poster is trying to have program logic inside of his paint method, something that should never be done.
    Mr. Fubarable, Would you please explain why having a program logic inside paint method should never be done, Please

    Thank You.

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

    Default Re: Implementing Condition within paint() method is not working

    Quote Originally Posted by Shikatsu View Post
    Mr. Fubarable, Would you please explain why having a program logic inside paint method should never be done, Please
    It would help greatly if you were to tell us what you're trying to achieve with this bit of code.

    The paint method (or its Swing equivalent, paintComponent) is for painting only since you don't have much control over when it is being called. If you need to have your program display an image if it is in one state, and not display it if it is in another state, then the changing of state should be done in a non-paint method, and then the paint method will respond (usually in an if block) to that state.

  9. #9
    Shikatsu is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Implementing Condition within paint() method is not working

    It would help greatly if you were to tell us what you're trying to achieve with this bit of code.
    I started reading about double-buffering to solve flickering, in this tutorial:
    Applet Tutorial: Backbuffers

    In Backbuffer2 class no graphics are displayed until you move the mouse within the applet:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Math;
    
    public class Backbuffer2 extends Applet
       implements MouseMotionListener {
    
       int width, height;
       int mx, my;  // the mouse coordinates
       Point[] points;
       int N = 300;
       Image img;
       Image backbuffer;
       Graphics backg;
    
       public void init() {
          width = getSize().width;
          height = getSize().height;
    
          mx = width/2;
          my = height/2;
    
          points = new Point[ N ];
          for ( int i = 0; i < N; ++i ) {
             int x = (int)(( Math.random() - 0.5 ) * width / 1.5);
             int y = (int)(( Math.random() - 0.5 ) * height / 1.5);
             points[i] = new Point( x, y );
          }
    
          img = getImage(getDocumentBase(), "fractal.gif");
    
          backbuffer = createImage( width, height );
          backg = backbuffer.getGraphics();
          backg.setColor( Color.white );
    
          addMouseMotionListener( this );
       }
    
       public void mouseMoved( MouseEvent e ) {
          mx = e.getX();
          my = e.getY();
          showStatus( "Mouse at (" + mx + "," + my + ")" );
    
          backg.drawImage( img, 0, 0, this );
          for ( int j = 1; j < N; ++j ) {
             Point A = points[j-1];
             Point B = points[j];
             backg.drawLine( mx+A.x, my+A.y, mx+B.x, my+B.y );
          }
    
          repaint();
          e.consume();
       }
       public void mouseDragged( MouseEvent e ) { }
    
       public void update( Graphics g ) {
          g.drawImage( backbuffer, 0, 0, this );
       }
    
       public void paint( Graphics g ) {
          update( g );
       }
    }
    In a board game for example you need an initial state of the graphics to be displayed, after eRaaaa hint, this is how it's solved:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Math.*;
    
    public class Backbuff3 extends Applet implements MouseMotionListener
    {
        int Width, Height;
        int MouseX, MouseY;
        Point Points[];
        int N = 300;
        Image IMG;
        Image  BackBuffer;
        Graphics BackGraph;
        
        boolean DrawImgForFirstTime = true;
        
        @Override
        public void init()
        {
            setSize(700, 457);
            Width  = getSize().width;
            Height = getSize().height;
            
            MouseX = Width/2;
            MouseY = Height/2;
            
            Points = new Point[N];
            
            for(int i = 0; i<N; ++i)
            {
                int x = (int)((Math.random()-0.5)*Width/1.5);
                int y = (int)((Math.random()-0.5)*Height/1.5);
                
                Points[i] = new Point(x,y);
            }
            
            // Load the image "kitten.jpg" from the same
            // directory that the applet resides in.
            IMG = getImage(getDocumentBase(), "kitten.jpg");
            
            BackBuffer = createImage(Width, Height);
            BackGraph      = BackBuffer.getGraphics();
            BackGraph.setColor(Color.white);
            
            MouseX = 100;
            MouseX = 100;
            
            for(int i = 1; i<N; ++i)
            {
                Point A = Points[i-1];
                Point B = Points[i];
                
                BackGraph.drawLine(MouseX+A.x, MouseY+A.y, MouseX+B.x, MouseY+B.y);
            }
            
            addMouseMotionListener(this);
            repaint();
        }
        
        @Override
        public void mouseMoved(MouseEvent ME)
        {
            MouseX = ME.getX();
            MouseY = ME.getY();
            
            BackGraph.drawImage(IMG, 0, 0, this);
            
            for(int i = 1; i<N; ++i)
            {
                Point A = Points[i-1];
                Point B = Points[i];
                
                BackGraph.drawLine(MouseX+A.x, MouseY+A.y, MouseX+B.x, MouseY+B.y);
            }
            
            showStatus("Mouse at ("+MouseX+","+MouseY+")");
            repaint();
            ME.consume();
        }
        
        @Override
        public void mouseDragged(MouseEvent ME) {}
        
        @Override
        public void update(Graphics G)
        {
            G.drawImage(BackBuffer, 0, 0, this);
            
            // Added this to be the intial state 
            // of the graphics that will be drawn,
            // as you can see this gets executed only once
            // as it should be (thanks to "eRaaaa"), 
            // otherwise its incontinent;
            if (DrawImgForFirstTime) 
            {
                if(G.drawImage(IMG, 0, 0, this))
                {
                    MouseX = Width/2;
                    MouseY = Height/2;
                    
                    G.drawImage(BackBuffer, 0, 0, this);
            
                    G.drawImage(IMG, 0, 0, this);
                    
                    G.setColor(Color.white);
                    
                    for(int i = 1; i<N; ++i)
                    {
                        Point A = Points[i-1];
                        Point B = Points[i];
                        
                        G.drawLine(MouseX+A.x, MouseY+A.y, MouseX+B.x, MouseY+B.y);
                    }
                    System.out.println("DrawImgForFirstTime = " + DrawImgForFirstTime);
                    DrawImgForFirstTime = false;
                }
            }
        }
        
        @Override
        public void paint(Graphics G)
        {
            update(G);
        }
    }
    I don't say it's the best way to go and I still have a lot to learn, so is there a better way to approach this?
    Any advice and/or readings you can suggest?

    Thank You For taking the time to help, its very appreciated.

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

    Default Re: Implementing Condition within paint() method is not working

    Ah, now you're getting into the intricacies of AWT Graphics, something I know little or less about. Please ignore my posts above!

Similar Threads

  1. paint method
    By larry_d1990 in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2011, 06:45 PM
  2. Using Graphics outside of a paint method
    By Catfish1 in forum New To Java
    Replies: 6
    Last Post: 10-26-2010, 09:57 PM
  3. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 08:07 AM
  4. Java Paint Method?
    By leapinlizard in forum Java 2D
    Replies: 2
    Last Post: 02-11-2010, 08:01 PM
  5. an error in paint method
    By hopey in forum Java 2D
    Replies: 7
    Last Post: 04-24-2009, 11:12 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
  •