Results 1 to 2 of 2
Thread: Quick applet question!!
- 12-20-2010, 04:38 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
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:
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.htmlJava 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); } }
Thanks in advance for any help!!!!Last edited by Eranga; 12-21-2010 at 02:19 AM. Reason: code tags added
- 12-21-2010, 02:22 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags when you are posting again. Unformated codes are really hard to read.
Similar Threads
-
Quick Question...
By FatalSylence in forum New To JavaReplies: 4Last Post: 10-15-2010, 02:38 PM -
Really quick question
By shadycharacter in forum New To JavaReplies: 2Last Post: 04-22-2010, 10:06 PM -
A Quick Applet Question :)
By smithywill in forum New To JavaReplies: 1Last Post: 07-15-2009, 01:35 PM -
Hello everyone! quick question.
By irishhokie in forum New To JavaReplies: 5Last Post: 04-03-2009, 04:13 AM -
One last quick question
By jigglywiggly in forum New To JavaReplies: 7Last Post: 01-26-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks