Stanford CS106a newbie - Having trouble compiling in Eclipse - java

I'm following along with Stanford's CS106a class and trying to do the assigments. I had difficulty running the sample code from the book but somehow managed to run them with the ACM package. Right now I'm trying to do the assignments and run my own code. I've created a "project" and a .java file in that project. I don't know how to run it though. I keep getting the following:
Error: Could not find or load main class Pyramid.
I think it is because the program isn't accessing the ACM package. Below is the code although I think it would happen with any code I write. Any help would be appreciated.
Thanks so much.
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class GRectExample extends GraphicsProgram {
public void run() {
GRect rect = new GRect(100, 50, 125, 60);
rect.setFilled(true);
rect.setColor(Color.RED);
add(rect);
}
}

Create a main method inside GRectExample class, for examle
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
public class GRectExample extends GraphicsProgram {
public void run() {
GRect rect = new GRect(100, 50, 125, 60);
rect.setFilled(true);
rect.setColor(Color.RED);
add(rect);
}
public static void main(String args[])
{
new GRectExample().run();
}
}

Looks like you have to tell Eclipse where to locate the ACM package, most of the times it can't assume the exact location.
Right click on your project folder and select Properties.
Select the Java Build Path option and click on the "Add External JARs" and that will include it into your project...

Not too familiar with Eclipse, but here's a suggestion:
Right - Click on the Project folder
click Properties at the bottom
click Run/Debug Settings
Make sure your Launching class is the list. Click on it, make sure it's the Main class
Make sure you use the fully qualified name i.e. mypackage.MyClass
Also try clicking all of them in the list. And make sure only the one you want to be the launching class has the Main Class field filled in.

Related

InteliJ JavaFX displaying blank white screen when booting

I'm trying to get a starter project for a template running over for JavaFX, the assignment is to get a hello world program up and running using JavaFX to get the program running.
(Some Background context: We're using IntelliJ for our class assignment, but recently we found out that it seems(?) support for JavaFx has been discontinued within InteliJ, regardless the same assignment remains so we installed a third party library following the steps over from a guide about getting the library, ( How to get JavaFX and Java 11 working in IntelliJ IDEA ), the instructions helped to get the program running and compiled and I used the paths out, but when I ran the program, I got a blank white screen, and I have no clue if this is normal or not.)
The code is just the default JavaFX template from Intllij, I've tried installing the SDKS and JDKS (which seemed to help the program compile), but when running, I just get a blank white screen.
Here is a screenshot of the white screen problem https://i.imgur.com/7ZYju3M.jpg
And the code below; it's just a IntelliJ default startup, but I don't know why it doesn't work.
package sample;
// Original Imports found in testTemplate
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
public class Controller {
}
We expected the results to say hello world. I think so anyways, but I actually have no clue what it's supposed to look like as it's my first time, I think it's supposed to say hello world. (Later on, we're supposed to get the GUI configured, but for right now, I just want to figure out what's going wrong with the program.)

lwjgl import Input Classes is not working

I have a Problem with Java and lwjgl the Import:
import org.lwjgl.input.Keyboard;
won't work. I have added the jars:
lwjgl-glfw.jar
lwjgl-opengl.jar
lwjgl-stb.jar
lwjgl.jar
joml
You are using LWJGL 3 right? LWJGL 3 doesn't have a Keyboard or Mouse class, you have to use the functions provided by GLFW. http://www.glfw.org/docs/latest/input_guide.html
As Wendelin said, if you're using LWJGL 3, the import you try to use, is there no longer. In LWJGL 3, you set callbacks. A callback, is a function/method that you create, and LWJGL executes. If for example, you set a close callback, your function/method is called when LWJGL has detected that the user wants to quit.
I can show you two examples of this: closing and iconifying.
import static org.lwjgl.glfw.GLFW.glfwSetWindowCloseCallback;
import static org.lwjgl.glfw.GLFW.glfwSetWindowIconifyCallback;
public class Program {
public static void main(String[] arguments) {
glfwSetWindowCloseCallback(display, (NULL) -> {
System.out.prinln("User tried to quit")
});
glfwSetWindowIconifyCallback(display, (window, iconified) -> {
System.out.println("User tried to iconify the window")
});
}
}
The first argument to the callback function setter, display, is the display the callback will be set on. You should check out the link Wendelin provided, for more information.
If you are using LWJGL 2, or the imports aren't working in LWJGL 3, you've probably not attached the framework to your project correctly. If this is the case, you'll simply have to fix that problem. If you're in IntelliJ IDEA (MacOS), you can go to File -> Project Structure -> Project Settings -> Libraries, and click on + to add a new framework to your project.

Background not showing up in java processing project

I think I'm doing everything right in my code but my background won't show up in my processing project, here is my code.
package finalproject;
import processing.core.PApplet;
import processing.core.PImage;
public class FinalProject extends PApplet {
PImage background;
PImage player;
public void setup() {
size(1360, 1080);
player = loadImage("player.png");
background = loadImage("rust.png");
}
public void draw() {
background(background);
image(player, 500, 500);
}
}
Processing expects files to be inside a data directory next to the code.
You're presumably running this from an IDE like Eclipse instead of the Processing editor, so where you put that data directory depends on how your code is setup. And you haven't posted a MCVE, so it's hard to help you with that.
But basically, you need to debug your sketch to figure out exactly where Processing is looking for the files. Then you need to move the files there. This is probably something simple like putting them inside a data directory.
If you still can't get it working, please post a MCVE along with a screenshot or a description of your directory structure.
If you are using the processing IDE than the data folder should be located in your sketch folder next to all the .pde files. Make sure that the image you are using has the same resolution as the sketch window. If you are still having issues I would recommend that you try moving your setup and draw methods out of your class and into the main processing sketch.

Getting an error "No main class found" when I tried to run a simple program on NetBeans.

package javaapp;
import javax.swing.JApplet;
import java.awt.Graphics;
public class JavaApp extends JApplet
{
public void paint(Graphics canvas)
{
canvas.drawOval(100, 50, 200, 200);
canvas.fillOval(155, 100, 10, 20);
canvas.fillOval(230, 100, 10, 20);
canvas.drawArc(150, 160, 100, 50, 180, 180);
}
}
I tried to run this code which draws a smiley face on a canvas. I ran this code also on Eclipse, where it compiled fine. But on NetBeans, it keeps saying it can't find the main class. Is NetBeans recommended for a beginner like me or should I look for another IDE that better suits for a beginner? I am more fond with the NetBeans because the interface seems more friendly.
In netbeans, right click on the project and chose properties. Under
Application > Webstart, there is an option Applet class. specify the
full path to your main class (i.e with package names).
Right click the project node and choose 'run' from the list. Don't right click the file (run file) as this doesn't work in NetBeans.

Intellij IDEA Designer for an Applet

I'm trying to design an applet using the designer delivered with Intellij IDEA. I have no idea where I can find an option to make one. Solutions have been posted on the net, but seem to be outdated at the moment.
Create this class in your application
import java.applet.Applet;
import java.awt.*;
public class Main extends Applet {
public void paint(Graphics g) {
g.drawString("SALAM Applet", 20, 20);
g.drawString("سلام اپلت", 20, 40);
}
}
Right click on your applet class and select create 'Main'.
Click OK.
Run
for more information visit this link

Categories