I've been tasked with creating a very simple webpage that includes a Java applet of a vehicle that moves across the screen and stops before leaving the window. I'm new to Java, so although I was able to successfully create an application that will do what I want, I'm not having any luck converting the application into an applet. Here is my code:
package finalProject;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Car extends JFrame {
private static final long serialVersionUID = -5306640844197169382L;
public static void main(String[] args) {
JFrame GUI = new JFrame();
GUI.setTitle("Final Project");
double width = java.awt.Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int w = (int) width;
GUI.setSize(w, 210);
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawCar van = new drawCar();
Container vanPane = GUI.getContentPane();
vanPane.add(van);
GUI.setVisible(true);
for(int i = 0; i <= w - 332; i++) {
van.setLayout(null);
van.setLocation(i += 1, 0);
try {
Thread.sleep(12);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class drawCar extends JPanel {
private static final long serialVersionUID = 4320980242217677298L;
int width = getWidth();
int height = getHeight();
public void paint (Graphics g) {
Color bodyRed = new Color (255, 0, 0);
Color tailLightRed = new Color (168, 0, 0);
Color handle = new Color (196, 196, 196);
Color tire = new Color (36, 36, 36);
Color hubCap = new Color (255, 128, 48);
Color headLamp = new Color (255, 255, 194);
Color smoke = new Color (41, 41, 41);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Sans-serif", Font.ITALIC, 9);
g2.setFont(font);
// rear bumper
g.setColor(Color.white);
g.fillRect(15, 140, 25, 10);
g.setColor(Color.black);
g.drawRect(15, 140, 25, 10);
// running board
g.setColor(Color.white);
g.fillRect(90, 140, 140, 10);
g.setColor(Color.black);
g.drawRect(90, 140, 140, 10);
...
}
}
I run into trouble when I try to compile the file (Car.java). I've tried several different methods for compiling Car.java. This is the error message that I most often see:
'javac' is not recognized as an internal or external command, operable program or batch file.
I have added the location path of javac.exe to the list of Environment Variables, but as far as I can tell, it hasn't done any good. I'm still told that 'javac' isn't recognized. If I changed the directory to the location of javac.exe and try to compile, then javac.exe works, but I am told that Car.java is not found. If I then use -sourcepath to identify the location of Car.java, then this is what comes up:
javac: no source files
Is it possible to turn this application into an applet without running it through a compiler? In other words, can I just modify the code in the .java file and change it to a .class file?
I'm also open to hearing if my code could be improved; however, since my code (even if not ideal) still creates the desired effect, my main focus right now is getting this turned into an applet.