In my Main
I want to create a JFrame
, then I want to create a BackgroundPanel
and I want to add this one in the JFrame
.
This is Main class:
public class Main {
public static void main(String args[]) {
Frame frame = new Frame();
BackgroundPanel back = new BackgroundPanel();
frame.getContentPane().add(back);
frame.setSize(400, 287);
frame.setVisible(true);
}
}
This is BackgroundPanel class:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BackgroundPanel extends JPanel {
private Image img;
public BackgroundPanel() {
img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("sfondo.png"));
loadImage(img);
}
private void loadImage(Image img) {
try {
MediaTracker track = new MediaTracker(this);
track.addImage(img, 0);
track.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setOpaque(false);
g.drawImage(img, 0, 0, null);
}
}
And the JFrame
is a normal JFrame
class.
When I execute it, there are no errors, simply it put out a normal JFrame
without background.
Help me Please!
@nIcEcOw I used the code in the first answear to print my image on a JPanel. But when I execute it, there's an error in output.
this the error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at paintingexample.CustomPanel.<init>(PaintingExample.java:82)
at paintingexample.PaintingExample.displayGUI(PaintingExample.java:28)
at paintingexample.PaintingExample.access$000(PaintingExample.java:19)
at paintingexample.PaintingExample$1.run(PaintingExample.java:42)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 1 second)
I'm confused about project's structure. I noticed that i can't put the package folder in bin directory; this creates a conflict in my ide. I dont understand how i can put the java files in src and the package folder in bin directory. My java files are in package folder..how I can do this?
I'm using NetBeans IDE 8.0
I red NetBean's image importing tutorial, and there also tips me to create another package to import images within. Now I the code is:
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class PaintingExample {
private ImagePanel imagePanel;
public void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
imagePanel = new ImagePanel();
contentPane.add(imagePanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new PaintingExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class ImagePanel extends JPanel {
private ImageIcon imageIcon;
public ImagePanel() {
imageIcon = new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp/newpackage/Schema elettrico divella rev 2014-Model.jpg"));
}
@Override
public Dimension getPreferredSize() {
return (imageIcon == null ? new Dimension(100, 100): new Dimension(
imageIcon.getIconWidth(), imageIcon.getIconHeight()));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imageIcon.getImage(), 0, 0, this);
}
}
I changed :
imageIcon = new ImageIcon(ImageIO.read(ImagePanel.class.getResource(
"/images/aeroplaneright.jpeg")));
in
imageIcon = new javax.swing.ImageIcon(getClass().getResource("/org/me/myimageapp/newpackage/aeroplaneright.jpg"));
@nIcEcOw you're my hero! :D Now all works fine. But just another thing:
now I'm using your ImagePanel class in a bigger project. I'm also using part of SwingTest code in the Main class of project to create a frame of a customized jframe class (FramePrincipale) with ImagePanel background. When I execute I'm obtaining a frame with my bakcground, but there aren't the other swing elements (labels,textfields..) that are part of my customized jframe. How can I fix this?
this is Principale class (main project class):
import java.awt.EventQueue;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
public class Principale {
private ImagePanel imagePanel;
private static FramePrincipale frame = new FramePrincipale();
private void displayGUI() throws IOException {
JPanel contentPane = new JPanel();
imagePanel = new ImagePanel();
contentPane.add(imagePanel);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
new Principale().displayGUI();
} catch (IOException ex) {
Logger.getLogger(Principale.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
EventQueue.invokeLater(runnable);
while (true) {
frame.scriviLabel();
}
}
}
FramePrincipale is a JFrame Form that is in the same project.
@nIcEcOw: Now it works! Thank You for all man! :-)