imageicon

image loading using a JFileChooser into a JFrame

佐手、 提交于 2019-12-07 13:15:41
问题 I am trying to write a code that displays an image selected using a JFileChooser into another JFrame .I tried the following code below but only got the following errors. Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:228) at power.<init>(fCGUI.java:53) at fCGUI.main(fCGUI.java:11) Here is the code: import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import

NullPointerException when using path name to load image file

孤人 提交于 2019-12-07 07:39:58
问题 Looked everywhere and still can't find a solution to this problem while using NetBeans. When I use the following code to load a file by path: Image owl = new ImageIcon(this.getClass().getResource("/images/owl.gif")).getImage(); I get a NullPointerException. I read somewhere where it suggested creating a new folder and making it a source file for the project, but that didn't help. I've tried multiple suggestions that I found on this site and others, but I'm not getting any results. I'm

How to make drawn images transparent in Java

女生的网名这么多〃 提交于 2019-12-06 03:05:22
I got the animation to work in my Snake Clone Game. But the problem based on the picture is that the images do not have transparency(notice the white background of the circle pictures. Programming-wise, is there a fix to be able to include transparency to these drawn images? Here's a picture containing my code and the output of the program. P.S. On a side note, I decided to paste the direct link instead of the IMG code because I cannot seem to get it to display on StackOverFlow. I put an exclamation point in the front of the IMG code but it did not work so here's the direct link. As the other

image loading using a JFileChooser into a JFrame

孤者浪人 提交于 2019-12-06 01:58:38
I am trying to write a code that displays an image selected using a JFileChooser into another JFrame .I tried the following code below but only got the following errors. Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:228) at power.<init>(fCGUI.java:53) at fCGUI.main(fCGUI.java:11) Here is the code: import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class fCGUI { public static void main(String []args)

JButton Image Icon not displaying .png file

梦想的初衷 提交于 2019-12-06 01:53:55
I've been looking everywhere for a solution to this and read some similar posts related to this problem but none of them worked for me. I'm trying to display the image "b.png" on the JButton and when i roll over the button the icon changes. package GUI_JButton; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Gui extends JFrame { private JButton reg; private JButton custom; public Gui() {

Java adding ImageIcon to JLabel

青春壹個敷衍的年華 提交于 2019-12-05 22:00:30
问题 I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame . It has worked in the past for me and now is not, i can't see what I did wrong. I have tried printing the current working directory and changing where I get my image to match that. It is likely that the problem is not getting the image, since my (filefinder or filereader or something like that) can find it without problems, but I cannot correctly add it (the ImageIcon ) to the JLabel , or

Java - Draw text in the center of an image

☆樱花仙子☆ 提交于 2019-12-05 08:37:54
I need to write text in the center of an image. The text to write is not always the same. The code I'm using is here: // Here I first draw the image g.drawImage(img, 22, 15, 280, 225, null); // I get the text String text = photoText.getText(); // Set the text color to black g.setColor(Color.black); // I draw the string g.drawString(text, 79.5F, 220.0F); The problem is that the text isn't at the center of the image, what can I do? I only need to draw the text at the horizontal center. trashgod Using a JLabel is less work, but FontMetrics , shown here , will let you manage the geometry directly.

Java: ImageIcon vs. Image difference

一个人想着一个人 提交于 2019-12-05 00:36:58
Could anyone explain to me in noob way what the difference is betweeen ImageIcon and Image classes/objects in Java? Thanks tenorsax - support Monica Their nature and application is different. Image is an abstract superclass of all classes that represent graphical images. ImageIcon is an implementation of Icon interface that uses Image as its source. Edit: Think of an Image as something that could be rendered and an ImageIcon as something that will be rendered as an Icon when its paintIcon() method is called. Edit: The links above will take you to the JDK 6 api. These links will take you to the

Java getClass().getResource(“file”) leads to NullPointerException

十年热恋 提交于 2019-12-04 23:12:25
I am following the zetcode Snake java games tutorial and always get this error: ImageIcon iid = new ImageIcon(this.getClass().getResource("ball.png")); ball = iid.getImage(); Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source) at snake2.Board.<init>(Board.java:52) at snake2.Snake.<init>(Snake.java:10) at snake2.Snake.main(Snake.java:22) I actually just copied and pasted the code to see how it works. They are in the right packages too; but when I try to run it, I always end up with this error. Bozho The image should be in the same package

save resized image java

和自甴很熟 提交于 2019-12-04 22:56:47
问题 How do i save a resized image to a specific folder? private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { ImgChooser ic = new ImgChooser(); ImageIcon icon = new ImageIcon(me,"id pic"); Image img1 = icon.getImage(); Image img2 = img1.getScaledInstance(105, 105, 0); icon.setImage(img2); jLabel1.setIcon(icon); } This first code is where i get the image and resize it. Then i want the resized image to be saved in another folder. Thanks in advance 回答1: Use ImageIO.write(...) as