I am learning and programming a game for android using libgdx and i have got stuck on this error for quite a long period of time. I have enclosed the following content
The code.
The command i used to access the asset.
Screenshot that contains the error when i debug the code on android.
Screenshot of my Package Explorer.
All the possible combinations i tried to get the code working.
1.The Code
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
public class Gameassetloader implements Disposable, AssetErrorListener {
public static final String TAG = Gameassetloader.class.getName();
public static Gameassetloader instance = new Gameassetloader();
private AssetManager assetManager;
/** Load the appropriate texture for the respective entities **/
public AssetCharacter boy;
public AssetRock rock;
public AssetShield shield;
/** singleton: prevent instantiation from other classes **/
private Gameassetloader(){}
public void init(AssetManager assetManager)
{
this.assetManager = assetManager;
//set asset manager error handler
assetManager.setErrorListener(this);
//load texture atlas
assetManager.load(Gameconstants.TEXTURE_ATLAS_OBJECTS,TextureAtlas.class);
//start loading assets and wait until finished
assetManager.finishLoading();
Gdx.app.debug(TAG, "# of assets loaded:" + assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames())
Gdx.app.debug(TAG, "asset:" + a);
TextureAtlas atlas = assetManager.get(Gameconstants.TEXTURE_ATLAS_OBJECTS);
//enable texture filtering for pixel smoothing
for(Texture t : atlas.getTextures())
t.setFilter(TextureFilter.Linear,TextureFilter.Linear);
//create game resource objects
boy = new AssetCharacter(atlas);
rock = new AssetRock(atlas);
shield = new AssetShield(atlas);
}
public void dispose() { assetManager.dispose(); }
#Override
public void error(String fileName, Class type, Throwable throwable)
{
Gdx.app.error(TAG, "Couldn't load asset' " + fileName + "'", (Exception)throwable);
}
}
2.The Command
public static final String TEXTURE_ATLAS_OBJECTS ="gdxgame-android/assets/packed/packed.png";
3.Screenshot of Package Explorer
http://i1294.photobucket.com/albums/b608/Abhishek_M369/PackageExplorer_zpsd2589ee2.jpg
4.Screenshot of Error log (Android DDMS)
http://i1294.photobucket.com/albums/b608/Abhishek_M369/ErrorLog_zps8a4a68d9.jpg
5.All the possible combinations i tried
"/gdxgame-android/assets/packed/packed.png";
"gdxgame-android/assets/packed/packed.png";
"/assets/packed/packed.png";
"assets/packed/packed.png";
"/packed/packed.png";
"packed/packed.png";
"/packed.png";
"packed.png";
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.toString();
//this caused shutdown of emulator
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.readString();
// I even tried setting the build path of the asset folder as source folder
// Also tried placing the image in the data folder.
// Tried using the absolute path too , i.e Gdx.files.absolute("the absolute");
// Tried passing the absolute path directly as string
Nothing seems to work for me.
The problem was very simple, the problem lied in the different versions of libgdx and the documentation. The answer explained below will solely confirm to libgdx version 0.9.8 only.
(Note: The usage of Texturepacker GUI was used to pack textures && not the method from the libgdx library)
First the "assetManager" had to be supplied with a file that contains the coordinates of the images which was a mistake here as i was supplying the packed image.
The GL20 should be able to parse NPOT images but it was unable to do so and the reason remains unknown so i had to pack the texture to a POT which was accomplished by selecting the POT options in the GUI. After doing this i was able to load the newly POT image easily with the following code
/**Mention only the folder/file under the asset dir**/
public class Gameconstants { public static final String location = "packed/packed.txt" }
/**access the same using the following command**/
private AssetManager assetManager;
assetManager.load(Gameconstants.location,Texture.class);
This Answer may not be very convincing but it surely solved my problem.
Thank you to all who helped :)
Related
I am trying to apply a texture to an item created with forge for Minecraft but for some reason it wouldn´t load. I am new to this so I cant recognize where the problem is, so I will upload multiple lines of code.
This code is from itemInit.Java:
package com.example.examplemod.init;
import com.example.examplemod.ExampleMod;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.registries.ObjectHolder;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
#Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID, bus = Bus.MOD)
#ObjectHolder(ExampleMod.MOD_ID)
public class itemInit {
public static Item example_item = null;
#SubscribeEvent
public static void registerItems(final RegistryEvent.Register<Item> event) {
event.getRegistry().register(new Item(new Item.Properties().group(ItemGroup.MISC)).setRegistryName("example_item"));
}
}
This is the example_item.json:
{
"parent":"items/generated",
"textures":{
"layer0":"examplemod:items/example_item"
}
}
en_us.json:
{
"item.examplemod.example_item":"Super Seed"
}
And here is a picture of the project hierarchy:Project hierarchy
In your example_mod.json file, line 2, change it to
"parent": "item/generated",
Inside the directory assets\examplemod\models, you have two folders named blocks and items. Change their names to 'block' and 'item' without the quotation marks. If you encounter any other problem, post your error log using pastebin or some other site. Also, create a github repo for your project and then post the link here :)
Also, I recommend using DeferredRegister class and RegistryObjects to register your custom items.
Syntax:
public static final DeferredRegister<Item> NAME = new DeferredRegister<>(ForgeRegistries.ITEMS, "Your Mod ID");
public static final RegistryObject<Item> Example_item = NAME.register("Registry_Name", () -> new Item(new Item.Properties()));
I am using Vaadin in Java and I am following this tutorial: Vaadin Upload
So I have created a new Class name Uploader. But there is some stuff which doesn't work in my code, I put what is not working in ** text **:
import com.vaadin.server.FileResource;
import com.vaadin.ui.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by mflamant on 15/05/2017.
*/
public class Uploader {
final Embedded image = new Embedded("Uploaded image");
**image.setVisible(false);**
class Image implements Upload.Receiver, Upload.SucceededListener{
public File file;
public OutputStream receiveUpload(String filename, String mimeType){
FileOutputStream fos = null;
try{
file = new File(filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e){
e.printStackTrace();
return null;
}
return fos;
}
public void uploadSucceeded(Upload.SucceededEvent event){
image.setVisible(true);
image.setSource(new FileResource(file));
}
};
Image receiver = new Image();
Upload upload = new Upload("Upload image here", receiver);
**upload.setButtonCaption("Start Upload");**
**upload.SucceededListener(receiver);**
Panel panel = new Panel("Image storage");
Layout panelContent = new VerticalLayout();
**panelContent.addComponents(upload, image);**
**panel.setContent;**
}
The error I have is "Can not resolve symbol". Can you explain to me why these lines aren't working?
Upload example doesn't list the whole code of the application. It only includes the code snippets specific to the Upload component itself. These code snippets are not expected to work if you just paste them into your class.
This example is a part of Vaadin Documentation and you're expected to understand the basics at the time you reach this part.
Example code is intended to work as a part of a method that builds a Vaadin component. The particular error is that you can only call methods, like image.setVisible(false) from an executable code block. You can't just paste them in your class declaration, that's not a valid Java code.
Tutorial links to a working code on Github. As you can see it contains all the necessary initialization in place:
public class UploadExample extends CustomComponent implements BookExampleBundle {
private static final long serialVersionUID = -4292553844521293140L;
public void init (String context) {
//... omitted for brevity
basic(layout);
//... omitted for brevity
}
void basic(VerticalLayout layout) {
final Image image = new Image("Uploaded Image");
//the rest of the example code goes here
Please, note that this class alone still doesn't work as a standalone application. This is just one of the components.
So, what you can do now:
Complete Vaadin Tutorial first. This should help you grasp the concepts.
Read the Introduction part of the docs first. This will help you build the working application. Then you can jump to specific components.
Clone Book Examples application from Github and then try to figure out how it works.
I am trying to load an image from my computer into a code to produce a color histogram. My code is compiling but it says that the image is not found, although it is on the Home part of my laptop as 'me.jpg.' Below is the first part of my code, Any tips?
import java.io.*;
import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.*;
public class test {
public test() {
}
public static void main(String[] args) {
PlanarImage image = JAI.create("fileload", "me.jpg"); // Load Image
int [][] imageHistogram = getHistogram(image);
FileWriter writer = null;
File outputFile = new File("test2.txt");
I recommend you store your code and your data (images) in different, proper, places.
Then, open the terminal and set the data directory as the current directory. And invoke the JVM specifying the code directry into the classpath:
java -classpath <directory-of-code> my.class <parameters...>
Update
Also, you could pass the absolute path as a parameter and receive it in your code:
public static void main(String[] args) {
PlanarImage image = JAI.create("fileload", args[0]);
...
And the command line:
java -classpath <directory-of-code> my.class my-home/me.jpg
I'm following along with a youtube tutorial on programming in Java using Slick2D and lwjgl. When I run this code:
package game;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame{
public static final String gamename = "Ham Blaster!";
public static final int menu = 0;
public static final int play = 1;
public Game(String name) {
super(name);
this.addState(new Menu(menu));
this.addState(new Play(play));
}
public static void main(String[] args){
try{
AppGameContainer appgc;
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(640, 360, false);
}catch(SlickException e){
e.printStackTrace();
}
}
#Override
public void initStatesList(GameContainer gc) throws SlickException {
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
}
It throws a security exception saying that the package org.lwjgl is sealed. I've added the lwjgl jar file and the two jar files associated with slick (slick2d and lwjgl . jar) to the build path. Is there a solution?
I had the same problem ones. And I Found that going into the Jar file(s) and opening up the Manifest file, and then removing the
Sealed: true
line entirely fixed my problem. And just so you know there would be no need to re package or something if you open the jar file with e.g. Winrar and then editing the file while it is in the jar.
You probably only need to go into the lwjgl jar files.
Hope it works out for you!
I've got an issue with my .jar file. It runs fine in Eclipse but as soon as I export it, it won't open. I've checked the manifest file and it looks like it's okay.
I've tried exporting it as a runnable jar, as well as just using the jar builder. Nothing worked.
I've tried to run it in command prompt and it says it can't access the jar file... I've searched around a while on here and haven't found an answer yet.
I'm not sure what I'm doing wrong. The only thing I can think of is I'm not getting my images correctly.
I'm using .png files for the program's sprites and here's an example of how I get them for my program.
This code begins the building of a level from a .png file.
public class SpawnLevel extends Level{
public SpawnLevel(String path) {
super(path);
}
protected void loadLevel(String path){
try{
System.out.println("classpath is: " + System.getProperty("java.class.path"));
BufferedImage image = ImageIO.read(SpawnLevel.class.getResource(path));
int w = width = image.getWidth();
int h = height= image.getHeight();
tiles = new int[w*h];
image.getRGB(0,0,w,h,tiles,0,w);
}catch(IOException e){
e.printStackTrace();
System.out.println("EXEPTION FOUND!!! Could not load the level file!");
}
}
protected void generateLevel(){
System.out.println("Tiles: " + tiles[0]);
}
}
I've made one other .jar before for another program and didn't have a problem. Any help would be greatly appreciated.
If it helps, I used this code to display the resource folder path information.
System.out.println("classpath is: " + System.getProperty("java.class.path"));
Here's what my current path for my resources folder looks like. Before I export it from eclipse.
classpath is: C:\Users\name\workspace\Rpg_Game\bin;C:\Users\name\workspace\Rpg_Game\res
After I export to .jar
classpath is: 2ndGameTest.jar
If your images are in your resources package in the src The path you should be using for getResource() is something like
class.getResource("/resources/levels/level1.png")
UPDATE with test program
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestImage {
public static void main(String[] args) throws IOException {
Image image = ImageIO.read(TestImage.class.getResource("/resources/images/image.png"));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
JOptionPane.showMessageDialog(null, label);
}
}