Error When trying to add a image on android studio (java) - java

My script is post to show a image each time I run it on android studios but I keep on getting an error each time I try to run the script it's post to show funny picture of trumps face. I am not really sure why I keep on getting an error each time i run the script because it works when I put in a image of a smiley face.
Here's the script:
package com.udacity.gamedev.logging;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class LoggingDemo extends ApplicationAdapter {
// TODO: Give your ApplicationListener a log TAG]
public static final String TAG = LoggingDemo.class.getName();
SpriteBatch batch;
Texture img;
#Override
public void create() {
batch = new SpriteBatch();
img = new Texture("Trump.jpg");
// TODO: Use Gdx.app to find what ApplicationType we're running
// TODO: Use Gdx.app to log the result
Gdx.app.log(TAG, "We're running on" +
Gdx.app.getType()
);
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
}
// TODO: Run the Desktop app and find your log message
// TODO: Run the Android app and find your log message
}
Here's the error:
enter image description here

There is proper protocol you must follow to be able to use the image in android coding. Which involves using resources files. Please follow this guide to better acquaint yourself with adding resources (in this case images) to your android project.
http://www.higherpass.com/android/tutorials/working-with-images-in-android/

You have to put the file Trump.jpg under the assets folder of the android project. Did you do that? Based on the exception it isn't there.
And use the command
new Texture(Gdx.files.internal("Trump.jpg"));
to initialize the Texture object.
If it's still not working clean your projects. I don't know Android Studio, but in Eclipse you can select the projects and click on Project/Clean to clean and rebuild the application.

Related

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.

libGDX HTML5 Project Builds Differently on Different Machine

This is a strange one! On my old desktop, I was able to build and run my libGDX project for HTML5 using the gradlew html:dist gradle command.
I uploaded the files to my site and everything worked well.
But on my laptop I synced the project via GitHub to Android Studio, ran the same command and it appears that the default ApplicationListener runs instead of my own custom one.
The only thing that appears is a red rectangle: http://johnathongoss.com/jaggybattles/
The desktop and android versions run and build fine on my laptop.
I'm using Android Studio and the latest version of libGDX.
Here is some code:
public class HtmlLauncher extends GwtApplication {
#Override
public ApplicationListener createApplicationListener() {
return new MyGame();
}
#Override
public GwtApplicationConfiguration getConfig () {
GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(1280, 720);
return cfg;
}
}
//MyGame Class (Doesn't seem to get this far?)
#Override
public void create() {
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
setScreen(new LoadingScreen(this)); //Loads the assets and sets Main Menu Screen
}
I've had this problem before but it was because I didn't change the createApplicationListener() method to return my own custom ApplicationListener. Now it seems to return the right one, I really don't know what could be causing this.
I should point out I can't access my desktop computer anymore due to relocating.
Any advice would be gratefully received!
Edit: Loading Class
https://gist.github.com/jaggygames/79b1bd821f4203aadecfe5e8ba925150
Assets Class:
https://gist.github.com/jaggygames/5b863f1692b0974b769d5694b5a3334f
MyDistance Font Class
https://gist.github.com/ea7b5965488cd5672e9e7c20917df9a1

Java OpenCV asking for manycam to be open when manycam is already open?

So I decided i wanted to learn about image processing and vision tracking and such so installed opencv and got it working in eclipse for Java. However, when i try to take an image all I get is an image that says "Please start Manycam or choose another video source"
Here is the code i am using:
package testests;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
public class Hello
{
public static void main(String[] args)
{
System.out.println(System.getProperty("java.library.path"));
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture cap = new VideoCapture(0);
if (!cap.isOpened())
System.out.println("Not connected to webcam ):");
else
System.out.println("Connected to camera: " + cap);
Mat frame = new Mat();
cap.retrieve(frame);
System.out.println(frame);
Highgui.imwrite("test.jpg", frame);
cap.release();
}
}
any help would be greatly appreciated!
Edit:
I got it working with manycam. However, i would like to use it without manycam and i cannot figure out how to. each time i try that image comes back up
So to get it working i captured camera index 1 instead of 0. I'm not entirely sure why that worked, but it worked.

Stanford CS106a newbie - Having trouble compiling in Eclipse

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.

drawImage() for .png files doesn't work in Java applet?

I made this pacman game using an applet. When the applet finishes loading, it is meant to display a 'start menu' graphic i made which says 'Double click to start the game', etc. The graphic would take up the whole 400x400 applet. Here's the code:
public void paint(Graphics g)
{
if (! isRunning)
{
switch (result)
{
case 0:
showStartScreen(g);
break;
case 1:
showWonScreen(g);
break;
case -1:
showLostScreen(g);
break;
}
return;
}
//Code for rendering other stuff if game is here
}
showStartScreen:
public void showStartScreen(Graphics g)
{
Image intro=img.getImg("pacman-intro.png");
g.drawImage(intro, 0, 0, this);
}
This works fine when I run this locally on Eclipse, but in the web browser, when the applet is loaded I just see a blank box where the java's loading animation previously was. Surprisingly, if I double click randomly where the applet is meant to be, the game starts and works as normal. So this makes me think that the problem is just with drawing .png files using drawImage.
The same thing occurs when you win or lose the game,it just hangs instead of drawing the graphic it needs to.
You can see this here (you can use arrow keys to control)
P.S I am using double buffering here.
Any thoughts..?
EDIT: Code for ImgHelper class which is used to load the img:
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
public class ImgHelper
{
public Image getImg(String file)
{
//Engine is the name of the base applet class
URL url=Engine.class.getClassLoader().getResource("assets/" + file);
return new ImageIcon(url).getImage();
}
}
Ah, I checked the contents of your jar file.
Replace "pacman-intro.png" with "pacman-intro.PNG". It's case-sensitive.
Are the pngs in the right directory? Is "." in the path env-variable on the server?
Try "./pacman-intro.png" i.e.

Categories