Fix Input == null! error to Create an Image - java

I am trying to load a spritesheet, but it is not working. It gives an error Exception in thread "Thread-0" java.lang.IllegalArgumentException: input == null!
I saved the image into folders } res --> Textures --> sheets.png . I made sure the res folder is in the Class Folder by building a path.
Please help
// public class Game
private void init(){
display = new Display(title, width, height);
test = BufferedImageLoader.loadImage("/sheets.png");
Assets.init();
}
private void render(){
bs = display.getCanvas().getBufferStrategy();
if(bs == null){
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.clearRect(0, 0, width, height);
g.drawImage(test, x, 10, null);
//End Drawing!
bs.show();
g.dispose();
}
// public class BufferedImageLoader {
public static BufferedImage loadImage(String path){
try {
return ImageIO.read(BufferedImageLoader.class.getClassLoader().getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}

Figured it out! Make a separate folder with all the images in the source folder. Then, use the link /folder name/ photo name . format (ex. "/Images/mario.png"). This way works.

Related

How to draw image created in try/catch?

I'm learning Java, and I've converted File type into Image using IOException, but how can I use my new Image outside of try/catch?
try {
File obraz = new File("C:\\Users\\ender\\Pictures\\logo.jpg");
Image image = ImageIO.read(obraz);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image);
}
Because now IntelliJ does not recognize image.
In this case - as paintComponent will be called often, and you want to load the image just once, put the image in a field.
private Image image;
...() {
try {
File obraz = new File("C:\\Users\\ender\\Pictures\\logo.jpg");
image = ImageIO.read(obraz);
} catch (IOException ex) {
ex.printStackTrace();
}
}
...() throws IOException {
File obraz = new File("C:\\Users\\ender\\Pictures\\logo.jpg");
image = ImageIO.read(obraz);
}
#Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if (image != null) {
g2.drawImage(image);
}
}
I have shown two solutions:
catching the exception as done now: but one should do something, give an error message to the user that file logo.jpg does not exist
passing the exception on by throws, often the better solution.
The convention is to use #Override as this catches typos like public void paintComponent(Graphics2D g) or public void painComponent(Graphics g).

reading Image fails: Java

Im coding a liitle game for java and want to make the Player look like a rocket, so i try to load the miage and then draw it on Java awt canvas.
Unfortunately i kepp gettin an IOException, but i dont get why... is the image some how wrong?
Thank you ver helpiNg me in advance and here`s the code:
(the image is in the same package as the Player class)
public class Player extends GameEntity {
private BufferedImage img;
public Player(int x, int y, ID id){
super(x, y , id);
}
BufferedImage loadImage(String fileName) {
BufferedImage bi = null;
//System.err.println("....setimg...." + fileName);
try {
bi = ImageIO.read(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Image could not be read");
System.exit(1);
}
return bi;
}
#Override
public void tick() {
x += velX;
y += velY;
}
#Override
public void render(Graphics graphics) {
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
BufferedImage rocketImage = loadImage("rocket.png");
Graphics2D g2d = (Graphics2D) graphics;
g2d.drawImage(rocketImage, at, null);
}
}

Why does my BlueJ jar file load some images, but not others?

I used some of the information around this site to find out to use URLs to get images into a jar file; which I want to be able to be used alone. But when I make the jar file with BlueJ, only some images show up.
Its a blackjack game, and only the table canvas shows up, while no cards ever do. Here's the code:
THIS WORKS (the table):
public class TableComponent extends JLabel{
BufferedImage table;
public TableComponent(){
URL finalTable = getClass().getResource("blackjackTableCanvas.jpg");
try {
table = ImageIO.read(finalTable);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
g2.drawImage(table, 0, 0, null);
}...}
but this does not (the cards):
public class CardRender2 extends JComponent{
BufferedImage image;
String val;
String suit;
String filename;
public CardRender2(Card card) {
this.val = card.value.face;
this.suit = card.suit.toString();
filename = this.fetchCardFileLabel();
URL cardview = getClass().getResource("\\card deck\\" + filename + ".png");
try {
image = ImageIO.read(cardview);
} catch (IOException e) {
e.printStackTrace();
}
}
public CardRender2(){
this.val = null;
this.suit = null;
filename = "DEALER_FIRST_CARD";
URL cardview = getClass().getResource("\\card deck\\DEALER_FIRST_CARD.png");
try {
image = ImageIO.read(cardview);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
g2.drawImage(image, 0, 0, null);
}...}
my cards are in a folder in the directory I try to import into BlueJ, whereas the table is in the directory root. There are 53 cards in there (incl dealer hidden card) and I'd rather not put all of then in the root. I tried to implement them similarly. How can I do this?
looks like changing from
URL cardview = getClass().getResource("\\card deck\\DEALER_FIRST_CARD.png");
to
URL cardview = getClass().getResource("card deck/DEALER_FIRST_CARD.png");
did the trick.

How to copy bufferedImage

I have written the following example to give something runnable of a problem I am having. When you press the button the controlWhichImage switches to 2. The problem is that when it switches from the original image to a copy the image disappears.
public class PainterDemo01 extends JPanel implements ActionListener {
BufferedImage createdImage;
BufferedImage img;
int controlWhichImage;
JFrame mainFrame;
JButton changePicture;
public PainterDemo01(){
changePicture = new JButton("Press");
changePicture.addActionListener(this);
controlWhichImage = 1;
mainFrame = new JFrame();
mainFrame.add(this);
this.add(changePicture);
mainFrame.setPreferredSize(new Dimension(600,600));
mainFrame.setVisible(true);
mainFrame.pack();
img = loadImage();
}
public BufferedImage loadImage(){
img = null;
try {
img = ImageIO.read(new File("/home/gerry/Desktop/100_0647.JPG"));
} catch (IOException e){
System.out.println("no file here");
}
return img;
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
loadImage();
if (createdImage == null){
this.createdImage = new BufferedImage(this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_ARGB);
}
Graphics g2 = this.createdImage.getGraphics();
if (controlWhichImage == 1){
g2.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
g.drawImage(img, 0,0,img.getWidth(),img.getHeight(),null);
g2.dispose();
}
if (controlWhichImage == 2){
//Draw bufferedImage on to to JPanel
g.drawImage(this.createdImage,this.createdImage.getWidth(),this.createdImage.getHeight(),null);
}
}
#Override
public void actionPerformed(ActionEvent e){
controlWhichImage = 2;
repaint();
}
public static void main(String[] args) {
// TODO code application logic here
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PainterDemo01().setVisible(true);
}
});
}
}
The problem is that getGraphics (or better named createGraphics) is called outside the if statement, also for 2, hence both causing a resource leak (as no g2.dispose is called), and also a clean slate.
if (controlWhichImage == 1) {
Graphics g2 = createdImage.getGraphics();
g2.drawImage(img,0,0,img.getWidth(),img.getHeight(),null);
g2.dispose();
}
Also do things like loading the image outside the paint code.
See this question if you want to know how to copy an BufferedImage:
How to copy BufferedImage
Of course! You have to go and paint the image at the outskirts. Please use this
g.drawImage(this.createdImage, 0, 0, this.createdImage.getWidth(),this.createdImage.getHeight(),null);
Use img obj since its already instantiated but not createdImage obj, createdImage contains null since its just declared but not instantiated. If you use createdImage obj means if you perform any operation upon createdImage obj then you will get NullPointerException.
Graphics g2 = this.img.getGraphics();
---------

How to draw on buffered image with java graphics?

My issue: Every time I create graphics from a buffered image and then draw another buffered image to the graphics I get an image that is blank.
My code is as follows.
Graphics2D g2d = atlas.createGraphics();
// images[i] is a buffered image read the fileio
g2d.drawImage(images[i], null, x, y); // Image is not blank, been tested
g2d.dispose();
// then save image
Ironically after trying to create a self contained example which is as follows... it worked. I am not quite sure what I am doing wrong in my code and I am wondering if it is because maybe an image is not static, could an image or another variable not being static affect my drawing?
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) {
String FOLDER_LOCATION = "./Images/";
BufferedImage atlas = new BufferedImage(2048, 2048, BufferedImage.TYPE_INT_ARGB);
BufferedImage redSquare = readImage(FOLDER_LOCATION + "red.png");
Graphics2D g2d = atlas.createGraphics();
g2d.drawImage(redSquare, null, 0, 0);
g2d.dispose();
writeImage(atlas, FOLDER_LOCATION + "atlas.png");
}
public static BufferedImage readImage(String location) {
BufferedImage img = null;
InputStream is = null;
try {
is = new FileInputStream(location);
img = ImageIO.read(is);
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return img;
}
public static void writeImage(BufferedImage bi, String location) {
try {
File file = new File(location);
ImageIO.write(bi, "png", file);
} catch(Exception e) {
e.printStackTrace();
}
}
}
After I save the image I see just a blank 2048 by 2048 image. I printed the entire image out and I get (0, 0, 0), but if I print out any of the image I am drawing to the atlas I get something like (72, 32, 283).
I am not quite sure what I am doing wrong, but my entire source code for this project is here: https://github.com/gemurdock/jTextureAtlas and the branch I am working on is here: https://github.com/gemurdock/jTextureAtlas/tree/alpha.
You have to look at the alpha branch to see my code

Categories

Resources