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

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-15-2012, 03:14 AM   PM User | #1
pineappelle
New Coder

 
Join Date: Jun 2011
Posts: 34
Thanks: 1
Thanked 0 Times in 0 Posts
pineappelle is an unknown quantity at this point
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 03:15 AM.. Reason: ball,explode,computer,programming,kitsilano
pineappelle is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 07:32 AM.


Advertisement
Log in to turn off these ads.