Some days ago, I wasted a lot of time searching some way to show a image in a JFrame. And here is my final solution:
jPanel1 = new javax.swing.JPanel(){
#Override
public void paintComponent(Graphics g) {
BufferedImage image = null;
try {
BufferedImage in = ImageIO.read(Startup.class.getResource("imagem.jpg"));
image = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
g.drawImage(in, 0, 0, null);
} catch (Exception ex) {}
super.paintComponents(g);
}
};
I just want to know, if it is the one way to do that, or exists another solutions like a image component on Swing o AWT that can be easily used?
You can show an image using JLabel, which is much simpler than your solution. For example:
label.setIcon(new ImageIcon("Path/to/your/image.jpg"));
Related
i created a custom Java Button like this
public class GraphicPaintedButton extends JButton implements ToPaint{
protected BufferedImage background;
private boolean painted = false;
public GraphicPaintedButton() {
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(painted){
System.out.println("PAINTING!");
Dimension size = getSize();
g.drawImage(background, 0, 0,size.width, size.height,0, 0, background.getWidth(), background.getHeight(), null);
}
}
#Override
public void loadImage(String url){
painted = true;
URL imagePath = getClass().getResource(url);
BufferedImage result = null;
try {
result = ImageIO.read(imagePath);
} catch (IOException | IllegalArgumentException e) {
System.err.println("Errore, immagine non trovata");
e.printStackTrace();
}
background = result;
}
}
Iif i load an image on the button, it calls the repaint,and it's good, the image is shown, when i load a new Image, it calls the repaint again and the new Image is shown. The problem is when i pass the mouse over the button, it calls the rapaint, but loads the old image. Why? And how did it get the old image, as the new one should have replaced it?
I don't know why you want to go this extend and not simply use the icon property of the button, but...
First, I'd get rid of the painted flag, it runs the risk of allowing the paintComponent method to try and paint a null image, because the loadImage method may fail, but the painted flag is always set to true - Let's face it, you could easily end up with painted == true and background == null ... which is not a valid state
Instead, reason about the actual state you're interested in...
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Dimension size = getSize();
g.drawImage(background, 0, 0, size.width, size.height, 0, 0, background.getWidth(), background.getHeight(), null);
}
}
I would also change the loadImage method
public void loadImage(URL url) throws IOException {
background = ImageIO.read(url);
revalidate();
repaint();
}
First, don't use String, it's meaning is to ambiguous, instead, specify the lowest common parameter you're willing to deal with.
Secondly, the image is either going to load or it's not, if you're not going to deal with the exception, in any meaningful fashion, pass it back to the caller who may be in a better position to deal with it - IMHO
I don't know if this important or not, but I'd also consider overriding the getPreferredSize method...
#Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(10, 10) : new Dimension(background.getWidth(), background.getHeight());
}
This will at least try and match the button's size to the size of the image
I am facing an issue that I am not able to draw an png image into my swing GUI into JPanel (which I am adding into JScrollPane after).
Here is my code I am trying to use for this.
if(processCreated == true){
System.out.println("updating tree of organisms");
processCreated = false;
updateTree();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Svet.class.getName()).log(Level.SEVERE, null, ex);
}
tree = new File(path+"/out.png"); //File
try {
treeImage = ImageIO.read(tree); //BufferedImage
tp = new TreePane(treeImage.getScaledInstance( 500, 500 ,treeImage.SCALE_SMOOTH)); // my class, implementation below
treeOutput.add(tp); //adding "JPanel" with picture into GUI
treeOutput.repaint();
} catch (IOException ex) {
Logger.getLogger(Svet.class.getName()).log(Level.SEVERE, null, ex);
}
}
After this the JScrollPane remains empty. Is there anything wrong with my code? You can find what is what in comments.
Here is my TreePane class
public class TreePane extends JPanel{
Image img;
public TreePane( Image img ){
this.img = img;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int imgX = 0;
int imgY = 0;
g.drawImage(img, imgX, imgY, this);
}
}
Very likely your TreePane is layed out to size 0,0 and therefore you'll never see anything. Try adding a breakpoint and inspecting in paintComponent - it might not even ever get called.
To fix, you should explicitly set the size of your TreePane before adding it to the JScrollPane.
tp = new TreePane(treeImage.getScaledInstance( 500, 500 ,treeImage.SCALE_SMOOTH)); // my class, implementation below
tp.setSize(myWidth, myHeight);
treeOutput.add(tp); //adding "JPanel" with picture into GUI
treeOutput.repaint();
I need some help with painting an Image object upon/inside/on a BufferedImage and then painting that BufferedImage on a JPanel.
I've prepared a small program to illustrate my problem. Just a frame with a panel accompanied by an ImageLoader.
The image is placed in the same folder as the code. The sten Image is painted successfully when just painted, but not when I try painting it with the BufferedImage, which you'll notice if you try to run the program. just create the Test object and the constructor does the rest.
Thanks in advance!
My code:
public class Test extends JFrame{
static class ImageLoader {
public static Image loadImage(String name){
Image img = null;
img = Toolkit.getDefaultToolkit().getImage(ImageLoader.class.getResource(name));
return img;
}
}
class Panel extends JPanel{
Image sten;
BufferedImage bf = new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);
public Panel(Image sten){
super();
this.sten = sten;
initBF();
}
private void initBF(){
Graphics2D g = (Graphics2D) bf.createGraphics();
g.drawImage(sten, 0,0,this);
}
public void paintComponent (Graphics g)
{
g.drawImage(bf, 100,100,null);
g.drawImage(sten, 0,0,null);
repaint();
}
}
public Test(){
setSize(new Dimension(500, 500));
setEnabled(true);
this.setBounds(50, 150, 500, 500);
setVisible(true);
Image sten = ImageLoader.loadImage("sten.png");;
Panel panel = new Panel(sten);
panel.setBackground(Color.GREEN);
panel.setSize(500, 500);
this.add(panel);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.paintComponent(this.getGraphics());
}
}
The problem is that Toolkit.getDefaultToolkit().getImage loads images asychronously so will not be loaded when paintComponent is invoked. Use a MediaTracker to block until the image has been loaded:
public Image loadImage(String name) {
Image img = null;
MediaTracker tracker = new MediaTracker(myPanel); // pass the panel from ctor
img = Toolkit.getDefaultToolkit().getImage(ImageLoader.class.getResource(name));
tracker.addImage(img, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
return img;
}
or far simpler:
img = ImageIO.read(ImageLoader.class.getResource(name)));
This will eliminate the need to use MediaTracker.
Some notes:
Don't call paintComponent directly, request repaints by invoking repaint.
Don't use getGraphics for custom painting - this uses a transient Graphics reference.
When using a custom Graphics reference, make sure to call Graphics#dispose when finished using the reference.
In addition of Reimeus' aswer, notice that Toolkit.getDefaultToolkit().getImage() is non-blocking, it loads images asynchronously, so at the point when you call g.drawImage(sten, 0,0,this); sten will probably not still actually be loaded (see the doc of the method). See also this (false) bug report.
I am using Jtree for listing various images of a directory, I want to display image on applet when the user click on the image name displayed in the Tree, the code i'm using is as below, ta is an object of the applet because i'm using it in another class.
private void displayImage(URL furl, String fname) {
ta.Picture = ta.getImage(furl, fname);
prepareImage(ta.Picture, this);
Graphics g = ta.imageCanvas.getGraphics();
g.clearRect(10, 10, 800, 800);
g.drawImage(ta.Picture, 10, 10, this);
} // displayImage
public void valueChanged(TreeSelectionEvent e)
{
// TODO Auto-generated method stub
FileTreeNode node = (FileTreeNode) tree.getLastSelectedPathComponent();
System.out.println("slecte asldf " + node.isLeaf());
if (node.isLeaf())
{
currentFile = node.file;
System.out.println("File name " + currentFile.getName());
try
{
URL furl = new URL("file:/F:/photos");
displayImage(furl, currentFile.getName());
}
catch (MalformedURLException mle)
{
System.out.println("Exception::::::" + mle);
}
}
else
currentFile = null;
}
But its not working.
As you are showing files from the local filesystem, working with URLs is not required. Use
displayImage(currentFile);
and rewrite that method as following:
private void displayImage(File file) {
BufferedImage image = ImageIO.read(file);
ta.image = image;
ta.repaint();
}
where the paint method of the (I an assuming) component ta must be like
BufferedImage image;
public void paint(Graphics g) {
g.clearRect(10, 10, 800, 800);
g.drawImage(ta.Picture, 10, 10, this);
}
Because of security reasons, the applet will only be able to access the file system if signed or running without security manager (most often on the same computer).
But its not working.
This is in no way helpful, do you get exceptions? What happens? Please post an SSCCE for better help sooner
I want to display image on applet when the user click on the image
name displayed in the Tree, the code i'm using is as below, ta is an
object of the applet because i'm using it in another class.
IMO you are going about it wrong using the JPanel object and Component#getGraphics.
Dont use Component#getGraphics() as its not good practice and not persistent thus on next call to repaint() the screen will be cleared.
Dont use Applet with Swing components rather use JApplet.
Add a custom JPanel with getters and setters for BufferedImage variable to the container and than override paintComponnet and draw the BufferedImage there.
Now to change the BufferedImage simply call the setter i.e setBackgroundImage(BufferedImage img) and than call repaint() on JPanel to show the changes. Like so:
public class MyPanel extends JPanel {
private BufferedImage bg;
public MyPanel(BufferedImage bi) {
bg=bi;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
g2d.drawImage(bg,0,0,this);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(bg.getWidth(),bg.getHeight());
}
public BufferedImage setBackgroundImage(BufferedImage bi) {
bg=bi;
}
}
Now we use it like so:
MyPanel mp=new MyPanel(...);//create the panel with an image
...
add(mp);//add to container
...
mp.setBackgroundImage(..);//change the image being displayed
mp.repaint();//so the new image may be painted
How do I make an Image show up in a Java Swing Applet?
The simplest way is to add it as an Icon to a JLabel that you put on your GUI.
http://download.oracle.com/javase/6/docs/api/javax/swing/JLabel.html#JLabel(javax.swing.Icon)
Here's an outline of what you need
class myApplet extends JApplet {
private BufferedImage image;
myApplet() {
try {
image = ImageIO.read(new File("insert image path here"));
} catch (IOException ex) {
// handle exception...
}
}
#Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}