0

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.

royhowie
  • 11,075
  • 14
  • 50
  • 67
neffje
  • 1
  • 1
  • Ignoring any compiler issues, is there anything about [actual applets](http://docs.oracle.com/javase/8/docs/api/java/applet/package-summary.html) in your question? To make an applet, you must have a class extending `Applet` somewhere if you're ever going to incorporate your code into a webpage. Currently, your code is only fit for a desktop application. – FThompson May 08 '15 at 22:41
  • Yes, there is something about actual applets in my question: "Is it possible to turn this application into an applet without running it through a compiler?" I understand that my class must extend Applet (or JApplet, since I'm using Swing). If it was as simple as changing "JFrame" to "JApplet," then I wouldn't be here. – neffje May 08 '15 at 22:52
  • You'll have to edit the source code *and* run through a compiler in order to convert it to an applet, so I've closed your question as a duplicate as your issue stems primarily from the compiler question rather than applets in particular. – FThompson May 08 '15 at 23:03
  • So you understand my problem and could probably help me, but you choose to merely mark my question as a duplicate over a technicality, and another user who could also likely help me just edits out my "Thanks in advance." – neffje May 08 '15 at 23:12
  • 1
    Quite the contrary: I'm trying to help you. It's just more efficient for me to refer you to a similar question with a large amount of solution-related information rather than to type out the exact same solution information again myself. In marking it as a duplicate, I hope to save others' time (both yours and other potential answerers) as well. – FThompson May 08 '15 at 23:23
  • @neffje - The "thanks in advance" is noise. Stuff like that routinely edited out of Questions. – Stephen C May 08 '15 at 23:25
  • I agree that the answers in the linked question really should solve your problem. The "'javac' is not recognized..." message is comes from the shell, and it is clearly saying that there is a PATH problem that you haven't solved properly. – Stephen C May 08 '15 at 23:33
  • Also, it is a bad idea to ask lots of unrelated question in one Question. You have asked (at least) 4 unrelated questions: 1) how to fix my path problem, 2) can I run an applet without compiling it (answer: no), 3) How to convert a Swing app to an applet (answer: rewrite it) 4) Can you review my code (answer: no ... that is off-topic). – Stephen C May 08 '15 at 23:40
  • @Vulcan I agree that it would be helpful to refer me to a similar question if that similar question answered my question. Marking my question as a duplicate does not save me time. You understand my need, but you are more concerned that my need is reflected in my question. – neffje May 09 '15 at 00:01
  • @StephenC Maybe "thanks in advance" is noise to you, but to me, it's an attempt to show that I'm sincerely grateful for help. I've spent a lot of hours trying to make this work, to no avail. I came here looking for help, and my question is being rejected because I don't have enough experience with Java to refer to my problems in the way that those who are experienced with Java would refer to them. – neffje May 09 '15 at 00:04
  • @neffje We love to help! But StackOverflow is designed heavily with future researchers in mind (who encounter similar questions), so it's paramount that duplicate questions do not obscure their ability to find the original answer which, nine times out of ten, has the best answers accumulated on it because duplicate questions are closed. While there's less focus on individual interaction and help, it's definitely something you'll value more and more as you find that many of your questions are already answered all in one place at the tip of a Google search. – FThompson May 09 '15 at 02:17
  • For better or for worse, it's a "net-good" utilitarian kind of approach in a way, but that also doesn't mean we can't help out somewhat in the comments. Have you been able to solve the issue? – FThompson May 09 '15 at 02:19
  • @Vulcan I appreciate the explanation, and I can understand your position. Thanks to the information that Dmitry provided, I am able to successfully compile my .java file. I modified the code enough to get the applet to show in a browser, and the vehicle draws; however, I am still having issues. I don't know if the issues are a result of my coding being inefficient, but that's what I suspect. – neffje May 09 '15 at 02:47
  • The first issue is the lag. It takes a second or two before the vehicle draws. The second issue is movement. In Chrome and Firefox, the vehicle won't move. The third issue is consistency. In IE, the vehicle will typically not move after the first load. When I refresh, however, the vehicle will move 8 or 9 times out of ten. – neffje May 09 '15 at 02:49

1 Answers1

0

Could you show your modified PATH variable?

The easiest solution is running javac with the full path from your source code location:

c:\location_of_CarJava>"c:\Program Files\Java\jdk1.8.0_45\bin\javac.exe" ...
  • The variable value is: "C:\Program Files\Java\jdk1.8.0_40\bin." Must I also include "\javac.exe" in the variable? Could it be such a simple solution? EDIT: I added "\javac.exe" to the variable and it still said it's not recognized. – neffje May 08 '15 at 22:42
  • Can you post the output of `echo %PATH%`? – Dmitry Zolotukhin May 08 '15 at 22:47
  • My suggestion was to call javac directly: `"c:\Program Files\Java\jdk1.8.0_45\bin\javac.exe" Car.java` – Dmitry Zolotukhin May 08 '15 at 22:48
  • 'C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\' – neffje May 08 '15 at 22:54
  • I changed directory to the .java file location and called upon javac.exe direction with the entire path, and I received neither errors nor confirmation that the .java file was compiled. – neffje May 08 '15 at 22:56