Alex.G
Alex.G

Reputation: 1

Running Java Code as a Java Application and not an Applet

I'm new to Java and I've been trying to run this code as a Java Application instead of a Java applet and it doesn't work (using Eclipse IDE). When I click run, it doesn't give me the option to run it as a Java Application. How would I fix this?

Here is my code:

import java.awt.Color;

import acm.graphics.GOval;
import acm.graphics.GPoint;
import acm.graphics.GRect;
import acm.program.*;
import acm.graphics.*;

public class Coordinates extends GraphicsProgram {
    public void run() {
        GOval myOval = new GOval(-8, -8, 16, 16);
        myOval.setColor(Color.RED);
        myOval.setFilled(true);
        add(myOval);

    }

}

Here are the options given to me when I click run:

Java_Screenshot

Thank you.

Upvotes: 0

Views: 247

Answers (6)

Linh Tran
Linh Tran

Reputation: 11

Late answer but hope it's useful for others with the same problem. I ran into the same issue if I click run for individual program in the project. But when I right-click the project (see pic), I can choose Java Application and then pick the specific program to run (I have lots of programs within the project) and then it runs as usual... Hope this helps with your case. Java Application

Upvotes: 0

Anthony
Anthony

Reputation: 1333

You could try to run it in Applet Runner for Eclipse plug-in. It's a free plug-in to run applets in the IDE.

Full disclosure, I'm the developer of the plug-in.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168815

The bad news begins with the fact that an ACM based GraphicsProgram extends Applet/JApplet.

enter image description here

That's bad news because the Java Plug-In technology needed to run applets and web-start apps was deprecated around Java 9 & removed from the Java API.


To do custom painting as suggested in the example, I'd extend a Swing-based JPanel and change the paint method, then display it in a JFrame.

Anything ACM based is no longer functional.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347184

This is just guess work, as we don't have access to acm.* and applets have their defined life cycle, but the intention would be to create a JFrame and add the Coordinates component to it

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                Coordinates coordinates = new Coordinates();
                coordinates.init();
                frame.add(coordinates);
                coordinates.start();
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Coordinates extends GraphicsProgram {

        public void run() {
            GOval myOval = new GOval(-8, -8, 16, 16);
            myOval.setColor(Color.RED);
            myOval.setFilled(true);
            add(myOval);

        }
    }
}

Upvotes: 1

g00se
g00se

Reputation: 4296

Try the following

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Coordinates());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

user2601064
user2601064

Reputation: 271

You need a main method, yo.

public static void main(String[] args){}

Upvotes: 0

Related Questions