Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How would i get these images to change?

    The code below changes the image to the next one but wont continue changing the image for all for of them. What am i doing wrong?

    Code:
    /**
     * @(#)chapter16random.java
     *
     * chapter16random Applet application
     *
     * @author
     * @version 1.00 2012/2/8
     */
    
    import java.awt.*;
    
    public class chapter17a extends java.applet.Applet implements Runnable {
    
    	Graphics offscreen;
    	Thread runner;
    	int left, top, xmove;
    	Image workspace;
    	Image[] ball;
    	int currentpicture;
    	MediaTracker tracker;
    
    	public void init() {
    
    		workspace=createImage(400,400);
    		ball=new Image[4];
    		offscreen=workspace.getGraphics();
    		left=10;
    		top=10;
    		xmove=1;
    		loadimages();
    		currentpicture=0;
    
    	}
    
    	public void loadimages() {
    
    		tracker=new MediaTracker(this);
    		ball[0]=getImage(getCodeBase(), "ball1.gif");
    		ball[1]=getImage(getCodeBase(), "ball2.gif");
    		ball[2]=getImage(getCodeBase(), "ball3.gif");
    		ball[3]=getImage(getCodeBase(), "ball4.gif");
    		tracker.addImage(ball[0],0);
    		tracker.addImage(ball[1],1);
    		tracker.addImage(ball[2],2);
    		tracker.addImage(ball[3],3);
    		try{tracker.waitForAll();
    		}
    		catch(InterruptedException e) {}
    
    	}
    
    	public void paint(Graphics screen) {
    
    		offscreen.setColor(Color.white);
    		offscreen.fillRect(0,0,400,400);
    		offscreen.setColor(Color.red);
    
    		if(currentpicture==0) {
    			offscreen.drawImage(ball[0],left,top,75,75,this);
    		}
    		else if(currentpicture==1){
    			offscreen.drawImage(ball[1],left,top,75,75,this);
    		}
    		else if(currentpicture==2) {
    			offscreen.drawImage(ball[2],left,top,75,75,this);
    		}
    		else {
    			offscreen.drawImage(ball[3],left,top,75,75,this);
    		}
    
    		screen.drawImage(workspace,0,0,400,400,this);
    
    	}
    
    	public void start() {
    
    		if(runner==null) {
    			runner=new Thread(this);
    		}
    		runner.start();
    
    	}
    
    	public void run() {
    
    		while(true) {
    			repaint();
    			checkedge();
    			left=left+xmove;
    			try {Thread.sleep(5); }
    			catch (InterruptedException e) {}
    		}
    
    	}
    
    	public void stop() {
    
    		if(runner!=null) {
    			runner.stop();
    			runner=null;
    		}
    
    	}
    
    	public void checkedge() {
    
    		if(left<0||left+75>400) {
    			currentpicture=1;
    			runner.suspend();
    			try {
    				Thread.sleep(10);
    			}
    			catch (InterruptedException e){}
    			runner.resume();
    			currentpicture=2;
    			currentpicture=3;
    			currentpicture=4;
    		}
    
    	}
    
    	public void update(Graphics screen) {
    		paint(screen);
    	}
    }
    Last edited by pineappelle; 02-15-2012 at 04:15 AM. Reason: ball,explode,computer,programming,kitsilano


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •