Can't use LWJGL correctly, code giving errors - java

I'm trying to run this code that is used to see if things were setup properly:
package simpleslickgame;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
public class SimpleSlickGame extends BasicGame
{
public SimpleSlickGame(String gamename)
{
super(gamename);
}
#Override
public void init(GameContainer gc) throws SlickException {}
#Override
public void update(GameContainer gc, int i) throws SlickException {}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
g.drawString("Howdy!", 10, 10);
}
public static void main(String[] args)
{
try
{
AppGameContainer appgc;
appgc = new AppGameContainer(new SimpleSlickGame("Simple Slick Game"));
appgc.setDisplayMode(640, 480, false);
appgc.start();
}
catch (SlickException ex)
{
Logger.getLogger(SimpleSlickGame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
All of the Googling I've done has led me to checking the natives over and over and I"m pretty sure I've done it right. When I go to the libraries in this project's build path, JRE System library and Slick2D both say Native library location: .../windows/x64. I've tried just .../windows, and I've tried it with the JRE not having a native location. I followed two different tutorials on how to do this and I keep getting errors trying to run this simple code. Any help?

basically what you need is to add slick.jar and lwjgl.jar to your build path and then right click your project >> open properties >> click on Java Build Path >> open Libraries tab >> expand JRE System Library >> click Native library location >> edit >> External Folder >> find ..\lwjgl-2.9.0\native\windows folder on your hard drive >> select it >> and you're done
g.drawString("Howdy!", 10, 10);
I would just move this string to some other location, cause it's showing over the FPS counter :)
EDIT:
you can use the natives from ..\slick\lib\natives-windows.jar.
open it with WinRAR or something and extract to a folder, then just set that folder to be your Native library location

Related

How to solve error class file for com.jogamp.common.type.WriteCloneable not found

While compiling JOGL code, I am getting following error
class file for com.jogamp.common.type.WriteCloneable not found
I also attached a screenshot of the error:
Here is the code:
package com.jogamp.opengl;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class HelloWorld implements GLEventListener {
#Override
public void init(GLAutoDrawable arg0)
{
}
#Override
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
//Draw H
gl.glBegin(GL2.GL_LINES);
gl.glVertex2d(-0.8, 0.6);
gl.glVertex2d(-0.8, -0.6);
gl.glVertex2d(-0.8, 0.0);
gl.glVertex2d(-0.4, 0.0);
gl.glVertex2d(-0.4, 0.6);
gl.glVertex2d(-0.4, -0.6);
gl.glEnd();
//Draw W
gl.glBegin(GL2.GL_LINES);
gl.glVertex2d(0.4,0.6);
gl.glVertex2d(0.4,-0.6);
gl.glVertex2d(0.4,-0.6);
gl.glVertex2d(0.6,0);
gl.glVertex2d(0.6,0);
gl.glVertex2d(0.8,-0.6);
gl.glVertex2d(0.8,-0.6);
gl.glVertex2d(0.8,0.6);
gl.glEnd();
}
#Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4)
{
}
#Override
public void dispose(GLAutoDrawable arg0)
{
}
public static void main(String[] args) {
final GLProfile gp = GLProfile.get(GLProfile.GL2);
GLCapabilities cap = new GLCapabilities(gp);
final GLCanvas gc = new GLCanvas(cap);
HelloWorld sq = new HelloWorld();
gc.addGLEventListener(sq);
gc.setSize(400, 400);
final JFrame frame = new JFrame("Hello World");
frame.add(gc);
frame.setSize(500,400);
frame.setVisible(true);
}
}
Please help me resolve this issue.
Note: I downloaded jogl-all.jar file. Then I unzipped it. Then written the code above.
Download jogamp-fat.jar here (it's the latest release candidate in October 2020, otherwise, take this one when it becomes more recent, typically when JOGL 2.4.0 or any later version is released).
Compile with:
javac -cp .:jogamp-fat.jar HelloWorld.java
Run with:
java -cp .:jogamp-fat.jar HelloWorld
You can find much more information in our official wiki here. Windows users must replace : by ;. Using the fat JAR is easier and less error prone especially for newbies, it contains both the Java libraries and the native libraries of all JogAmp APIs (JOGL, JOCL, JOAL and GlueGen).
You need to include gluegen-rt lib to compile your class.
Also, you don't need to unzip jar files.
Use -cp variable to add a jar library to your classpath:
javac -cp .:gluegen-rt.jar:jogl-all.jar HelloWorld.java
To run your example you also need native extensions. You can find them here
Note: Use the latest JOGL libraries especially if you use the latest Mac OS version.
java -cp .:gluegen-rt.jar:jogl-all-natives-macosx-universal.jar:jogl-all.jar:gluegen-rt-natives-macosx-universal.jar HelloWorld
Here is the output:

Unable to find the right JVM options

Alright, first and foremost, I am new to Java, but not to programming.
For a personnal project I decided that I should use Slick2D, a graphics library in java, and I am running into one problem I haven't been able to fix for now.
Considering the following:
I use Netbeans
I'm currently on linux
From what I understand, slick2d uses lwgjl. I have followed the instructions on the following page:
Setting up Slick2D for netbeans
My problem I think is giving my JVM the right options.
Currently the ones I set are:
-Djava.library.path=/home/karel/Téléchargements/slick/native/unix
Here is the current error output I get:
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:87)
at org.lwjgl.Sys.<clinit>(Sys.java:117)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at jcoinche.client.JcoincheClient.main(JcoincheClient.java:29)
/home/karel/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
My code is as follow (well, the part that needs a library, and doesn't work - it's all tutorial material, by the way. Nothing I made myself):
package jcoinche.client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import static jcoinche.client.Game.gamename;
import static jcoinche.client.Game.xSize;
import static jcoinche.client.Game.ySize;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;
public class JcoincheClient
{
public static void main(String[] args)
{
Socket socket;
AppGameContainer appgc;
try
{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(xSize, ySize, false);
appgc.setTargetFrameRate(60);
appgc.start();
}
catch(SlickException e)
{
e.printStackTrace();
}
}
Game.java:
package jcoinche.client;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame
{
public static final String gamename = "MyGameName";
public static final int play = 0;
public static final int xSize = 400;
public static final int ySize = 300;
public Game(String gamename){
super(gamename);
this.addState(new Play());
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(play).init(gc, this);
this.enterState(play);
}
}
Play.java:
package jcoinche.client;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Play extends BasicGameState
{
public Play()
{
}
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException
{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException
{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException
{
}
public int getID()
{
return 0;
}
}
The error is telling you of the absence of a file named 'lwjgl64'. You are getting an UnsatisfiedLinkError, which according to the Java Documentation, is:
Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
Now, are you using Slick2D alone? Or do you have LWJGL and Slick2D? Slick2D is not standalone; you need LWJGL to run it. I suggest getting LWJGL from their site, and add it's native folder to the classpath.
From the Slick2D docs, they write this:
-Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
Source
There are a few answers on stack that may help you solve this issue as it is clear from the error that it is caused by a linking error to LWJGL64. One post with a number of solutions here offers a number of solutions, though targeted at the eclipse IDE specifically the general solution applies:
To quote from here #Ben Jackson
LWJGL needs the native components for your particular platform to be
in java.library.path. These are in the subdirectory native in the
LWJGL distribution and end in .so on Linux, OSX and Solaris and .dll
for windows.
Other potential examples solving the error:
Setting up natives
Alright, this is a bit embarassing, but I understood what my problem was and it was a simple misunderstanding of the link I gave (instructions on how to install and use slick2D on netbeans):
In fact, my problem was that, because of the lwgjl.jar and other similar files in the slick2D folder, I thought lwgjl was, in the end, included in the slick folder.
As such, for the last step, I gave to my JVM a path directing it to the slick library folder, while it was, this time, asking for lwgjl files, causing the errors shown above.
I changed that path to the lwgjl/native/linux folder, and everything is now working perfectly.
Thanks for your answers, they helped greatly.

File Path Changing on Export

I'm currently trying to develop a game, and the file path is changing when I export it.
Here is the code:
package random;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Troll extends JFrame{
/**
*
*/
private static final long serialVersionUID = 4176461585360667597L;
public static BufferedImage img;
public static void main(String[] args){
File f = new File("troll.png");
try{
if(f.exists()){
System.out.println("ITS THERE! :D");
img = ImageIO.read(f);
} else {
System.out.println("DOESNT EXIST, REAL PATH IS: " + f.getAbsolutePath() );
}
}catch(Exception e){
e.printStackTrace();
}
new Troll();
}
public Troll(){
init();
}
public void init(){
setSize(1200,800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void paint(Graphics g){
g.drawImage(img, 500, 350, this);
}
}
When I run it through Eclipse ( The IDE I'm using ), it's running fine and it's showing the image. When I export it as a jar and convert it to an exe using Jar2Exe Software, the image does not appear and in the console it says that the absolute path for the image is on my Desktop. But when I open the exe using 7-Zip, the picture is in the exe.
How can I export it so that when the user runs it, the program can find the file path and show the image, instead of it thinking that it's on my desktop?
If you want to publish it as a jar then you need to use the Jar-specific API for reading your file. See eg. How to read a file from a jar file? (and you need to configure Eclipse to put the picture in the jar, which it sounds like you're already doing).
Also you should let us know whether it works when it's in a jar but not as an exe, that will help us narrow down where the problem may be.
I hope this is not a troll (lol)
img = ImageIO.read(this.getClass().getResource("/troll.jpg"));
You are in a jar, there is no resource file.
See that link as well: http://www.jar2exe.com/createdexe/integrate/protect
Thread.currentThread().getContextClassLoader().getResource("hello/yes.gif");

Tiled game libGDX - couldn't load file (Exception in thread "LWJGL Application")

I am following a java tiled game tutorial using libGDX and I met the following errors :
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: gaming creation/herbe16.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:142)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:112)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:108)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:119)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104)
at com.poussins.screens.Play.show(Play.java:35)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at com.poussins.Poussins.create(Poussins.java:11)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: gaming creation/herbe16.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:133)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:563)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
... 12 more
here is the code i'm using in the Play class :
package com.poussins.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class Play implements Screen{
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(camera);
renderer.render();
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
#Override
public void show() {
map = new TmxMapLoader().load("maps/map-Herbe-Goudron.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
}
}
In order to put the .tmx map into the Eclipse project, I just made a Copy (from the folder where the .tmx map is) and a paste in the Eclipse project named "Poussins-Desktop / assets / maps". I used the same actions (copy paste) to give the .png tiles to the same Eclipse project named "Poussins-Desktop / assets / maps" (can see it in the screenshots).
In the code, I am not sure with this following line :
map = new TmxMapLoader().load("maps/map-Herbe-Goudron.tmx");
because I don't know if the path to the .tmx is good, but it seem to respect what was said in the tutorial ...
In fact, in the error, it is saying that the file herbe16.png cannot be loaded. but this file is a tile from the whole .tmx map.
I am working on UBUNTU 13.10 system, Eclipse 3.8.1
I hope someone will help me and I thank you in advance for paying attention to my problem.
Okay, I don't know if this will help you..
I was just declaring the path for my files not minding caps letters (I'm using windows7).
Now, when I tested the code as a java application on my desktop it was going smoothly but when I tried launching it on an emulator or an android device it would crash.
Turns out that the emulator and device both need the exact path with capital letters.
This may have already been solved or the OP may have given up on it, but I came across this error too on the same tutorial. I solved the issue in my case. It may be possible that the OP was using a .png of a different bit depth (as was in my case). The .png that caused the error was 64 bit depth (you can check by right clicking on the .png -> PROPERTIES -> DETAILS). Using a .png of a bit depth of 32 allowed the loading to work perfectly fine. I used photoshop to create my .png tileset, and when creating the new file, set it to 8 bit (next to the color mode).

Issue Exporting .app Using Processing and Leap Motion SDK

I have been trying to export a standalone application for Mac using the "Export Application" function in Processing. The simple application, which involves updating a float value on the screen that corresponds to the y-axis position of a hand detected by a Leap Motion, works perfectly well within Processing. However, once I export the application and try to run it, the app opens for a brief second and quickly closes. I have exported applications from Processing successfully before, but not one that uses the Leap SDK. I am using the LeapMotion library for Processing to access the Leap SDK: https://github.com/heuermh/leap-motion-processing. Here is my code:
import processing.core.*;
import com.leapmotion.leap.processing.*;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Listener;
public class Test extends PApplet {
Controller controller;
MyListener listener;
float position = 0.0f;
class MyListener extends Listener {
public void onFrame(Controller controller) {
if (!controller.frame().hands().isEmpty()) {
position = controller.frame().hands().leftmost().palmPosition().get(1);
}
}
}
public void setup() {
size(600,600);
controller = new Controller();
listener = new MyListener();
controller.addListener(listener);
}
public void draw() {
background(0);
textAlign(CENTER,CENTER);
textSize(50);
text(position, 300,300);
}
public static void main(String args[]) {
PApplet.main("Test");
}
}
When I export the application, the contents of the application are as follows:
Info.plist
Java
-core.jar
-gluegen-rt-natives-macosx-universal.jar
-gluegen-rt.jar
-jogl-all-natives-macosx-universal.jar
-jogl-all.jar
-LeapJava.jar
-LeapMotion.jar
-libLeap.dylib
-libLeapJava.dylib
-Test.jar
MacOS
-Test
Plugins
-jdk1.7.0_45.jdk
Resources
-en.lproj
-sketch.icns
PkgInfo
Any help would be greatly appreciated!
P.S. I've already tried bundling the program into a runnable JAR file.

Categories