Reputation: 1
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:
Thank you.
Upvotes: 0
Views: 247
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
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
Reputation: 168815
The bad news begins with the fact that an ACM based GraphicsProgram
extends Applet
/JApplet
.
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
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
Reputation: 4296
Try the following
public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(new Coordinates());
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 271
You need a main method, yo.
public static void main(String[] args){}
Upvotes: 0