Inserting an image and swapping with drawn image
What i am trying to do is edit one of my original applets.
I have two cars racing that i drew in the applet. What i want to do is replace the drawn images with an inserted image.
I figured out how to insert the image, but how can I swap the created image and .gif easily? I know there has to be an easy way to do this without altering all of the code...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
publicclass Kevin_McConnaughey_211extends Appletimplements ActionListener
{
int flag1 = 0;
int n11=0, n22=0, r=0, q=0, tree=110;
Button race =new Button ("Go!");
Thread th =new Thread();
Color dbrown =new Color(128,64,0);//creates new colors
Color rdbrown =new Color(92,65,35);
Color dgreen =new Color(22,105,49);
Image image;
boolean ready =false;
publicvoid init()
{
setLayout(null);//sets up the button
race.addActionListener(this);
race.setBounds (380,555,50,40);//75,40
add(race);
image = getImage(getCodeBase(),"car.gif");
ready =true;
repaint();
//background = getAudioClip(getCodeBase(), "audio/spacemusic.au");
}
publicvoid start()
{
flag1 = 1;
setSize(800,600);
//background.loop();
}
publicvoid paint (Graphics g)
{
g.setColor (dbrown);//colors background
g.fillRect(0,350,900,900);
g.setColor (Color.black);
g.fillRect(0,0,900,350);
g.setColor (Color.white);
for (int pd=1;pd<400;pd++)
{
n11=1+(int) (Math.random()*800);//creates stars
n22=1+(int) (Math.random()*325);
g.fillOval (n11,n22,2,2);
}
g.setColor(Color.white);//draws eclipsed moon behind the stars
for (int mm=0;mm<=5;mm++)
g.drawOval(75+mm*1,50,75,75);
g.setColor(Color.black);//creates road
g.fillRect(0,400,900,900);
g.setColor(dbrown);//creates land
g.fillRect(0,550,900,900);
int x4points[] ={40,40,65,65,105,53,0};//polygon for trees
int y4points[] ={310,350,350,310,310,170,310};
for (int tre=0;tre<=7;tre++)//creates trees
{
g.setColor(dgreen);
g.fillPolygon(x4points,y4points,7);
g.setColor(rdbrown);
g.fillRect(40+tree*tre,310,25,40);
g.setColor(Color.black);
g.drawRect(40+tree*tre,310,25,40);
for (int ta=0;ta<=6;ta++)
x4points[ta] = x4points[ta] + tree;
}
for (int ts=0;ts<=6;ts++)
x4points[ts] = x4points[ts] +0;
g.setColor(Color.white);//draws line for the road
for (int m=1;m<=5;m++)
g.fillRect(0,465,900,10);
if (ready){//draws the car at the top left corner of the screen
g.drawImage(image,0,10,this);
}
if (flag1 == 2) race(g);
}
publicvoid race(Graphics rr)//starts race
{
int x[]={40,60,60,65,65,70,80,80,80,80,75,75,65,65,55,55,30,30,25,25,15,15,15,15,30,30,35,35};
int y[]={450,450,455,455,450,450,450,450,440,440,435,435,435,435,425,425,425,425,435,435,435,435,450,450,450,455,455,450};
int x1[]={40,60,60,65,65,70,80,80,80,80,75,75,65,65,55,55,30,30,25,25,15,15,15,15,30,30,35,35};
int y1[]={525,525,530,530,525,525,525,525,515,515,510,510,510,510,500,500,500,500,510,510,510,510,525,525,525,530,530,525};
rr.setXORMode(Color.white);
rr.setColor(Color.black);
for (int a=0;a<=150;a++)//controls racing
{
int start=0;int end=27;
r=1+(int) (Math.random()*10);
for(int b=start;b<=end;b++)//increment of each step
x[b]=x[b]+r;
q=1+(int) (Math.random()*10);
for(int b1=start;b1<=end;b1++)
x1[b1]=x1[b1]+q;
rr.fillPolygon(x,y,28);
rr.fillPolygon(x1,y1,28);
try
{
th.sleep(75);
}
catch(Exception e)
{
}
rr.fillPolygon(x,y,28);
rr.fillPolygon(x1,y1,28);
}
}
publicvoid actionPerformed(ActionEvent e)
{
if (e.getSource()== race)
{
flag1 = 2;
update(getGraphics());
}
}
}
Thanks in advance...

