How to implement .png pics in a JFrame? - java

Hi I want to make a window, a GUI, and put an image in it.
I watched a YT tutorial (https://www.youtube.com/watch?v=Ap20Qw77TzY) and copied everything similar but the Window I make has no image at all. I tried different file types like .jpg and different window sizes, matching the picture size but it doesn't help.
That's my code, I get no real errors, except a warning of:
The serializable class main does not declare a static final serialVersionUID field of type long,line 8
This method has a constructor,line 25
Code
package main;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.*;
public class main extends JFrame {
/**
* author jan
*/
public main(String title){
super (title);
}
public void paint(Graphics gr) {
super.paint(gr);
gr.drawImage(Toolkit.getDefaultToolkit().getImage("Koppenhagen\\Pictures\\Herz.png"), 0, 0, this);
}
public static void main(String[] args) {
main window = new main("Mein Test!");
window.setSize(160,160);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}

Use ImageIO.read over Toolkit.getImage, it will throw a IOException of the image can't be load for some reason
Check the location of the image. Your example is looking for a file in Koppenhagen\\Pictures, relative to the execution context of the program. You could use File#exists to check if the file actually exists where you think it is
Don't load resources within the any paint method, loading images can take time and painting should run as fast as possible
I'd discourage you from overriding paint of top level containers like JFrame. A JFrame contains a JRootPane, which contains, amongst other things, a contentPane all of which can be painted independently of its parent container. Instead, start with a JPanel and override its paintComponent method instead, then add this to an instance of JFrame

Here's a simple Swing application that draws an image.
You have to put the image in the same directory as the Java code.
package com.ggl.testing;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class DrawImage implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(new ImagePanel(getImage()));
frame.add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private Image getImage() {
try {
return ImageIO.read(getClass().getResourceAsStream(
"StockMarket.png"));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawImage());
}
public class ImagePanel extends JPanel {
private static final long serialVersionUID = -2668799915861031723L;
private Image image;
public ImagePanel(Image image) {
this.image = image;
this.setPreferredSize(new Dimension(image.getWidth(null), image
.getHeight(null)));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
}

Related

rendering an Image to a JPanel

I am currently trying to render an Image to a JPanel. Here is my Code:
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class ScreenSaver extends JPanel {
private static final long serialVersionUID = 001;
public static void main(String[] args) {
new ScreenSaver();
}
public ScreenSaver() {
new Window(1600, 900, "ScreenSaver", this);
}
public Image ball;
public void initCode() {
try {
File pathToBall = new File("ball.png");
ball = ImageIO.read(pathToBall);
} catch (IOException ex) {
ex.printStackTrace();
}
renderImage()
}
public void renderImage(Graphics g) {
g.drawImage(ball, 0, 0, 100, 100, null);
}
}
The "initCode()" method gets called after the JFrame has loaded. My problem now is that I want to call the "renderImage()" method. In the parameters I have to put "Graphics g" to use the "g.drawImage" function. Sadly I now dont know what to put in the brackets when I want to call "renderImage()". Can someone help?
The painting of pictures and graphic elements works a little differently than in your question.
Here is a working example:
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class PaintExample extends JFrame{
private BufferedImage ball;
public static void main(String[] args) {
new PaintExample();
}
public PaintExample(){
initCode();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1600, 900));
setMinimumSize(new Dimension(800, 600));
add(new JPanel(){
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(ball, 0, 0, 100, 100, this);
}
});
pack();
setLocationRelativeTo(null);
setTitle("Paint Example");
setVisible(true);
}
public void initCode() {
try {
File pathToBall = new File("ball.png");
ball = ImageIO.read(pathToBall);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I hope this solves your problem.
Swing is event driven, this means that you never directly call painting methods.
Basics for a custom painting component:
extends JComponent
override paintComponent(Graphics g), this is the point where you can put your rendering code
override getPreferredSize(), so that the layout managers can do their duty, a suitable value is your image size
In addition I don't understand this line:
new Window(1600, 900, "ScreenSaver", this);

JPanel Won't load images?

I'm trying to create an Image output from local folder on a JPanel(inside the JFrame) using these code, but the JFrame shows nothing:
package photo;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class UI extends javax.swing.JFrame {
public UI() {
initComponents();
}
public class ImagePanel extends javax.swing.JPanel{
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(new File("D://Programming/Image Processing/Colour Temperature/Vampire-full.jpg"));
} catch (IOException ex) {
// handle exception...
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel Image;
// End of variables declaration
}
I don't know whether I used the right semantic, please help.
Thank you.
As #MadProgrammer said in the comments, you should add your panel to the frame. You would also need to set up the size of your JFrame if you want to see something when you launch your application.
For example change your UI constructor to something like:
public UI() {
// set up the size of your frame
this.setSize(800, 600);
// set up a layout manager
this.setLayout(new BorderLayout());
initComponents();
}
You can use whatever values you want for the width and height of your frame. You should also set up the type of Layout manager you want to use (an instance of BorderLayout for example)
Then use your initComponents()method to add the panel
private void initComponents() {
this.add(new ImagePanel());
}
With these changes your frame will display the image loaded in your ImagePanel

ImageIcon.paintIcon does not draw image on JPanel

I'm trying to draw a .png on a JPanel. I imported it using an ImageIcon constructor, and drew it in my custom panel's paintComponent.
My sscce:
package mypackage;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyPanel extends JPanel {
static JFrame frame;
static MyPanel panel;
static ImageIcon icon;
public static void main(String[] args) {
icon = new ImageIcon(MyPanel.class.getResource("MyImage.png"));
frame = new JFrame();
panel = new MyPanel();
frame.setSize(500, 500);
frame.add(panel);
frame.setVisible(true);
frame.repaint();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
icon.paintIcon(panel, g, 100, 100);
}
}
I expected the image, which is just a couple shapes on a white background, to display at (100, 100) on the panel. Instead, a blank screen:
The fact that no error occurs means the program is finding the file properly.
The image is in my Eclipse project in the same package as the class:
Why is this happening? How do I fix it?
As the code looks correct, I suggest that the resource is not being loaded correctly.
Place the png file in your classpath. Eg. I would have a directory:
~/ProjectRoot/resources/mypackage/
Then include resources in the classpath. In eclipse you can setup the classpath via
Project -> Properties -> Java Build Path -> Add Class Folder
BufferedImage img =
ImageIO.read(MyPanel.class.getResourceAsStream("MyImage.png"));
That throws an exception if the image isn't found. You can use it to make an ImageIcon.
When you use ImageIcon to read an image from a file, you get no indication whether or not the read was successful.
Here's your GUI where I read an image using ImageIO:
And here's the image, in the same directory as the Java source:
Here's your code, where I used an ImageIO read to read an Image, and draw the image in the paintComponent method.
package com.ggl.testing;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyPanel extends JPanel {
private static final long serialVersionUID = -9008812738915944216L;
private static JFrame frame;
private static MyPanel panel;
private static Image image;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
image = getImage();
frame = new JFrame();
panel = new MyPanel();
frame.setSize(500, 500);
frame.add(panel);
frame.setVisible(true);
}
});
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 100, 100, MyPanel.this);
}
private static Image getImage() {
Image image = null;
try {
image = ImageIO.read(MyPanel.class.getResource("maze.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
}

Displaying an image in Java using Swing

I'm trying to display an image in a window using Swing.
For some reason, when I run the program, the dialog box that displays contains nothing. Is there a clear reason why this is happening?
public class GameScreen {
public static void main(String[] args) {
GameView view = new GameView();
view.setVisible(true);
}
}
public class GameView extends JFrame {
public MapView mapPanel;
public void GameView() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mapPanel = new MapView();
this.add(mapPanel);
this.pack();
this.setLocationByPlatform(true);
}
}
public class MapView extends JPanel {
public MapView() {
ImageIcon map = new ImageIcon("map.jpeg");
JLabel mapLabel = new JLabel(map);
this.add(mapLabel,BorderLayout.CENTER);
}
}
On a side note, I've heard that using ../../ in file path names isn't recommended, however in most application packages the 'resources folder' is located in the parent directory of the executable files, so what is the main way people get around this?
Here's one way to draw an image on a JPanel using Swing.
In order for this code to work, you have to put the image in the same directory as the Java code.
If you want to put the image in a different directory, you have to make that directory part of the Java classpath, and add a slash to the front of the file name.
package com.ggl.testing;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class DrawImage implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(new ImagePanel(getImage()));
frame.add(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private Image getImage() {
try {
return ImageIO.read(getClass().getResourceAsStream(
"StockMarket.png"));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawImage());
}
public class ImagePanel extends JPanel {
private static final long serialVersionUID = -2668799915861031723L;
private Image image;
public ImagePanel(Image image) {
this.image = image;
this.setPreferredSize(new Dimension(image.getWidth(null), image
.getHeight(null)));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
}

JAVA Showing Image -GUI

I'm trying to make a simple GUI using JAVA
no image is shown
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
public class EmiloLadderSnack {
public JFrame frame=new JFrame("EmiloLadderSnack");
public Image img;
public Graphics g;
public EmiloLadderSnack()
{
frame.setBounds(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try
{
img= ImageIO.read(new File("/media/01CCE00FA6888D80/Achieve/Eclipse/EmiloLadderSnack/src/photo.jpg"));
g.drawImage(img, 50, 50, null);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String args[])
{
new EmiloLadderSnack();
}
}
please help me to show an image in my simple GUI using JAVA
I'm using Eclipse
Hovercraft Full Of Eels is right, as he/she usually is. It really did not look like you tried.
Look at the tutorials, but I do believe when Hovercraft Full Of Eels says the correct way, hover means as follows.
Let me explain what I did below. First I created a new class that extended the JFrame. The JFrame is what is suppose to hold all of the components in a window. Then draw on the JPanel so that all of your drawings are contained in a lightweight container. I set the layout with a new layout I just discovered due to StackOverflow which I am very thankful for. The layout is called the MigLayout and it is a third party resource. You have to download it and import it. Please note that you do not have to have the MigLayout, but it is preferable to use due to its ease of use. After I set the Layout Constraint to fill and docked the JPanel in the center I created a new class which extended the JPanel so that I could change the paint method. The #Override lets you, in a way, re create the method for that extended class. As you can see once draw to that one graphics class then you are all set. There is a lot more you should read up on. Read the comments below your post, they suggest fairly good material.
Anything I get wrong Hovercraft will say below in the comments. So look for that as well.
Hovers corrections:
import java.awt.Dimension;
import java.awt.Graphics;
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;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GraphicExample extends JPanel {
private static final String IMG_FILE_PATH = "/media/01CCE00FA6888D80/" +
"Achieve/Eclipse/EmiloLadderSnack/src/photo.jpg";
private BufferedImage img;
public GraphicExample(BufferedImage img) {
this.img = img;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, this);
}
}
#Override
public Dimension getPreferredSize() {
if (img != null) {
return new Dimension(img.getWidth(), img.getHeight());
}
return super.getPreferredSize();
}
private static void createAndShowGui() {
try {
BufferedImage img = ImageIO.read(new File(IMG_FILE_PATH));
JFrame frame = new JFrame("GraphicExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GraphicExample(img));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// the easy way to display an image -- in a JLabel:
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon);
JOptionPane.showMessageDialog(frame, label);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
My initial recommendations:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class DrawCircle extends JFrame {
JPanel panel;
public DrawCircle(String title, int width, int height) {
this.setTitle(title);
this.setSize(width, height);
this.setLocationRelativeTo(null); // Center JFrame
this.setLayout(new MigLayout("fill")); // Download external jar
this.panel = new DrawOval();
this.add(panel, "dock center"); // Link: http://www.miglayout.com/
this.setVisible(true);
}
public class DrawOval extends JPanel {
Color color = new Color(1, 1, 1);
public DrawOval() {
}
#Override
public void paint(Graphics g) {
g.setColor(color.RED);
g.fillOval(0, 0, this.getWidth(), this.getHeight());
}
}
}
I can't imagine that this is compiling. There must be a NullPointerException.
When you want to draw something you usually subclass JPanel and do the drawing in the paintComponent() method, like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 50, 50, null);
}

Categories

Resources