2014-03-14 32 views
-1

因此,我在JPanel中繪製了一個圖像,該圖像本身被添加到JFrame中。但是,當圖像第一次繪製時,它看起來很小。我不確定這是面板問題還是圖像問題。如下圖所示: ![輸入圖片說明] [1]Java:在JPanel中看起來很小的圖像

我在圖像周圍繪製了一個矩形。

現在JPanel應該被包含在JFrame中。如上所示,JFrame不應該被着色。 JPanel的大小約爲JFrame的四分之一,圖像應該佔據幾乎所有的JPanel。

請問您是否問題或面板是圖片。對不起,如果它顯而易見。

等待SSCCE

回答

5

我不知道你是基於您發佈的代碼的幾個隨機行做什麼。代碼中沒有任何地方是你實際創建/讀取圖像。

據我所知,Mandelbrot Set實際上是通過繪製代碼來完成的。如果是這樣,問題可能是您沒有覆蓋您繪製面板的getPreferredSize()(不使用setSize()方法)以返回您正在繪製的圖像的大小。有關更多信息,請閱讀Swing教程中關於Custom Paining的部分。

或者,如果您實際上使用的是現有圖像,請參閱How to Use Icons的Swing教程中有關使用圖像的示例。

另外,在使框架可見之前,應將組件添加到框架中。

如果您需要更多幫助,請發佈適當的SSCCE來說明問題。

+0

很抱歉,如果它是模糊的,我現在已經列入其繪製圖像的代碼。 – user3352349

+0

@ user3352349'我現在已經包含了繪製圖像的代碼 - - 沒有幫助。仍然沒有幫助。我們仍然不知道如何調用draw()方法。看起來您正在繪製BufferedImage,但我們不知道實際將BufferedImage繪製到面板的位置,或者您不使用JLabel顯示BufferedImage的原因。你添加了調試代碼來顯示尺寸變量的尺寸大小嗎?同樣,如果您需要更多幫助,請選擇適當的SSCCE – camickr

-1

如何添加一個標籤面板內,並繪製使用setIcon(..)

我通常使用下面的類,以使圖像適合標籤尺寸圖像(我把我的標籤具有靜態大小 - 不調整大小) ..你可能要修改它以滿足您的需要..

import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 
import java.awt.image.ImageObserver; 
import java.net.URL; 
import javax.swing.ImageIcon; 

public class CustomImageIcon extends ImageIcon { 

    private BufferedImage dest; 

    public CustomImageIcon(String filename) { 
     super(filename); 
    } 

    public CustomImageIcon(Image image) { 
     super(image); 
    } 

    public CustomImageIcon(URL location) { 
     super(location); 
    } 

    @Override 
    public synchronized void paintIcon(Component c, Graphics g, int x, int y) { 

     if(c!=null) 
      dest = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB); 
     else dest = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_ARGB); 

     ImageObserver imgObs = getImageObserver(); 
     if(imgObs==null) imgObs = c; 

     int width; 
     int height; 
     if(c!=null) 
     { 
      width = c.getWidth(); 
      height = c.getHeight(); 
     } 
     else 
     { 
      width = getIconWidth(); 
      height = getIconHeight(); 
     } 


     g.drawImage(dest, 0, 0, c); 
      g.drawImage(
        getImage(), 
        0, 
        0, 
        width, 
        height, 
        imgObs); 
    } 
} 
+0

-1,爲什麼要通過擴展圖標來執行自定義繪畫代碼。使用Icon的關鍵是使用ImageIcon類,並將Icon添加到標籤,因此您不需要進行自定義繪畫。 – camickr

+0

正如我之前所說,我需要使圖像自動適合標籤,而不管圖像的大小。有時我有一個問題,圖像不適合在標籤.. 抱歉,如果這是錯誤的你.. =) –

1
BufferedImage image = ImageIO.read(file); //Read image through BufferedReader 

labelimage.setIcon(new ImageIcon(image.getScaledInstance(labelimage.getWidth(), labelimage.getHeight(), image.SCALE_SMOOTH))); // This line will automaticallically set Image size equal to size of Jlabel 
相關問題