Results 1 to 2 of 2
  1. #1
    fezman1337 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default Quick applet question!!

    I have to make a racing game type applet where I import 2 pictures of a red and white car. Then when a go button is hit up top in the middle the 2 cars start inching their way to the right and then stops after a set amount of pixels. Using an example of a similar program using circles, I was able to write out most of the code and here is what I have so far:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Race extends JApplet implements Runnable, ActionListener{
    	private int pos1=0, pos2=0;
    	private Graphics buffer;
    	private Image offScreen;
    	private Thread runner;
    	private JButton go;
    	private ImageIcon [] img = new ImageIcon[2];
    
    	public void init() {
    		setLayout(new FlowLayout());
    		setBackground(Color.yellow);
    		go = new JButton("Go");
    		go.addActionListener(this);
    		add(go);
    		offScreen = createImage(getSize().width, getSize().height);
    		buffer = offScreen.getGraphics();
    		img[0] = new ImageIcon("whiteCar.gif");
    		img[1] = new ImageIcon("redCar.gif");
    
    	}
    
    	public int moveInc(){
    		return (int)Math.floor(5 * Math.random());
    	}
    
    	public void paint(Graphics g) {
    
    		img[0].paintIcon(this, g, pos1, 100);
    		img[1].paintIcon(this, g, pos2, 200);
    
    		if(pos1 > 300) {
    			pos1 = 301;
    			runner=null;
    		}if(pos2 > 300){
    			pos2 = 301;
    			runner=null;
    		}
    		g.drawImage(offScreen,0,0,this);
    	}
    		public void run() {
    			while(pos1 <= 300 && pos2 <= 300) {
    				pos1 = pos1 + moveInc();
    				pos2 = pos2 + moveInc();
    				try{
    					runner.sleep(15);
    				}catch(InterruptedException ie){}
    				repaint();
    			}
    		}
    
    		public void actionPerformed(ActionEvent ae) {
    			pos1=pos2=0;
    			if(runner == null) {
    				runner = new Thread(this);
    				runner.start();
    			}
    
    	}
    	public void update(Graphics g) {
    		paint(g);
    	}
    }
    I had the pictures showing up before but after I continued to add the code it is a blank yellow screen. Here is what the applet should look like (teacher hosted website) : http://www.brandensimbeck.com/School...ek13/Race.html

    Thanks in advance for any help!!!!
    Last edited by Eranga; 12-21-2010 at 03:19 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Quick Question...
    By FatalSylence in forum New To Java
    Replies: 4
    Last Post: 10-15-2010, 03:38 PM
  2. Really quick question
    By shadycharacter in forum New To Java
    Replies: 2
    Last Post: 04-22-2010, 11:06 PM
  3. A Quick Applet Question :)
    By smithywill in forum New To Java
    Replies: 1
    Last Post: 07-15-2009, 02:35 PM
  4. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 05:13 AM
  5. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 09:53 AM

Posting Permissions

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