i am trying to load an image using this methods.
I am asking for my image here:
private BufferedImage image = controller.loadImage("/paddle.png");
And the loadImage method is
public BufferedImage loadImage(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getResource(path));
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
I added "res" to my class folder also but nothing seems to work.
My project explorer looks like this : http://prntscr.com/hmar35
EDIT: I also tried URL loading but failed to work either,the code for URL loading is:
public BufferedImage loadImage(String path) {
BufferedImage image = null;
try {
URL link = Controller.class.getResource(path);
image = (BufferedImage) Toolkit.getDefaultToolkit().getImage(link);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
Try using
ImageIO.read(getClass().getClassLoader().getResourceAsStream(path));
where path = "paddle.png" in your case.
If your res folder is set as a source folder, and "paddle.png" is located directly inside of the res folder, you do not need to add a "/" to the file path.
Note: Also make sure you refresh your project to make sure all files are recognized.
Related
Locally, everything works well for me.
When I build a project and run it on the server it will not load images.
How do I change it to load as getInputStream, or some other way to work in a .jar file?
I use this code:
private final Path root = Paths.get("images");
#Override
public Resource load(String filename){
try{
Path file = root.resolve(filename);
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
} else {
throw new RuntimeException("Could not read the file!");
}
} catch (MalformedURLException e) {
throw new RuntimeException("Error: " + e.getMessage());
}
}
I tried to use from this link, but it won't show me the image after BufferedReader:
https://www.baeldung.com/java-classpath-resource-cannot-be-opened
Below is the code that I am trying to use.
InputStream in = new BufferedInputStream(Tester.class.getClassLoader().getResourceAsStream("appResources/img/GESS.png"));
Image image=null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(image);
'image' comes null when I am printing it.
Try this:
InputStream in = Tester.class.getResourceAsStream("your/path");
Image image=null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(image);
The value or your/path is either appResources/img/GESS.png or /appResources/img/GESS.png depending on your maven configuration and the directory you setup for your project.
For instance, if you add the following entry to your pom.xml file:
<resources>
<resource>
<directory>src/main/appResources</directory>
</resource>
</resources>
Then, you can get the same resource by using a shorter path since your program knows where to look for resources:
InputStream in = Tester.class.getResourceAsStream("img/GESS.png");
Image image=null;
try {
image = ImageIO.read(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(image);
More info here and here.
you can use InputStream instream=Thread.currentThread().getContextClassLoader().getResourceAsStream("appResources/img/GESS.png");
you use like this
InputStream instream=Thread.currentThread().getContextClassLoader().getResourceAsStream("appResources/img/GESS.png");
Here are two utility methods I'm using for image loading.
public static Image getImage(final String pathAndFileName) {
try {
return Toolkit.getDefaultToolkit().getImage(getURL(pathAndFileName));
} catch (final Exception e) {
//logger.error(e);
return null;
}
}
public static URL getURL(final String pathAndFileName) {
return Thread.currentThread().getContextClassLoader().getResource(pathAndFileName);
}
I am trying to write a method that can help me declare variables more easily using the method's arguments. I'm very new to java and I'm trying to make a game that would have a decent amount of images and I want to know if there is an easier way to create these BufferedImage Variables.
currently my code looks like this
public class Painter extends JPanel {
BufferedImage sprite = new
BufferedImage(60,60,BufferedImage.TYPE_INT_ARGB);
public void createBufImg() {
try {
sprite = ImageIO.read(new File("Images/SpriteSheet.png"));
System.out.println("File read");
} catch (IOException e) {
System.out.println("Could not read image");
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
createBufImg();
g.drawImage(sprite,20,20,this);
}
}
and I'm looking to see if there is a way to declare the variables so it could be called more like this
sprite = new createBufImg(Images/SpriteSheet.png);
Sure. Make the method take a String (filePath) as a parameter. Set the return type to BufferedImage. Then use that string in the constructor of file.
public BufferedImage createBufImg(String filePath) {
try {
return ImageIO.read(new File(filePath));
} catch (IOException e) {
System.out.println("Could not read image");
}
return null;
}
You can then call the method like this:
sprite = createBufImg("Images/SpriteSheet.png");
Just leave out the new in the createBufImg call, and make it return a BufferedImage that will be created in the method:
public BufferedImage createBufImg(File imagePath) {
try {
return ImageIO.read(imagePath);
} catch (IOException e) {
// handle exception, and either throw an exception or return something appropriate
}
}
...
BufferedImage myImage1 = createBufImage(path1);
BufferedImage myImage2 = createBufImage(path2);
OK so I have a .wav in a resource file I made called sound. The path for the file name I want is /music/sound/One.wav . I tried to replace this with the path I had for the old path I had when i didn't have it in the resource file. I want to do it this way so i can make a jar file and have people play it. The code part I have is:
public class AL implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
InputStream in;
try {
String wav = "C:\\Users\\Mike\\workspace\\music\\sound\\One.wav";
in = new FileInputStream(wav);
AudioStream audio = new AudioStream(in);
AudioPlayer.player.start(audio);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}}}
I have been reading around and way something about get class and resource but had no luck. Help would be great, thanks in advance.
Try doing
try (InputStream in = this.getClass().getResourceAsStream("sound/One.wav")) {
if (in != null) {
AudioStream audio = new AudioStream(in);
AudioPlayer.player.start(audio);
}
}
or maybe just "One.wav", depending on how you set up your classpath. Since this is in Eclipse, you probably want sound to be a source directory. When you migrate this to a jar, make sure it either contains sound/One.wav or One.wav in that path.
I have a program that uses OpenCV to make some pictures with the webcam.
When I run the application in my IDE(NetBeans) it works like a charm, but when I try to run the jar file it doesn't even show what the webcam sees in our JFrame. Does anyone know how to solve this?
public void run(){
try {
grabber = new VideoInputFrameGrabber(0);
grabber.start();
while (active) {
IplImage originalImage = grabber.grab();
Label.setIcon(new ImageIcon( originalImage.getBufferedImage() ));
}
grabber.stop();
grabber.flush();
} catch (Exception ex) {
//Logger.getLogger(ChPanel.class.getName()).log(Leve l.SEVERE, null, ex);
}
}
public BufferedImage saveImage(){
IplImage img;
try {
//capture image
img = grabber.grab();
// save to file
File outputfile = new File(Project.getInstance().getFileURLStr() + " capture" + fotoCount++ + ".jpg");
ImageIO.write(img.getBufferedImage(), "jpg", outputfile);
//get file and set it in the project library
BufferedImage ImportFile = ImageIO.read(outputfile);
Project p = Project.getInstance();
MainScreen ms = MainScreen.getInstance();
ImageIcon takenPhoto = new ImageIcon(ImportFile);
p.setNextImage(takenPhoto);
ms.setPanels();
return ImportFile;
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
When I run it out of CMD, it does show the image on my JFrame. But when I try to take the picture it shows this message: "Failed to write core dump. Minidumps are not enabled by default on client version of windows"
I already found the problem, I had about 3 versions of Java installed, wich propably wasn't okay :p