Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 17th, 2013, 01:36 PM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Question Homework - 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  

Reply With Quote
  #2  
Old February 17th, 2013, 06:16 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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.

Reply With Quote
  #3  
Old February 18th, 2013, 10:23 AM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old February 18th, 2013, 10:51 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Quote:
it never would display that last repaint.

Can you explain what is not being displayed?

Quote:
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?

Reply With Quote
  #5  
Old February 18th, 2013, 11:10 AM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #6  
Old February 18th, 2013, 11:46 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Quote:
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?

Reply With Quote
  #7  
Old February 18th, 2013, 12:40 PM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Quote:
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  

Reply With Quote
  #8  
Old February 18th, 2013, 01:17 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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?

Reply With Quote
  #9  
Old February 18th, 2013, 01:45 PM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #10  
Old February 18th, 2013, 01:54 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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?

Quote:
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.

Reply With Quote
  #11  
Old February 18th, 2013, 02:31 PM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Quote:
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  


Reply With Quote
  #12  
Old February 18th, 2013, 02:46 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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)


Quote:
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.

Reply With Quote
  #13  
Old February 18th, 2013, 03:10 PM
WorkingDude WorkingDude is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 WorkingDude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 54 m 37 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #14  
Old February 18th, 2013, 03:21 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
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)?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Repaint() problem - not able to repaint part of the display after a loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap