I have created a android project with LibgdGdx where i create a AssetManager class, where I load all the assets I need, but when I run the project i have an error when a ttf file is loading. The code of the AssetManager:
public AssetManager manager;
public AssetsManager(){
manager = new AssetManager();
loadAssets();
}
public void loadAssets(){
loadTtf("assets/Birds.TTF");
}
void loadTtf(String path){
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, "assets/Birds.TTF", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter font = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
font.fontFileName = path;
font.fontParameters.size = 20;
manager.load(path , BitmapFont.class, font);
}
I try to load the ttf file through this code
BitmapFont font = manager.manager.get("assets/Birds.TTF",BitmapFont.class);
Part of the error I have:
com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: assets/Birds.TTF (Internal)
at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:579)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:380)
at com.poum.game.Main.render(Main.java:33)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1649)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1354)
As i have said, the project is run on Android
Thank you for everything
String path="Birds.TTF"; //can be inside nested folder
String fileName = "Birds.TTF" ; // it can be any name with extension, will use to load and retrieve
Load in this way :
manager=new AssetManager();
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".TTF", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter parms = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
parms.fontFileName = path; // path of .TTF file where that exist
parms.fontParameters.size = 20;
manager.load(fileName, BitmapFont.class, parms); // fileName with extension, sameName will use to get from manager
manager.finishLoading(); //or use update() inside render() method
Retrieve font from AssetManager
BitmapFont font=manager.get(fileName,BitmapFont.class);
EDIT
From your screenshot, I got your file name Birds.ttf not Birds.TTF
so change
String path = "Birds.ttf";
String fileName = "Birds.ttf"
Android file-system is case sensitive.
Run Configuration for desktop module should be like this :
Related
Is it possible to use the FileObject::findFiles method or similar to search in ZIP files which are stored in a folder? Or do I have to open the zipfiles by myself?
FileObject root = vfs.resolveFile(file:///home/me/test/vfsdir);
// shows everything except the content of the zip
FileObject[] allFiles = root.findFiles(Selectors.SELECT_ALL);
// should contain only the three xmls
FileObject[] xmlFiles = root.findFiles(xmlSelector);
VFS Directory-Tree
/ (root)
/folderwithzips
/folderwithzips/myzip.zip (Zipfile not a folder)
/folderwithzips/myzip.zip/myfile.xml
/folderwithzips/myzip.zip/myfile2.xml
/folderwithzips/other.zip
/folderwithzips/other.zip/another.xml
Sadly it's not possible to search through the content of zips in a VFS as if they were folders.
So I have to load every zip manually and execute my selector on the content.
This small method does the trick for me.
public static void main(String[] vargs) throws FileSystemException {
FileSystemManager manager = VFS.getManager();
FileObject root = manager.resolveFile("/home/me/test/vfsdir");
List<FileObject> files = findFiles(root, new XMLSelector());
files.stream().forEach(System.out::println);
}
public static List<FileObject> findFiles(FileObject root,FileSelector fileSelector) throws FileSystemException {
List<FileObject> filesInDir = Arrays.asList(root.findFiles(fileSelector));
FileObject[] zipFiles = root.findFiles(new ZipSelector());
FileSystemManager manager = VFS.getManager();
List<FileObject> filesInZips = new ArrayList<>();
for (FileObject zip: zipFiles){
FileObject zipRoot = manager.createFileSystem(zip);
Stream.of(zipRoot.findFiles(fileSelector)).forEach(filesInZips::add);
}
return Stream.concat(filesInDir.stream(),filesInZips.stream()).collect(Collectors.toList());
}
Hi I'm currently stuck on trying to load a .ttf font, I'm getting a GdxRuntimeException with the message: Couldn't load dependencies of asset: coastershadowfont
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter params = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
params.fontFileName = "fonts/coastershadow.ttf";
params.fontParameters.size = 30;
assetManager.load("coastershadowfont", BitmapFont.class, params);
try {
assetManager.finishLoadingAssets();
} catch (Exception exception) {
System.out.println(exception.toString());
}
Pass fileName with extension, when you call load(...) method on AssetManager. FileName should be anything with extension.
assetManager.load("coastershadowfont.ttf", BitmapFont.class, params); //Adds the given asset to the loading queue of the AssetManager.
assetManager.finishLoading(); // triggered to execute task
And get BitmapFont with fileName that you specified at loading time.
font= assetManager.get("coastershadowfont.ttf",BitmapFont.class);
I have coded a program in Eclipse and it works properly when I run in that.
public static void initialize() throws IOException{
JTextField tfQrText;
int size = 250;
File qrFile;
BufferedImage qrBufferedImage;
JFrame gui = new JFrame("qrCode Generator");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(250, 340);
gui.setLayout(new BorderLayout());
gui.setResizable(false);
File iconFile = new File(VisualQrCodeGenerator.class.getResource("icon.png").getFile());
BufferedImage iconBuffered = ImageIO.read(iconFile);
gui.setIconImage(iconBuffered);
JButton generate = new JButton("Generate qrCode");
gui.add(generate,BorderLayout.SOUTH);
tfQrText = new JTextField();
PromptSupport.setPrompt("Enter Your Text ... ", tfQrText);
gui.add(tfQrText,BorderLayout.NORTH);
qrFile = new File(VisualQrCodeGenerator.class.getResource("qrCodeImage.png").getFile());
qrBufferedImage = ImageIO.read(qrFile);
ImageIcon qrImageIcon = new ImageIcon(qrBufferedImage);
JLabel image = new JLabel();
image.setIcon(qrImageIcon);
image.setHorizontalAlignment(JLabel.CENTER);
gui.add(image,BorderLayout.CENTER);
gui.setVisible(true);
generate.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if(!(tfQrText.getText().equals(""))){
//create() Method make the QRcode Image
create();
}
}
});
The Error occurs on line:
BufferedImage iconBuffered = ImageIO.read(iconFile);
When I make it .jar file, it says:
My Project structure is like this:
+src
+qrCodeGenerator
-VisualQrCodeGenerator
-icon.png
-qrCodeImage.png
The code is running properly and without any error in program and I can work with it. But when I make it .jar file, it errors me as I uploaded it image.
This is normal: you can't access a classpath resource as a File object. This is because it is embedded inside a JAR. It works inside your IDE because resources are typically stored inside a temporary folder (and not inside a JAR).
You need to access it with an InputStream using Class.getResourceAsStream and use ImageIO.read(InputStream) instead.
As such, change your code to:
qrBufferedImage = ImageIO.read(VisualQrCodeGenerator.class.getResourceAsStream("qrCodeImage.png"));
and
iconBuffered = ImageIO.read(VisualQrCodeGenerator.class.getResourceAsStream("icon.png"));
Ok, I am not sure what I am doing wrong here but I am not able to reference images in my NB Java project. My code is basically the same as this:
ImageIcon i = new ImageIcon("Resources/character.png");
I Have a package called Resources in my src, and character.png in that package, so what am I doing wrong?
If your file is within the class path (eg. <project>/src/Resources/character.png), load it as resource:
Here's an example:
public class Example
{
public ImageIcon loadImage()
{
// Note the '/'
final String path = "/Resources/character.png";
// Load the image as a resource
ImageIcon icon = new ImageIcon(this.getClass().getResource(path));
// Return the result
return icon;
}
}
// ...
Example e = new Example();
ImageIcon icon = e.loadImage();
I am following the article http://www.ibm.com/developerworks/library/os-eclipse-dynamicemf/ to load dynamically metamodels.
I load the model instanced document using this
ResourceSet load_resourceSet = new ResourceSetImpl();
// ResourceSet load_resourceSet2 = new ResourceSetImpl();
/*
* Register XMI Factory impl ementation using DEFAULT_EXTENSION
*/
load_resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", //$NON-NLS-1$
new XMIResourceFactoryImpl());
/*
* Add bookStoreEPackage to package registry
*/
load_resourceSet.getPackageRegistry().put("http:///com.ibm.dynamic.example.bookstore.ecore",
bookStoreEPackage);
// load_resourceSet2.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", //$NON-NLS-1$
// new XMIResourceFactoryImpl());
/*
* Load the resources using the URI
*/
Resource modelo_esquerda = load_resourceSet
.getResource(URI.createURI("./BookStore.xmi"), true);
But, I´ve got this error message
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'BookStore' is not found or is abstract. (.\BookStore.xmi, 9, 34)
The XMI file already exists on directory.
What I can do?
Thank you
You can try the following, it worked well for me:
XMIResourceImpl resource = new XMIResourceImpl();
File source = new File(xmlName.xml);
resource.load(new FileInputStream(source), new HashMap<Object,Object>());
Data data = (Data) resource.getContents().get(0);
Where Data is your model.