Loading an image - java

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.

Related

Java ImageIO, Can't read input file

Some years ago I took a couple of courses in programming. Now I'm in a situation where some very basic image processing might save me loads of time. Unfortunately I'm a bit stuck trying to read an image. I haven't done this before and basically just copied some code I found, but I get "javax.imageio.IIOException: Can't read input file!". I've tried moving the image "Test.jpg" and change the path. I don't understand what is causing the issue. I use macOS, not sure if this is relevant.
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MyImage{
public static void main(String args[])throws IOException{
BufferedImage image = null;
File f = null;
try {
f = new File("Users/simonprobert/eclipse-workspace/LineLengthCounter/src/Test.jpg");
image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Reading complete.");
} catch(IOException e){
System.out.println("Error: "+e);
}
}
}
(Currently I'm just trying to read the image, the rest is not important at this point)

Java robot class doesn't work after being exported [Solved][Reason: Window scaling ]

So I have a class that utilizes java's robot class to take a screen picture and make 5 smaller pictures from it.
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
public class screencap {
static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
public ArrayList<BufferedImage> get() throws AWTException, IOException {
ArrayList<BufferedImage> array = new ArrayList<BufferedImage>();
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
BufferedImage img1 = bufferedImage.getSubimage(481,1039,190,31);
BufferedImage img2 = bufferedImage.getSubimage(682,1039,190,31);
BufferedImage img3 = bufferedImage.getSubimage(883,1039,190,31);
BufferedImage img4 = bufferedImage.getSubimage(1085,1039,190,31);
BufferedImage img5 = bufferedImage.getSubimage(1286,1039,190,31);
array.add(img1);
array.add(img2);
array.add(img3);
array.add(img4);
array.add(img5);
return array;
}
}
So what I can do is use the class above to get an array of 5 buffered image like so
screencap nsc = new screencap();
try {
ArrayList<BufferedImage> bfl = nsc.get();
} catch (AWTException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
The problem that I encountered was that the code worked flawlessly when I ran it on eclipse but when I exported it as an executable jar file it didn't work and couldn't even throw an error. I'm pretty sure the code at fault here is screencap.get() but I don't know how to fix it. Can someone tell me what went wrong? Thanks in advance.
SOLVED: I turned off window scaling. Apparently it messes with my program somehow.
Also, when I execute the program from cmd it works even if window scaling is on
#ArnaudClaudel thank you for suggesting I use cmd
The reason my program didn't work is that I have window scaling turned on to 125%. It made my exported program look bigger than it supposed to be, making pictures look blurry and it messes with the function in the question. I fixed it by turning off window scaling. Another solution is to execute the jar file from the cmd.

ImageIO.read() infinite loop

ImageIO.read() just seems to be stuck in an infinite loop.
My code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Texture {
BufferedImage _img;
public Texture(String path) {
try {
_img = ImageIO.read(new File(path));
} catch (final IOException e) {
e.printStackTrace();
}
}
}
Other class:
private Texture _tex;
_tex = new Texture("/res/img.png");
I have tried loading the image this URL and File, none works.
I am using eclipse, on a mac, and I am using LWJGL 3 if that has anything to do with anything.
Hope you can help me! :-)
Instead of using a File, try using an input stream like this:
InputStream stream = Texture.class.getResourceAsStream(path);
_img = ImageIO.read(stream);
I'm actually experiencing a similar issue, but it's related to the creation of BufferedImages.

Java jar File error message Exception in thread "AWT-EventQueue-0"

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.

Error while loading image

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);

Categories

Resources