I'm trying to learn how to program frames in Java and I ran into a problem. When I create a jar file of my code I come up with a blank screen. My previous jar files don't have the same problem. When I ran the jar file through command prompt I got the following error message:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at getResources.getImg(getResources.java:31)
at MyCanvas.paint(gameLoop.java:99)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
…
Here is the code for the main class and MyCanvas class:
class MyCanvas extends JComponent{
public void paint(Graphics g){
getResources get = new getResources();
BufferedImage titleImg = get.getImg("Title");
BufferedImage testImg = get.getImage("");
g.drawImage(titleImg, 0, 0, null);
g.drawImage(testImg, 0 , 0, null);
}
}
Here is the code for my "getResources" class:
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.Font;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/**
* Write a description of class getResources here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class getResources{
public void getResources(){
}
public BufferedImage getImg(String path){
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource("/Sprites/" + path + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public BufferedImage getImage(String path){
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource("/Sprites/test.png"));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public AudioStream getSeven(){
InputStream in;
AudioStream as = null;
try{
in = this.getClass().getResourceAsStream("/Music/seven.wav");
as = new AudioStream (in);
}catch(Exception e){
e.printStackTrace();
}
return as;
}
}
I am pretty sure that the error is in the "getImg" method and the way it retrieves images from the source folder. I removed all the lines of codes that had references to that method and the jar file ran just fine. However, the "getImage" class runs just fine in the jar file. Maybe something about passing a string to get the path of the image creates the error? I'd like to know if anyone knows what is causing the error and if I should even be concerned by not being able to run jar files, I do like using them.
Related
I have tested my program on windows platform, works perfectly. But when I try to run it on Raspbian, Unix, it doesn't go further after iplcvLoadimage.
What could be the problem?
Here is my code
mport com.googlecode.javacv.cpp.opencv_core.CvPoint;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvSize;
import static com.googlecode.javacv.cpp.opencv_core.cvZero;
import static com.googlecode.javacv.cpp.opencv_core.cvMinMaxLoc;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_32F;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMatchTemplate;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_TM_SQDIFF;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.opencv.core.Core;
public class MatchTemplateTest {
public static HashMap<String,Double> getPatternMatch(String img)
{
ArrayList<String>names=new ArrayList<>();
HashMap<String,Double> MatchMap=new HashMap<>();
File []f=null;
try
{
String path=new File(new File(".").getCanonicalPath()+"/Output"+"/Logo").getAbsolutePath();
f=new File(path).listFiles();
} catch (Exception e) {
}
IplImage src=null;
try {
// this is where the problem is
src= cvLoadImage(img,0);
// can not process further
} catch (Exception e) {
System.out.println("ERROR "+e);
}
If the problem is with reading the image, then there are two possiblities
The image does not exist, or the path to the image is wrong. In *nix the path is separated with a slash '/', e.g. /home/asharma/data/myimage.pgm, but in windows it is separated with a backslash, e.g. C:\User\Data\My Image.pgm
The image decoder for the image does not exist in the version of the library. For example, if the image is in PNG format, but opencv was not compiled with PNG, then you cannot read the image.
what is the output of System.out.println("ERROR "+e);?
Here is my code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import model.Map;
public class MyView {
private BufferedImage img = null;
private static MyPanel panel;
//init image
try{
img = ImageIO.read(new File("/src/minepic/start.png"));
} catch (IOException e){
System.out.println(e.getMessage());
}
}
I want to load an PNG image from the src directory but I don't know why it doesn't work, anyone can help me?
Error in command "try catch" and NetBeans say "unreported exception IOexception; must be caught or declared to be thrown"
One more, even i declared img as a BufferedImage before, but in command "try catch" img just like didn't declared because in NetBeans it doesn't become green, still black.
There are few issues with the code:
It is incomplete. The braces are not matched.
you have written code outside main method (possible but not recommended)
To read the image from src folder (which is a part of your class path) use the below snippet:
Inputstream is = MyView.class.getResourceAsStream("minepic/start.png");
if(is==null){
is = MyView.class.getClassLoader().getResourceAsStream("minepic/start.png");
}
img = ImageIO.read(is);
How do I get the images I stored in my web pages folder out so I can read them into a buffered image?
I found many explanations online on how it works but it's so confusing!
Maybe someone could help me by explaining it to me in a scenario familiar to me?
This is my server folder tree:
I want to read PNG pictures from images in Web Pages from within my ItemStorage class.
Here's what that class looks like:
import java.util.ArrayList;
import java.util.List;
import be.pxl.minecraft.model.Item;
import com.sun.jersey.spi.resource.Singleton;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
#Singleton
public class ItemStorage {
private ImageStorage test;
private HashMap<String, Image> categories;
private HashMap<String, BufferedImage> images;
private List<Item> recipesList;
public ItemStorage() {
File directory = new File("/images");
if (directory.isDirectory()) { //FILE PATH NOT A DIRECTORY
BufferedImage img = null;
for (File f : directory.listFiles()) {
try {
img = ImageIO.read(f);
images.put(f.getName(), img);
} catch (IOException ex) {
Logger.getLogger(ItemStorage.class.getName()).log(Level.SEVERE, "Error loading image", ex);
}
}
}
recipesList = new ArrayList<Item>();
//BufferedImage air = getImage("air"); //TRIED DIFFERENT APPROACH, SEE getImage()
//Armor
recipesList.add(new Item(7, 2, getImage("diamond_boots"), "Boots (Diamond)",
"0,0,0,1,0,1,1,0,1", String.format("%d,%d", getImage("air"), R.drawable.diamond_ingot )));
}
public void setItems(List<Item> list) {
recipesList = list;
}
public List<Item> getItems() {
return recipesList;
}
#Path("/images")
#Produces("image/png")
public Response getImage(String imageName) { //TRYING TO HTTP TO THE IMAGE
BufferedImage img = null;
try {
File imageFile = new File(imageName + ".png");
img = ImageIO.read(imageFile);
return Response.ok(img).build();
} catch (IOException ex) {
Logger.getLogger(ItemStorage.class.getName()).log(Level.SEVERE, "Error loading image", ex);
} finally {
return Response.ok(img).build();
}
}
}
As you can see, I tried to call the images both through HTTP and a simple IO directory.
This is a restful server running under tomcat 7.0.41.0
Please collaborate.
what puzzles me with your approach is, that you access a file from within your service, that is supposed to be located at the "root" ("/") directory. This is not inside your application.
In the rest service, there is no "context", as you have it in the servlet world. You need to identify the images folder somehow using a config option. You can also check the documentation of your REST server to see how to access the MessageContext and HTTPRequest. Then you can use them to access your web-application's runtime folder and access the images.
Actually, he cannot find java.io.
It recognizes import java.io.*;
import java.io.IOException;
but when I try InputStream = this.getClass().getResourceAsStream("users.xml");, i get the following error:
C:\Users\cpantaziu\Documents\NetBeansProjects\url\src\main\java\com\mkyong\common\controller\FailRegisterController.java:[120,12] cannot find symbol
symbol : variable InputStream.
Tools -> Java Platforms ses my jdk 1.6.
Why am I getting this?
you have to assign a reference variable:
InputStream ref= this.getClass().getResourceAsStream("users.xml");
You must name a variable:
InputStream is = this.getClas()...
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
*
* #author mbarb
*/
public class LoadSave {
public static BufferedImage GetPlayerAtlas(){
BufferedImage img = null;
InputStream is =
LoadSave.class.getResourceAsStream("Res/player2.png");
try {
img = ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
}//try catch .close()
return img;
}
}
I'm have the same issue here in the code above.
i just get this:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)
at Utilities.LoadSave.GetPlayerAtlas(LoadSave.java:19)
at Entities.Player.loadAnimations(Player.java:102)
at Entities.Player.<init>(Player.java:29)
at com.mycompany.platformgame.Game.initClasses(Game.java:32)
at com.mycompany.platformgame.Game.<init>(Game.java:21)
at com.mycompany.platformgame.PlatformGame.main(PlatformGame.java:7)
Command execution failed.
This is the code I'm using to load an image called "boat.png"
#Override
public void paint(Graphics g) {
BufferedImage img = null;
try {
img = ImageIO.read(new File("boat.png"));
} catch (IOException e) {
System.out.println("Can't load the image");
}
}
These two lines generate errors
BufferedImage img = null;
img = ImageIO.read(new File("boat.png"));
Although I've included
import java.io.*;
import java.awt.*;
Please help me find the error !!
The ImageIO class is in the javax.imageio package.
Try importing that package:
import javax.imageio.ImageIO;
I believe the problem is that BufferedImage is in the java.awt.image package, not just java.awt, so you need:
import java.awt.image.*;
or
import java.awt.image.BufferedImage;
Some IDEs will help you fix this error by suggesting which package to import - Eclipse certainly does.
EDIT: You also need to import javax.imageio.* or javax.imageio.ImageIO - but you definitely need one of the earlier imports too...
import java.awt.image.BufferedImage
this import statement should be included.