#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Question Repaint() problem - not able to repaint part of the display after a loop


    Hey all,

    Java newb here - obviously. I've been able to hack out the code that follows. I know it's not pretty and I will make it more modular once I clear this hurdle that is driving me batty.

    Most everything is working - it displays the initial background, shows the spinning 'blurred images' (images 1-6 are the regular images, 7-12 are the blurred ones that are shown in the loop).

    I have shown the spots leading up to the issue with the comments with 4 asterisks... // **** and the repaint that is giving me fits begins with 6 asterisks // ******

    If you can spot why this last repaint is not working, you will earn my eternal thanks...

    In all seriousness, thank you just for looking and seeing if you can spot the problem.

    Thanks all,
    WD

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class main extends java.applet.Applet implements Runnable 
    {
            
            int totalPics = 12;
            Image[] img = new Image[totalPics];
            Image[] pic = new Image[3];
            Graphics g;
    		Image bgDisplay;
            Image bufferImage;
            Graphics bufferGraphics;
           	int coins = 50;
    		int winValue;   
        	int[] current = new int[3];
    		int c1,c2,c3;
        	Thread runner;
    		int spincount=0;
    		int spinRoll;
            boolean spin=false;
            boolean win=false;
    		
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++         
            public void init() {
    		setBackground(Color.white);
    				 
            
    		bufferImage = createImage(920, 500);
            bufferGraphics = bufferImage.getGraphics();
           
    		// read in images             
            for ( int i = 0; i < totalPics; i++) {
              img[i] = getImage(getCodeBase(), "slot"+ (i+1) +".jpg" ); 
            } 
            bgDisplay = getImage(getCodeBase(),"bgDisplay.jpg" );
            updateCoins();
    //       initialize pic[]
            for ( int i = 0; i < 3; i++) 
            {
            	pic[i] = img[5];
            } 
    	
    		
    }// end of init++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                    
                                  
    
    public final boolean mouseDown(Event evt,int x, int y) {//+++++++++++++++++++++++++++++
     // where did the user click?
    	// on the handle (spin)?
        if (((x >= 674) && (x <= 746))&& ((y >=152) && (y<=238))) {
        	coins--;
        	updateCoins();
          	spin=true;
            return true;
         }//end of if
        else if (((x >= 525) && (x <= 600))&& ((y >=155) && (y<=232))) {
                    // will be used for other areas
    
    				return true;
         	}//end of elseif
        else {return false;}
    }//end of mousedown++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
       
    //*******************************************************************************
    public final void paint(Graphics g) {
                   // double bufferImage the slot machine
    bufferGraphics.drawImage(bgDisplay,20,0,this);
    for (int w = 0; w < 3;w++) 
    {        
    	bufferGraphics.drawImage(img[current[w]],28+w*30+(122*(w+1)),193,this);
    }
    g.drawImage(bufferImage,0,0,this);
    }//end of paint()
    //******************************************************************************        
    
                                                   
    public final void update(Graphics g)  {//+++++++++++++++++++++++++++++++++++++++++++++
    	for (int w = 0; w < 3;w++) 
    	{        
    		bufferGraphics.drawImage(img[current[w]],28+w*30+(122*(w+1)),193,this);
    	}
    	g.drawImage(bufferImage,0,0,this);
    }// end of update++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
                    
    public final void start() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	bufferGraphics.drawImage(bgDisplay,20,0,this);
    		repaint();
    		try { Thread.sleep(320); }
        	catch (InterruptedException e) { }
    		    if (runner == null) {
                runner= new Thread(this);
                runner.start();
                }//end of if
    }// end of start+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
                    
    public final void mouseTrap() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
          try { Thread.sleep(75); }
        	catch (InterruptedException e) { }
    
    		
    }//end of mouseTrap
    
    
                                    
    public final void run() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            while(true){
    		       mouseTrap();
    			            if (spin==true) {
                        		winlose();
    			            	spin();
    							clearmessage();
    							coins += winValue;
    							message();
    							updateCoins();
                         		}//end of if
             }//end of while loop
    }//end of run++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
    
    
            
    public final void spin() 
    {//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	clearmessage();
    	updateCoins();
    	spincount++;
    	int p = 45;  // to pad the incrementer for deceleration of spin
        for (int i = 20; i <100; i++) 
        {
    
        	// temp stop
        	for (int Z = 0; Z < 3; Z++) 
        	{
        		current[Z] = (int)Math.floor(Math.random() * 6) + 6;  // shows the 'blurred images' simulating motion
    
        		if (Z == 0) c1 = current[Z];
        		if (Z == 1) c2 = current[Z];
        		if (Z == 2) c3 = current[Z];
    
            		pic[Z] = img[current[Z]];
         	}
      	
        	repaint();   // *** This repaint works fine 
        	spin = false;
        	try { Thread.sleep(p); }//pause for (P) amount of time
        	catch (InterruptedException e) {}
    	}//end of for loop
        
    
        
        winValue = 500;   // giving a winValue for testing
    
        	if (winValue == 500)   // big win
        	{	
    
        		pic[0] = img[5];  // **** trying to feed the final display
        		pic[1] = img[5];
        		pic[2] = img[5];
    
        	    win = true;
        	    repaint();    // ****** It seems this repaint is having zero effect - 
    
    
        	}
        	else if (winValue == 250)
        	{
    
        		c1 = c2 = c3 = 4;
        		pic[0] = pic[1] = pic[2] = img[4];
        	    repaint();
        	    win = true;
    
        	}
        	else
        	{
        	
        		win = false;
    
        	}
       
    
        
        return;
        
        
    }
    
    
    
    public final void message() {//++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	if(win == true) 
    	{
    	   	bufferGraphics.setColor(Color.yellow);
    		bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,35));
    		bufferGraphics.drawString("WINNER!",265,435);
    		bufferGraphics.drawString(winValue+" Coins!",265,475);
    	}
    	else 
    	{
    	   	bufferGraphics.setColor(Color.blue);
    		bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,35));
    		bufferGraphics.drawString("     Sorry",265,435);
    		bufferGraphics.drawString("Not a Winner",265,475);
    	}
    	repaint();
    }//end of message+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    public final void clearmessage() {//++++++++++++++++++++++++++++++++++++++++++++++++++++
         	bufferGraphics.setColor(Color.black); 
         	bufferGraphics.fillRect(265,410,200,35);
         	bufferGraphics.fillRect(265,450,250,35);
         	bufferGraphics.drawString("                                 ",265, 435);
         	bufferGraphics.drawString("                                 ",265, 475);
    	repaint();
    }//end of clearmessage+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    
    public final void updateCoins() {//++++++++++++++++++++++++++++++++++++++++++++++++++++        
    	  bufferGraphics.setColor(Color.black);  
          bufferGraphics.fillRect(60,427,150,30);
          bufferGraphics.setColor(Color.yellow);
          bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,32));
          bufferGraphics.drawString("  "+coins,168-15*(String.valueOf(coins).length()),450);
    	repaint(); // display the coin total
     }//end of update points+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            
    
    public final void winlose() 
    {
    
    
    	spinRoll = (int)Math.floor(Math.random()*10000);
    	if (spinRoll > 9995)
    	{
    		winValue = 500;
    		return;
    	} else if (spinRoll > 9990)
    	{
    		winValue = 250;
    		return;
    	} else if (spinRoll > 9980)
    	{
    		winValue = 100;
    		return;
    	} else if (spinRoll > 9960)
    	{
    		winValue = 50;
    		return;
    	} else if (spinRoll > 9910)
    	{
    		winValue = 20;
    		return;
    	} else if (spinRoll > 9810)
    	{
    		winValue = 10;
    		return;
    	} else if (spinRoll > 9510)
    	{
    		winValue = 5;
    		return;
    	} else if (spinRoll > 8710)
    	{
    		winValue = 2;
    		return;
    	} else
    	{
    		winValue = 0;
    		return;
    	}
    
             
    }//end of winlose()++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    
    public final void stop() {//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
                 if (runner != null) {
                 runner.stop();
                 runner = null;
                }//end of if
    }// end of stop++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                       
    }//end of class Slots
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    A call to the repaint() method asks the JVM to call the painting method when it gets around to it. It will probably be after the method where the repaint() method was called exits.

    Is the repaint() in question being executed and then does the next call to the paint() method do what was requested? Add some calls to the println method to print out messages so you can see in what order the methods are called.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    A call to the repaint() method asks the JVM to call the painting method when it gets around to it. It will probably be after the method where the repaint() method was called exits.

    Is the repaint() in question being executed and then does the next call to the paint() method do what was requested? Add some calls to the println method to print out messages so you can see in what order the methods are called.
    I can understand that the repaint() may not be executed right away, but after that method, it then waits for the user to click (goes into a loop until the mousedown is detected).... and it never would display that last repaint. I added a few printlns after the repaint and a few more after that method - and they ended up in the loop, so I saw the console printing out the printlns in a loop... but the window never updated the images.

    Any other ideas or how I could force it to update?

    Thanks
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    it never would display that last repaint.
    Can you explain what is not being displayed?

    I added a few printlns after the repaint
    How long after the println by the repaint() method printed was the paint() method executed and its println executed?

    What is supposed to happen when the code is executed? I see three images on a forth background image. Then what is supposed to happen?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Can you explain what is not being displayed?


    How long after the println by the repaint() method printed was the paint() method executed and its println executed?

    What is supposed to happen when the code is executed? I see three images on a forth background image. Then what is supposed to happen?
    Ok, what the code is a slot machine sim - there is a background image and the 3 wheels. Images 1-6 are the actual wheels and images 7-12 are the 'motion blurred' images so when they are being rotated, it makes it look like the wheels are spinning rapidly. When I click on the handle, the blurred wheels spin and after several seconds when the loop is completed, they stop. That is when I would like to display my final images (the ones that the odds calculated prior to spinning the images). Those final images are the ones that aren't displaying.

    After the spinning is complete, it does pass through the code where I try to get the code to display the specific images I want:
    Code:
       winValue = 500;   // giving a winValue for testing
    
        	if (winValue == 500)   // big win
        	{	
    
        		pic[0] = img[5];  // **** trying to feed the final display
        		pic[1] = img[5];
        		pic[2] = img[5];
    
        	    win = true;
        	    repaint();    // ****** It seems this repaint is having zero effect - 
    
    
        	}
    I have further code afterwards that does display that the player has 'won'. (I am currently rolling the odds and then injecting a specific value to display the 'big win - 500 coins').

    Not sure about your second question - where would I look to find that info? I am using Eclipse to write and test my code.

    Sorry, but as I said, I am familiar with some other languages and scripting, but really new to java.

    Thanks
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Those final images are the ones that aren't displaying.
    Are the "final images" those in the pic[] array? Where in the code are those images being displayed?

    I don't see a call to the println() method next to the repaint() method in the code that you posted. How do you know that the repaint method is executed?
    How do you know if the paint() method is called or not after the repaint() method is called?

    What is supposed to happen when the code is executed?
    I see three images on a fourth background image.
    Then what is supposed to happen?
    What do I need to do to see the problem?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Are the "final images" those in the pic[] array? Where in the code are those images being displayed?

    I don't see a call to the println() method next to the repaint() method in the code that you posted. How do you know that the repaint method is executed?
    How do you know if the paint() method is called or not after the repaint() method is called?

    What is supposed to happen when the code is executed?
    I see three images on a fourth background image.
    Then what is supposed to happen?
    What do I need to do to see the problem?
    To see what is going on, add the pics from this file to the bin directory or if you're using a different java compiler, wherever the .class file ends up:
    http://www.mediafire.com/download.php?7u1jww1aa3c62fc

    The file just contains 13 .jpgs - the 12 files that get loaded into the pic[] array. The display gets loaded as the background and the three pic[] array are the 3 wheels. They initially display images and then when the handle is clicked, it 'blurs' the wheels (by flashing images 7-12 rapidly) and then once completed, I have it checking to see what 'winnings' contains.

    Right now I only have the 500 and 250 values hard coded and once I get that working, I'll complete the rest. It is supposed to then display the 3 img[5] images for the 'big win' if the value of winnings = 500 - which I hard coded for testing.

    This is the code that I ran last time after I added the printlns - they don't effect the running of the program since they display in the console:

    (there are 6 printlns - I commented out the 3 outside of the method because they are in the main loop and would scroll the 3 inside the loop once the images stop 'spinning'.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class main extends java.applet.Applet implements Runnable 
    {
            
            int totalPics = 12;
            Image[] img = new Image[totalPics];
            Image[] pic = new Image[3];
            Graphics g;
    		Image bgDisplay;
            Image bufferImage;
            Graphics bufferGraphics;
           	int coins = 50;
    		int winValue;   
        	int[] current = new int[3];
    		int c1,c2,c3;
        	Thread runner;
    		int spincount=0;
    		int spinRoll;
            boolean spin=false;
            boolean win=false;
    		
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++         
            public void init() {
    		setBackground(Color.white);
    				 
            
    		bufferImage = createImage(920, 500);
            bufferGraphics = bufferImage.getGraphics();
           
    		// read in images             
            for ( int i = 0; i < totalPics; i++) {
              img[i] = getImage(getCodeBase(), "slot"+ (i+1) +".jpg" ); 
            } 
            bgDisplay = getImage(getCodeBase(),"bgDisplay.jpg" );
            updateCoins();
    //       initialize pic[]
            for ( int i = 0; i < 3; i++) 
            {
            	pic[i] = img[5];
            } 
    	
    		
    }// end of init++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                    
                                  
    
    public final boolean mouseDown(Event evt,int x, int y) {//+++++++++++++++++++++++++++++
     // where did the user click?
    	// on the handle (spin)?
        if (((x >= 674) && (x <= 746))&& ((y >=152) && (y<=238))) {
        	coins--;
        	updateCoins();
          	spin=true;
            return true;
         }//end of if
        else if (((x >= 525) && (x <= 600))&& ((y >=155) && (y<=232))) {
                    // will be used for other areas
    
    				return true;
         	}//end of elseif
        else {return false;}
    }//end of mousedown++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
       
    //*******************************************************************************
    public final void paint(Graphics g) {
                   // double bufferImage the slot machine
    bufferGraphics.drawImage(bgDisplay,20,0,this);
    for (int w = 0; w < 3;w++) 
    {        
    	bufferGraphics.drawImage(img[current[w]],28+w*30+(122*(w+1)),193,this);
    }
    g.drawImage(bufferImage,0,0,this);
    }//end of paint()
    //******************************************************************************        
    
                                                   
    public final void update(Graphics g)  {//+++++++++++++++++++++++++++++++++++++++++++++
    	for (int w = 0; w < 3;w++) 
    	{        
    		bufferGraphics.drawImage(img[current[w]],28+w*30+(122*(w+1)),193,this);
    	}
    	g.drawImage(bufferImage,0,0,this);
    }// end of update++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
                    
    public final void start() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	bufferGraphics.drawImage(bgDisplay,20,0,this);
    		repaint();
    		try { Thread.sleep(320); }
        	catch (InterruptedException e) { }
    		    if (runner == null) {
                runner= new Thread(this);
                runner.start();
                }//end of if
    }// end of start+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
                    
    public final void mouseTrap() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
          try { Thread.sleep(75); }
        	catch (InterruptedException e) { }
    
    		
    }//end of mouseTrap
    
    
                                    
    public final void run() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            while(true){
    		       mouseTrap();
    			            if (spin==true) {
                        		winlose();
    			            	spin();
    							clearmessage();
    							coins += winValue;
    							message();
    							updateCoins();
                         		}//end of if
    
    //						System.out.println("out- Hello ...");   
    //			    	    System.out.println("out- Hello !");
    //			    	    System.out.println("out- Last Hello after Method");
             }//end of while loop
    }//end of run++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
    
    
            
    public final void spin() 
    {//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	clearmessage();
    	updateCoins();
    	spincount++;
    	int p = 45;  // to pad the incrementer for deceleration of spin
        for (int i = 20; i <100; i++) 
        {
    
        	// temp stop
        	for (int Z = 0; Z < 3; Z++) 
        	{
        		current[Z] = (int)Math.floor(Math.random() * 6) + 6;  // shows the 'blurred images' simulating motion
    
        		if (Z == 0) c1 = current[Z];
        		if (Z == 1) c2 = current[Z];
        		if (Z == 2) c3 = current[Z];
    
            		pic[Z] = img[current[Z]];
         	}
      	
        	repaint();   // *** This repaint works fine 
        	spin = false;
        	try { Thread.sleep(p); }//pause for (P) amount of time
        	catch (InterruptedException e) {}
    	}//end of for loop
        
    
        
        winValue = 500;   // giving a winValue for testing
    
        	if (winValue == 500)   // big win
        	{	
    
        		pic[0] = img[5];  // **** trying to feed the final display
        		pic[1] = img[5];
        		pic[2] = img[5];
    
        	    win = true;
        	    repaint();    // ****** It seems this repaint is having zero effect - 
    
        	    System.out.println("Hello ...");   
        	    System.out.println("Hello !");
        	    System.out.println("Last Hello from Method");
    
        	}
        	else if (winValue == 250)
        	{
    
        		c1 = c2 = c3 = 4;
        		pic[0] = pic[1] = pic[2] = img[4];
        	    repaint();
        	    win = true;
    
        	}
        	else
        	{
        	
        		win = false;
    
        	}
       
    
        
        return;
        
        
    }
    
    
    
    public final void message() {//++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	if(win == true) 
    	{
    	   	bufferGraphics.setColor(Color.yellow);
    		bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,35));
    		bufferGraphics.drawString("WINNER!",265,435);
    		bufferGraphics.drawString(winValue+" Coins!",265,475);
    	}
    	else 
    	{
    	   	bufferGraphics.setColor(Color.blue);
    		bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,35));
    		bufferGraphics.drawString("     Sorry",265,435);
    		bufferGraphics.drawString("Not a Winner",265,475);
    	}
    	repaint();
    }//end of message+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    public final void clearmessage() {//++++++++++++++++++++++++++++++++++++++++++++++++++++
         	bufferGraphics.setColor(Color.black); 
         	bufferGraphics.fillRect(265,410,200,35);
         	bufferGraphics.fillRect(265,450,250,35);
         	bufferGraphics.drawString("                                 ",265, 435);
         	bufferGraphics.drawString("                                 ",265, 475);
    	repaint();
    }//end of clearmessage+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    
    public final void updateCoins() {//++++++++++++++++++++++++++++++++++++++++++++++++++++        
    	  bufferGraphics.setColor(Color.black);  
          bufferGraphics.fillRect(60,427,150,30);
          bufferGraphics.setColor(Color.yellow);
          bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,32));
          bufferGraphics.drawString("  "+coins,168-15*(String.valueOf(coins).length()),450);
    	repaint(); // display the coin total
     }//end of update points+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            
    
    public final void winlose() 
    {
    
    
    	spinRoll = (int)Math.floor(Math.random()*10000);
    	if (spinRoll > 9995)
    	{
    		winValue = 500;
    		return;
    	} else if (spinRoll > 9990)
    	{
    		winValue = 250;
    		return;
    	} else if (spinRoll > 9980)
    	{
    		winValue = 100;
    		return;
    	} else if (spinRoll > 9960)
    	{
    		winValue = 50;
    		return;
    	} else if (spinRoll > 9910)
    	{
    		winValue = 20;
    		return;
    	} else if (spinRoll > 9810)
    	{
    		winValue = 10;
    		return;
    	} else if (spinRoll > 9510)
    	{
    		winValue = 5;
    		return;
    	} else if (spinRoll > 8710)
    	{
    		winValue = 2;
    		return;
    	} else
    	{
    		winValue = 0;
    		return;
    	}
    
             
    }//end of winlose()++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    
    public final void stop() {//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
                 if (runner != null) {
                 runner.stop();
                 runner = null;
                }//end of if
    }// end of stop++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                       
    }//end of class Slots
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I missed where you answered these questions:
    1)Are the "final images" those in the pic[] array?
    2)Where in the code are those images being displayed?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    I missed where you answered these questions:
    1)Are the "final images" those in the pic[] array?
    2)Where in the code are those images being displayed?
    1) Yes, I assigned the final values into the pic[] array

    2) here:

    Code:
    winValue = 500;   // giving a winValue for testing
    
        	if (winValue == 500)   // big win
        	{	
    
        		pic[0] = img[5];  // **** trying to feed the final display
        		pic[1] = img[5];
        		pic[2] = img[5];
    
        	    win = true;
        	    repaint();    // ****** It seems this repaint is having zero effect - 
    
    
        	}
    After the fake motion loop, I want to be able to assign the final values for pic[0], pic[1] and pic[2] and display them.

    What I am doing is rather than spinning the 3 wheels and then determining if I have a winner (which would produce hugely wrong odds), I am determining whether it's a winner before the wheels ever spin. Then after I spin the wheels, I want to assign the final wheel pics.

    The code above is where I was testing with just the one value. Once I can determine what isn't working with that, then I can complete the rest.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I do not see any println() calls in the methods: paint() or update()
    How do you know that the call to repaint() is not creating a later call to either of those methods?

    final values for pic[0], pic[1] and pic[2] and display them.
    Where are the contents of the pic[] array being drawn on the screen so they can be seen? If there is no call to a drawing method using the images in the pic[] array, those images will NOT be seen on the screen.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    I do not see any println() calls in the methods: paint() or update()
    How do you know that the call to repaint() is not creating a later call to either of those methods?


    Where are the contents of the pic[] array being drawn on the screen so they can be seen? If there is no call to a drawing method using the images in the pic[] array, those images will NOT be seen on the screen.
    Ok, all I did was cut from a section before I added them, but I see more of what you're asking so I updated the full code to what I have below.

    Note: When it is run, I see the paint, I see the update run several times and then pauses before I click the handle. When I click the handle and the blurred images are repainted rapidly, obviously the update is called numerous times.

    At the end of the loop, you can see if pass through the last repaint and then several more updates (the coins update, the Winner Text update etc):

    The last several lines from the console:
    In the update
    Before last repaint
    after last repaint
    In the update
    In the update
    In the update
    In the update
    In the update
    In the update
    In the update
    In the update
    In the update

    As far as your last question - if there is something I am missing, by all means, PLEASE tell me and I'll try adding it.


    Updated code:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class main extends java.applet.Applet implements Runnable 
    {
            
            int totalPics = 12;
            Image[] img = new Image[totalPics];
            Image[] pic = new Image[3];
            Graphics g;
    		Image bgDisplay;
            Image bufferImage;
            Graphics bufferGraphics;
           	int coins = 50;
    		int winValue;   
        	int[] current = new int[3];
    		int c1,c2,c3;
        	Thread runner;
    		int spincount=0;
    		int spinRoll;
            boolean spin=false;
            boolean win=false;
    		
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++         
            public void init() {
    		setBackground(Color.white);
    				 
            
    		bufferImage = createImage(920, 500);
            bufferGraphics = bufferImage.getGraphics();
           
    		// read in images             
            for ( int i = 0; i < totalPics; i++) {
              img[i] = getImage(getCodeBase(), "slot"+ (i+1) +".jpg" ); 
            } 
            bgDisplay = getImage(getCodeBase(),"bgDisplay.jpg" );
            updateCoins();
    //       initialize pic[]
            for ( int i = 0; i < 3; i++) 
            {
            	pic[i] = img[5];
            } 
    	
    		
    }// end of init++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                    
                                  
    
    public final boolean mouseDown(Event evt,int x, int y) {//+++++++++++++++++++++++++++++
     // where did the user click?
    	// on the handle (spin)?
        if (((x >= 674) && (x <= 746))&& ((y >=152) && (y<=238))) {
        	coins--;
        	updateCoins();
          	spin=true;
            return true;
         }//end of if
        else if (((x >= 525) && (x <= 600))&& ((y >=155) && (y<=232))) {
                    // will be used for other areas
    
    				return true;
         	}//end of elseif
        else {return false;}
    }//end of mousedown++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
       
    //*******************************************************************************
    public final void paint(Graphics g) {
                   // double bufferImage the slot machine
    bufferGraphics.drawImage(bgDisplay,20,0,this);
    for (int w = 0; w < 3;w++) 
    {        
    	bufferGraphics.drawImage(img[current[w]],28+w*30+(122*(w+1)),193,this);
        System.out.println("In the paint");
    }
    g.drawImage(bufferImage,0,0,this);
    }//end of paint()
    //******************************************************************************        
    
                                                   
    public final void update(Graphics g)  {//+++++++++++++++++++++++++++++++++++++++++++++
    	for (int w = 0; w < 3;w++) 
    	{        
    		bufferGraphics.drawImage(img[current[w]],28+w*30+(122*(w+1)),193,this);
    	    System.out.println("In the update");
    	}
    	g.drawImage(bufferImage,0,0,this);
    }// end of update++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
                    
    public final void start() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	bufferGraphics.drawImage(bgDisplay,20,0,this);
    		repaint();
    		try { Thread.sleep(320); }
        	catch (InterruptedException e) { }
    		    if (runner == null) {
                runner= new Thread(this);
                runner.start();
                }//end of if
    }// end of start+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
                    
    public final void mouseTrap() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
          try { Thread.sleep(75); }
        	catch (InterruptedException e) { }
    
    		
    }//end of mouseTrap
    
    
                                    
    public final void run() {//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            while(true){
    		       mouseTrap();
    			            if (spin==true) {
                        		winlose();
    			            	spin();
    							clearmessage();
    							coins += winValue;
    							message();
    							updateCoins();
                         		}//end of if
    
    //						System.out.println("out- Hello ...");   
    //			    	    System.out.println("out- Hello !");
    //			    	    System.out.println("out- Last Hello after Method");
             }//end of while loop
    }//end of run++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
    
    
            
    public final void spin() 
    {//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	clearmessage();
    	updateCoins();
    	spincount++;
    	int p = 45;  // to pad the incrementer for deceleration of spin
        for (int i = 20; i <100; i++) 
        {
    
        	// temp stop
        	for (int Z = 0; Z < 3; Z++) 
        	{
        		current[Z] = (int)Math.floor(Math.random() * 6) + 6;  // shows the 'blurred images' simulating motion
    
        		if (Z == 0) c1 = current[Z];
        		if (Z == 1) c2 = current[Z];
        		if (Z == 2) c3 = current[Z];
    
            		pic[Z] = img[current[Z]];
         	}
      	
        	repaint();   // *** This repaint works fine 
        	spin = false;
        	try { Thread.sleep(p); }//pause for (P) amount of time
        	catch (InterruptedException e) {}
    	}//end of for loop
        
    
        
        winValue = 500;   // giving a winValue for testing
    
        	if (winValue == 500)   // big win
        	{	
    
        		pic[0] = img[5];  // **** trying to feed the final display
        		pic[1] = img[5];
        		pic[2] = img[5];
    
        	    win = true;
        	    
        	    System.out.println("Before last repaint");
        	    repaint();  // ****** It seems this repaint is having no effect - 
        	    System.out.println("after last repaint");
    
        	}
        	else if (winValue == 250)
        	{
    
        		c1 = c2 = c3 = 4;
        		pic[0] = pic[1] = pic[2] = img[4];
        	    repaint();
        	    win = true;
    
        	}
        	else
        	{
        	
        		win = false;
    
        	}
       
    
        
        return;
        
        
    }
    
    
    
    public final void message() {//++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    	if(win == true) 
    	{
    	   	bufferGraphics.setColor(Color.yellow);
    		bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,35));
    		bufferGraphics.drawString("WINNER!",265,435);
    		bufferGraphics.drawString(winValue+" Coins!",265,475);
    	}
    	else 
    	{
    	   	bufferGraphics.setColor(Color.blue);
    		bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,35));
    		bufferGraphics.drawString("     Sorry",265,435);
    		bufferGraphics.drawString("Not a Winner",265,475);
    	}
    	repaint();
    }//end of message+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    public final void clearmessage() {//++++++++++++++++++++++++++++++++++++++++++++++++++++
         	bufferGraphics.setColor(Color.black); 
         	bufferGraphics.fillRect(265,410,200,35);
         	bufferGraphics.fillRect(265,450,250,35);
         	bufferGraphics.drawString("                                 ",265, 435);
         	bufferGraphics.drawString("                                 ",265, 475);
    	repaint();
    }//end of clearmessage+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    
    public final void updateCoins() {//++++++++++++++++++++++++++++++++++++++++++++++++++++        
    	  bufferGraphics.setColor(Color.black);  
          bufferGraphics.fillRect(60,427,150,30);
          bufferGraphics.setColor(Color.yellow);
          bufferGraphics.setFont(new Font("TimesRoman",Font.BOLD,32));
          bufferGraphics.drawString("  "+coins,168-15*(String.valueOf(coins).length()),450);
    	repaint(); // display the coin total
     }//end of update points+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            
    
    public final void winlose() 
    {
    
    
    	spinRoll = (int)Math.floor(Math.random()*10000);
    	if (spinRoll > 9995)
    	{
    		winValue = 500;
    		return;
    	} else if (spinRoll > 9990)
    	{
    		winValue = 250;
    		return;
    	} else if (spinRoll > 9980)
    	{
    		winValue = 100;
    		return;
    	} else if (spinRoll > 9960)
    	{
    		winValue = 50;
    		return;
    	} else if (spinRoll > 9910)
    	{
    		winValue = 20;
    		return;
    	} else if (spinRoll > 9810)
    	{
    		winValue = 10;
    		return;
    	} else if (spinRoll > 9510)
    	{
    		winValue = 5;
    		return;
    	} else if (spinRoll > 8710)
    	{
    		winValue = 2;
    		return;
    	} else
    	{
    		winValue = 0;
    		return;
    	}
    
             
    }//end of winlose()++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    
    
    public final void stop() {//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
                 if (runner != null) {
                 runner.stop();
                 runner = null;
                }//end of if
    }// end of stop++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                       
    }//end of class Slots
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    How much of the code for this program did you write?
    You don't know what controls which image(s) are displayed.

    Look at the args to the drawImage() method that displays the images.
    Add this to the arg for the println() method called in the paint and update methods:
    Code:
    +  " current="+java.util.Arrays.toString(current)
    obviously the update is called numerous times.
    The reason for so many printouts is because the call to println is INSIDE of a loop.
    Move the call to println OUTSIDE of the loop to be the first statement in the method so it prints only ONE time when the method is called, not each time the loop goes around.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    How much of the code for this program did you write?
    You don't know what controls which image(s) are displayed.

    Look at the args to the drawImage() method that displays the images.
    Add this to the arg for the println() method called in the paint and update methods:
    Code:
    +  " current="+java.util.Arrays.toString(current)

    The reason for so many printouts is because the call to println is INSIDE of a loop.
    Move the call to println OUTSIDE of the loop to be the first statement in the method so it prints only ONE time when the method is called, not each time the loop goes around.
    I learn mostly by example so I rewrote almost all of it from similar examples - and yes, I understood the drawimages are what's actually doing the paint and updates (repaint).

    The last printlns now are:
    In the update current=[7, 8, 9]
    Before last repaint
    after last repaint
    In the update current=[7, 8, 9]
    In the update current=[7, 8, 9]

    Of course, each time it's run, the numbers change since they're random.
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    What is printed out when you click the mouse in the "Hot zone" that makes the "wheels" spin?

    You have never said what the pic[] array is used for. Can you explain why you are saving references to images in that array if it is never used for anything.

    Did the print out of the contents of the current array give you any ideas on how to control what images are displayed (instead of using the pic[] array)?

IMN logo majestic logo threadwatch logo seochat tools logo