What am I doing wrong with my application(Java)? - java

I am attempting to make a game and I am using a tileset I found(png). I want to be able to break the image up into parts and place them in specific places in the jframe to show movment. But first how would I begin to import the image. All attempts I have had have failed and the image turns to a text file when importing. Please help solve this problem and how would I cut up and arrange the tiles?
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at World.Display.<init>(Display.java:15)
at Main
.main(Main.java:15)
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
World.Display l = new World.Display();
}
}
package World;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Display {
public Display() throws IOException{
BufferedImage img=ImageIO.read(getClass().getResource("assets/tilesetbackground.png/"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setTitle("Another RPG Version: "+Config.Global.Version);
frame.setLayout(new GridLayout(1024,768));
frame.setSize(1024,768);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

I'm sure the correct format of the string should starts from /
I checked it locally. Please take a look to the picture

Related

Setting JFrame icon - can't get suggested solutions in previous threads working

I am trying to set a JFrame icon. I have tried all the suggested solutions here and here , but have not yet had success.
In my method below you can see all the solutions attempted:
1 and 2 do not set an icon (I still see the coffee cup).
3 and 6 get this error:
The method setIconImage(Image) is undefined for the type Icon
4 gets this error:
java.lang.NullPointerException
5 get:
Type mismatch: cannot convert from URL to DocFlavor.URL
My calling class is here:
/Users/lawrence/eclipse-workspace/COA_Application/src/main/java/misc/Icon
My image is here:
/Users/lawrence/eclipse-workspace/COA_Application/Logo.png
(I have also tried COA_Application/src/main/resources/Logo.png)
I am a beginner so apologies if I am being slow. Note also I am using a mac.
package misc;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.google.api.services.sheets.v4.model.Color;
public class Icon {
static String filepath = "/Logo.png";
public void showFrame() throws IOException {
JFrame frame = new JFrame("Icon frame");
//method 4
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(filepath)));
//method 1
//BufferedImage myPicture = ImageIO.read(new File(filepath));
//frame.setIconImage(myPicture);
//method 2
//frame.setIconImage(ImageIO.read(new File(filepath)));
//method 3
//setIconImage(new ImageIcon(filepath).getImage());
//method 5
//URL url = getClass().getResource(filepath);
//frame.setIconImage(imgicon.getImage());
//method 6
//ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("./icon.png"));
//setIconImage(img.getImage());
JPanel panel = new JPanel();
frame.add(panel);
frame.pack();
frame.setSize(new Dimension(600,600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
Icon obj = new Icon();
obj.showFrame();
}
}
Image:
Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info page for embedded resource for how to form the URL.
Of course, the devil is in the details. The (upper / lower) case used for the path and resource name must be exactly as they are on the file system. The easiest way to make getResource(..) to works is to use a path from the root of the class-path. Specifying 'from the root' is done by prefixing the path with /.
Example using latest code (loads the icon from URL):
import java.awt.*;
import javax.swing.*;
import java.net.*;
public class Icon {
public void showFrame() throws Exception {
JFrame frame = new JFrame("Icon frame");
URL url = new URL("https://i.stack.imgur.com/bQ8fP.png");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
frame.pack();
frame.setSize(new Dimension(400,100));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
Icon obj = new Icon();
obj.showFrame();
}
}
Edit:
Given it works perfectly here, I'm starting to think the problem is the particular JRE being used.
I have just built and installed my application and it has the correct icon!

gif image not correct size and not repainting

I found a GIF I want to add into one of my Java programs. When I run it the picture shows up and works, but it leaves a trail behind it (it isn't actually replacing the white behind the picture) and the window is incorrectly sized. I searched before but nothing fixed it.
package tasks;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class duane
{
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://i2.kym-cdn.com/photos/images/newsfeed/000/602/307/4d3.gif");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame("Animation");
f.getContentPane().add(label);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
What would I add in to make the window the correct size and how would I repaint it?

Class can`t declare type

I am going through thenewboston's tutorials and I have an unexpected error. I have tried to do everything that Eclipse suggest, but can't figure it out where the problem is.
this is my Main Class
import javax.swing.JFrame;
class Main {
public static void main(String args[]) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
and this is GUI Class
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton reg;
private JButton custom;
public Gui(){
super("The title");
setLayout(new FlowLayout());
reg = new JButton("Regular Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon a = new ImageIcon(getClass().getResource("a.png"));
custom = new JButton("Custom", b);
custom.setRolloverIcon(a);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
}
Thanks brothers for helping me out!
You've posted a couple of different stacktraces with the error on different line numbers but the code seems to have moved around. The error itself is talking about a NullPointerException in a constructor for ImageIcon. Not really anything to do with the JButton so the tags are misleading.
Basically you're looking up an image location of b.png and a.png. If these two files don't exist then you'll get a runtime exception like you have. The quick fix is to add these two images to the project so they are found.
A more robust solution would be to handle the exception and either output a more meaningful error or just carry on without the icon on the gui.

Improving of java code - scale image

I am still pretty new to Java and tried to find information about how to include a picture in a frame.
Some pictures have the right size and fit in my screen, however other definitely have to be scaled to make them fit in.
So far I got this, is there a way to improve it and scale the picture down? For example half of the size it has now? What do I have to change in the code?
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
class DisplayImage {
public static void main(String avg[]) throws IOException
{
DisplayImage img =new DisplayImage();
}
public DisplayImage() throws IOException
{
BufferedImage img=ImageIO.read(new File("C://Address.jpg"));
BufferedImage ret = new BufferedImage(32,32,BufferedImage.SCALE_FAST);
ret.getGraphics().drawImage(img,0,0,32,32,null);
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(200,300);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You can scale an image with the following code:
image.getScaledInstance(-1, 25,
java.awt.Image.SCALE_SMOOTH);

Why can't Java parse this image?

I'm trying to load this image using ImageIO.read() using the 1.7.0u JVM:
http://taste-for-adventure.tablespoon.com/files/2012/02/2012-02-05-poll-hotdog-275w.jpg
Chrome has no trouble with it, but Java throws the following exception:
java.lang.NullPointerException
at java.awt.color.ICC_Profile.intFromBigEndian(ICC_Profile.java:1770)
at java.awt.color.ICC_Profile.getNumComponents(ICC_Profile.java:1462)
at sun.java2d.cmm.lcms.LCMSTransform.<init>(LCMSTransform.java:122)
at sun.java2d.cmm.lcms.LCMS.createTransform(LCMS.java:76)
at java.awt.color.ICC_ColorSpace.fromRGB(ICC_ColorSpace.java:222)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.setImageData(JPEGImageReader.java:635)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:550)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:295)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:427)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:543)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:986)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:966)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1400)
Is Java's image reader known to be fragile? Is there a more robust Java library for loading images?
Hi please try the following, This runs without any problem in java 1.6? Does it give the same exception?
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ShowImage {
public static void main(String[] args) {
JFrame frame = new JFrame("Debug Frame");
frame.setSize(200, 200);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image image = null;
JLabel label = null;
try {
image = ImageIO.read(new File("c:/scratch/hotdog.jpg"));
label = new JLabel(new ImageIcon(image));
} catch (IOException e) {
label.setText("Image loading failed");
}
frame.add(label,BorderLayout.CENTER);
frame.setVisible(true);
}
}
Not sure what you're trying to do, what's exactly in your code, and which line throws a NullPointerException but the following works fins in Java 6:
String imageUrl = "http://taste-for-adventure.tablespoon.com/files/2012/02/2012-02-05-poll-hotdog-275w.jpg";
BufferedImage bi = ImageIO.read(new URL(imageUrl));
if(bi != null)
System.out.println("Image Loaded!");
else
System.out.println("Something's wrong...");
Sorry but I cannot test it in Java 7 right now...

Categories