2014-02-23 18 views
1

我使用NetBeans,我想在jPanel上显示图像(基本上是为了让它滚动)。 我写了这个代码Java - 无法从文件中绘制图像

Graphics g=jPanelScrolling.getGraphics(); 
    File fileBackground = new File("background.jpg"); 
    Image background; 
    try{ 
     background=ImageIO.read(fileBackground); 
     final int WIDTH=background.getWidth(rootPane);    
     final int HEIGHT=background.getHeight(rootPane); 
     g.drawImage(background, WIDTH, HEIGHT, rootPane); 
    } 
    catch(IOException e){ 
     background=null; 
     jPanelScrolling.setBackground(Color.red); //to test if the image has been succesfully uploaded 
    } 

但是当我执行它,它显示我只是虚空的JPanel

我怎样才能使它发挥作用?

+2

请当然,图像本身是可以访问的。请看看这个[回答](http://stackoverflow.com/a/9866659/1057230),了解如何将图像添加到Java项目中。希望它有助于:-)另外,如果您重写任何'JComponent'的'paintComponent'方法,请不要自己创建'Graphics'对象,而应使用'Swing'本身提供的对象。请确保你重写'getCompatibleSize()''JComponent'的paintComponent'被覆盖,因为一些Layout Manager倾向于给出0和0的大小,而不是:( –

回答

1

尝试,

import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class ImageInFrame { 
    public static void main(String[] args) throws IOException { 
     String path = "Image1.jpg"; 
     File file = new File(path); 
     BufferedImage image = ImageIO.read(file); 
     JLabel label = new JLabel(new ImageIcon(image)); 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(label); 
     f.pack(); 
     f.setLocation(200,200); 
     f.setVisible(true); 
    } 
} 
+0

感谢您的回答,但我想插入一个图像插入jPanel,而不是标签 –

+0

@ user3343715将标签添加到JPanel中。 –

1

要显示的图像,尝试这样的事情:

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class ImagePanel extends JPanel{ 
    private BufferedImage bi; 

    public ImagePanel() { 

     try { 
      bi = ImageIO.read(new File("Your Image Path")); 
     } catch (IOException ex) { 
      Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex); 
     } 


     final JPanel panel = new JPanel(){ 
      @Override 
      protected void paintComponent(Graphics g){ 
       Graphics g2 = g.create(); 
       g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null); 
       g2.dispose(); 
      } 

      @Override 
      public Dimension getPreferredSize(){ 
       return new Dimension(bi.getWidth()/2, bi.getHeight()/2); 
       //return new Dimension(200, 200); 
      } 
     }; 

     add(panel); 
    } 

     public static void main(String args[]){ 
      SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       ImagePanel imgPanel=new ImagePanel(); 
       JOptionPane.showMessageDialog(
         null, imgPanel, "Image Panel", JOptionPane.PLAIN_MESSAGE); 
      } 
     }); 
     } 
} 

输出

Image