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

    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());
    		go = new JButton("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;
    		}if(pos2 > 300){
    			pos2 = 301;
    		public void run() {
    			while(pos1 <= 300 && pos2 <= 300) {
    				pos1 = pos1 + moveInc();
    				pos2 = pos2 + moveInc();
    				}catch(InterruptedException ie){}
    		public void actionPerformed(ActionEvent ae) {
    			if(runner == null) {
    				runner = new Thread(this);
    	public void update(Graphics 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) :

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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power

Similar Threads

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