简体   繁体   中英

java drawImage doens't work

I'm trying to show an image with a java panel but it's a no go. The code doesn't give any exceptions and/or errors and yet the image doesn't load. The image does exist and I've tried .jpg also but that doesn't work as well.

package feupcraftproject;
import javax.swing.*;

public class frame_jogo extends JFrame
{
    public panel_jogo panel;
    public frame_jogo() 
    {
        panel = new panel_jogo();
        //setLayout(new GridLayout(1,1,0,0));
        add(panel);   

        }
}

package feupcraftproject;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.applet.Applet;


public class panel_jogo extends Applet
{
    public BufferedImage  back;
    public panel_jogo()
    {
        try 
        {
            back = ImageIO.read(new File("src/Recursos/mapa_piso1.png"));         
        }
        catch (IOException A)
        {
            JOptionPane.showMessageDialog(null, A.toString());
        }   
       //this.setBackground(Color.BLACK);
    }
    //@Override
    public void paintComponent(Graphics g)
    {
        //super.paintComponents(g);
        g.drawImage(back, 611, 468, this);

    }
}

This is a bit inexplicable:

 public class panel_jogo extends Applet // ?? Applet ??

Does Applet have a paintComponent(...) method? I didn't think so.

Edit -- it doesn't and I see that you commented the @Override -- too bad, because it would have told you that no override exists, and so your paintComponent(...) method is fated to do nothing useful.


Recommendations:

  • Have your drawing panel class extend JPanel instead.
  • Don't comment out your @Override annotation. It's needed for a reason.
  • Make sure that your paintComponent(...) method does in fact call the super method, something you've commented out.
  • Do follow Java naming conventions including having class names begin with an upper case letter.
  • Another possible issue is that you appear to be drawing the image 611 pixels right and 468 pixels down. Consider having your drawing program override getPreferredSize() so that it is in fact large enough to show the entire image at its desired location.
  • Either that, or perhaps better, put the image into an ImageIcon, place that into a JLabel, and display use layout managers to place the JLabel in the lower right hand corner of your JPanel.

It looks like you "borrowed" code that in fact followed my recommendations above but for some unknown reason changed it to force it to misbehave, especially by having it extend Applet. Can you explain your reasoning behind this?

public class panel_jogo extends Applet
{
    public BufferedImage  back;
    public panel_jogo()
    {
        try 
        {
            back = ImageIO.read(new File("src/Recursos/mapa_piso1.png"));  

The Applet and File classes do not 'play well together'.

  1. An applet must be digitally signed by you, and trusted by the user (when prompted) in order to access a File .
  2. Even when trusted, the applet will not be able to load images from the server , any File objects created can only relate to the client machine, and the mapa_piso1.png is not (I presume) going to be found on the hard disks of the user's PC!
  3. An Applet can load images from the server it came from, by URL .

  1. Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets .
  2. Why AWT rather than Swing? See this answer on Swing extras over AWT for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see Mixing Heavyweight and Lightweight Components .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM