LWJGL/opengl in eclipse - java

I cannot seem to import the opengl properly.
I am following this simple tutorial:
https://www.youtube.com/watch?v=ZKJC2cloIqc
As far as I understand, I have the correct jar and native files.
// I can import these
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
//I cannot however import this:
import org.lwjgl.LWJGLEXCEPTION;
//and gl methods such as this are not recognized:
glClear(GL_COLOR_BUFFER_BIT);
I am using eclipse, I also have netbeans and am debating getting the intelij IDE being used in this tutorial if it will make this work.

You didn't Import GL11 to use gl methods (or other versions for certain methods )
You can use a static import so you don't have to put GL11 in front of every method or anything else

Related

Java class import issues. import java.awt.geom.Rectangle2D.Float not working

I am using the java.awt.geom library in Processing and specifically using the Rectangle2D.Float class.
When I use import java.awt.geom.Rectangle2D.Float I get an error when I try declaring Rectangle2D.Float buttonOne;, but if I use import java.awt.geom.Rectangle2D or import java.awt.geom.* the code works fine.
I am wondering why this is.
If you just drop the Rectangle2D from the Float it will work with the first import

glEnable(GL_TEXTURE_2D); Does not feel like working.... :(

So, I'm trying to apply a texture to my quad.
So i wrote this line of code : (line 35 in Artist.java)
glEnable(GL_TEXTURE_2D);
Which gave me this error :
GL_TEXTURE_2D cannot be resolved to a variable
I have these imports : (Hitting CTRL+SHIFT+O did not do anything)
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
I'm using Java, Eclipse, Mars. Anyone know why it won't work? :)
glEnable( GL_TEXTURE_2D) is a directive to the fixed-function
pipeline's shader generator that you want to include code to support
that texture unit
So if you use a modern opengl which has custom shaders, its option won't work ,and you won't need it.
For it to not error you need to import both GL_TEXTURE_2D and glEnable().
Like Sung Woo mentioned however, this function is deprecated and only needs to be used in the old OpenGL.

Automatically grouping imports in intelliJ IDEA

I want to group imports by packages in intelliJ.
Right now it sorts the imports properly, and gives me this.
import com.google.common.Something
import com.google.common.SomethingElse
import org.apache.commons.Something
import org.apache.commons.SomethingElse
I want it to be
import com.google.common.Something
import com.google.common.SomethingElse
import org.apache.commons.Something
import org.apache.commons.SomethingElse
How can I make intelliJ automatically do this, without having to set the package names individually in Editor->Cody Style->Java->Imports->Import Layout.

How do I import packages in Java?

I have a "Sprites" folder with some class files and a "Launcher" folder with some class files. I tried the following code for import:
package Sprites;
and it lead to the following
hw9\Launcher>javac *.java
TowerDefense.java:2: error: class, interface, or enum expected
package Sprites;
^
1 error
Am I doing this incorrectly? My Sprites and Launcher are in the hw9 directory, so I assumed it would work. A picture for clarification:
You can use a wildcard import to import all classes within the immediate directory:
import Sprites.*;
This opposed to something like:
import Sprites.Class1;
import Sprites.Class2;
import Sprites.Class3;
...
Generally, wildcard imports can produce conflicts and errors (for example java.awt.List and java.util.List), so usually better to avoid them.
Packages should also be lower-cased.
The error is due to syntax, usually when you see something like ...expected that is syntax error indicator.
In the class in your launcher package, include the import statements for the classes which are being referred to.
It should look something like the following:
package the.name.of.your.package;
import Spirites.NameOfclass; //quialify the import parth as is
class YourLauncherClass{
//class definition
}
Also make sure that semicolons aren't missing at the end of import and package.
Hope that helps.
Best practice is to import the specific class you require rather than importing the complete package.
import Spirites.NameOfclassRequired;
class YourClass{
//class definition
}
If you are using eclipse you can do that using CTRL+SHIFT+O When you do that eclipse imports the specific class you require. For an instance if you using an ArrayList rather than importing java.util.*; it will import java.util.ArrayList;
If you need multiple classes from a package then for sure you can import the entire package

Static import - Netbeans error

The error, Netbeans gives me, is:
static import only from classes and interfaces
which is somehow strange for me, as this:
import org.lwjgl.opengl.GL11;
works fine while this:
import static org.lwjgl.opengl.GL11;
doesn't. Why is it not working for me?
BTW, GL11 is a class and I don't know why but Netbeans, when importing statically, thinks opengl is the class I want to import.
You wanna write:
import static org.lwjgl.opengl.GL11.*;
You are importing the members of the class, thus the * at the end.
Static import allows you to import static fields of other class. For example you can say
import static java.awt.Color.RED;
And then use RED in your class without mentioning class where it is defined.

Categories

Resources