2013-07-01 17 views
-1

我是使用Java和Netbeans的新人。在許多其他語言中,這是一件簡單的事情。但是,在打破了我的大腦思維後,我無法做到。我的疑問很容易解釋。 如何在運行時使用java2D在公共JFrame中顯示位圖(存儲在硬盤上)?我需要編輯和/或創建?這很簡單嗎?如何使用java2d在jFrame中顯示圖片?

在此先感謝...

+0

_but打破了我的大腦思維後...我can't_之前說你可以」你至少應該嘗試一下。 – BackSlash

+0

我真的嘗試過,先生。我不是一個好的搜索者,你可以看到......但是我所有的嘗試都是失敗的。 (並且原諒我的英文) – Guill

回答

1

的基本過程是使用Graphics#drawImage渲染之前已經加載的圖像。

有一些事情你爲了實現這一目標需要...

  • 的圖像畫
  • 表面畫上

幸運的是,這兩個都比較易於在Swing中實現

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
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.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ShowMyImage { 

    public static void main(String[] args) { 
     new ShowMyImage(); 
    } 

    public ShowMyImage() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       ImagePane pane = new ImagePane(); 
       try { 
        pane.setImg(ImageIO.read(new File("C:\\hold\\thumbnails\\_MTCGAC__Pulling_Cords_by_Dispozition.png"))); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(pane); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class ImagePane extends JPanel { 

     private BufferedImage img; 

     public ImagePane() { 
     } 

     public void setImg(BufferedImage value) { 
      if (img != value) { 
       img = value; 
       repaint(); 
      } 
     } 

     public BufferedImage getImg() { 
      return img; 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      BufferedImage img = getImg(); 
      return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      BufferedImage img = getImg(); 
      if (img != null) { 
       int x = (getWidth() - img.getWidth())/2; 
       int y = (getHeight()- img.getHeight())/2; 

       g2d.drawImage(img, x, y, this); 
      } 
      g2d.dispose(); 
     } 
    } 
} 
+0

嗯,先生。有了這個,我得到了!我只需要研究關於Exceptions和Graphics2D類的一點點。順便說一下,我感謝你。 – Guill

-1

您可以使用java中的基本AWT包執行此操作。您將需要擴展JFrame類並重寫其paintComponent方法以在其中顯示圖像。即

public class MyFrame extends JFrame { 
    private java.awt.Image bitmap = ImageIO.read(new File("myBitmap.bmp")); 

    @Override 
    public void paintComponent(java.awt.Graphics graphics) { 
     int x = 0; 
     int y = 0; 
     int w = bitmap.getWidth(null); 
     int h = bitmap.getHeight(null); 
     graphics.drawImage(bitmap, x, y, w, h, this); 
    } 
} 

你可以簡單地實例它,然後

MyFrame myFrame = new MyFrame(); 
myFrame.setPreferredSize(new Dimension(100, 100)); 
myFrame.setVisible(true); 

當然,這是一個基本的例子。

+0

-1,JFrame沒有paintComponent()方法。自定義繪畫是通過覆蓋JPanel(或JComponent)的paintComponent()方法完成的。然後將面板添加到框架。您通常還會在方法的開頭調用super.paintComponent()。 – camickr

+0

所以... <@override>對我來說是新的。它覆蓋父方法? – Guill

+1

請參見:[使用@Override Liberally](http://www.javapractices.com/topic/TopicAction.do?Id=223) – camickr

0

您需要像下面講述的那樣擴展JPanel類並覆蓋paintComponent方法。

public class DrawImageClass extends JPanel { 
    private static final long serialVersionUID = 1L; 
    private Image image; 
    public DrawImageClass(Image image) throws HeadlessException { 
     super(); 
     this.image = image; 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.drawImage(image, null, null); 
    } 
} 

然後創建JFrame並將其添加到它。

public class App { 
    private static final int WIDTH=480, HEIGHT = 640; 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setSize(new Dimension(HEIGHT, WIDTH)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     try { 
      DrawImageClass panel = new DrawImageClass(ImageIO.read(new File(App.class.getResource("/1.png").getPath()))); 
      panel.setPreferredSize(new Dimension(HEIGHT, WIDTH)); 
      frame.add(panel); 
      frame.setVisible(true); 
     } catch (Exception e) { 
      System.out.println("wrong path or smth other"); 
     } 
    } 
} 

示例圖片這是在資源文件夾中的項目

+0

用於擴展'JPanel'的+1;用於重寫'paintComponent'的+1; -1使用'setPreferredSize'; -1不調用超級。paintComponent'; -1不提供'ImageObserver'到'drawImage'方法; -1不使用'JFrame#pack'; -1缺少[初始線程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html); -1沒有打印異常(好吧,這是一個挑剔的) – MadProgrammer

+0

你瘋了:)這是簡明的代碼碎片瞭解如何。它工作 – lummycoder

+0

這是一個不好的例子,工作與否 – MadProgrammer