2014-10-10 98 views
0

我想用另一個替換面板(它們都具有透明部分)。代碼如下:從JPanel刪除透明JPanel

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.GridLayout; 
import java.awt.Image; 
import java.util.Timer; 
import java.util.TimerTask; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class PhotoFrame extends JFrame { 

    public class PhotosDisplayPanel extends JPanel { 

     private static final long serialVersionUID = 1L; 
     private String[] imgsLink = null; 
     private long delay = 1000; 
     private long period = 1000; 
     private int curIdx = 0; 
     private PhotoDisplayPanel currentPhoto = null; 

     public PhotosDisplayPanel(String[] imgsLink) { 
      super(); 
      this.imgsLink = imgsLink; 
      setBackground(new Color(0, 0, 0, 0)); 
      setLayout(new GridLayout(1, 1)); 
      currentPhoto = new PhotoDisplayPanel(imgsLink[curIdx]); 
      add(currentPhoto); 
     } 

     public void start() { 
      if (imgsLink == null) { 
       return; 
      } 
      curIdx = -1; 
      Timer timer = new Timer(); 
      timer.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        curIdx++; 
        if (curIdx >= imgsLink.length) { 
         curIdx = 0; 
        } 
        displayNextImage(); 
       } 
      }, delay, period); 
     } 

     protected void displayNextImage() { 
      if (currentPhoto != null) { 
       remove(currentPhoto); 
      } 
      revalidate(); 
      repaint(); 

      currentPhoto = new PhotoDisplayPanel(imgsLink[curIdx]); 
      add(currentPhoto); 
      revalidate(); 
      repaint(); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
     } 

    } 

    public class PhotoDisplayPanel extends JPanel { 

     private static final long serialVersionUID = 1L; 
     private String imgLink; 

     public PhotoDisplayPanel(String imgLink) { 
      super(); 
      this.imgLink = imgLink; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      if (imgLink != null && !imgLink.equals("")) { 
       System.out.println("Draw image"); 
       // get dimension of the panel 
       int pWidth = getWidth(); 
       int pHeight = getHeight(); 

       g.setColor(new Color(255, 0, 0, 50)); 
       g.fillRect(0, 0, pWidth, pHeight); 

       Image img = new ImageIcon(imgLink).getImage(); 

       // Calculate positions and dimensions 
       int imgWidth = img.getWidth(this); 
       int imgHeight = img.getHeight(this); 
       int iwidth = 0; 
       int iheight = 0; 
       if (imgWidth/imgHeight > pWidth/pHeight) { 
        iwidth = pWidth; 
        iheight = imgHeight * pWidth/imgWidth; 
       } else { 
        iheight = pHeight; 
        iwidth = imgWidth * pHeight/imgHeight; 
       } 
       int ix = (pWidth - iwidth)/2; 
       int iy = (pHeight - iheight)/2; 

       // Fill the picture to the panel 
       g.drawImage(img, ix, iy, iwidth, iheight, this); 
      } else { 
       super.paintComponent(g); 
      } 

     } 

    } 

    private static final long serialVersionUID = 1L; 
    private static final int defaultWidth = 650; 
    private static final int defaultHeight = 400; 
    private int width = defaultWidth; 
    private int height = defaultHeight; 

    private PhotosDisplayPanel photoPanel; // is view panel which display 
    // input and output 
    private String[] imgsLink; 

    public PhotoFrame() { 
     initialize(); 
     imgsLink = new String[] { "background.png", "screenShot.jpg" }; 
     createControls(); 
    } 

    private void createControls() { 
     setLayout(new GridLayout(0, 1)); 
     photoPanel = new PhotosDisplayPanel(imgsLink); 
     add(photoPanel); 
     photoPanel.start(); 
    } 

    private void initialize() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(width, height); 
     setTitle("PhotosFrame"); 
     setAlwaysOnTop(false); 
     setBackground(Color.CYAN); 
     setLayout(new GridLayout(1, 1)); 
     setContentPane(new JLabel(new ImageIcon("blue_sunset.jpg"))); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new PhotoFrame().setVisible(true); 
      } 
     }); 
    } 
} 

其中,currentPhoto是一個透明背景的面板。然而,舊面板的圖像沒有完全清理,我可以看到新面板下的舊面板。有沒有辦法全面清除舊面板的圖像? 感謝

+1

爲了更好地幫助發佈一個[最小化,完整和可驗證示例](http://stackoverflow.com/help/mcve),我們可以複製粘貼編譯運行,這表明問題。 – 2014-10-10 17:55:31

+1

也許更換面板不是要走的路,而是改變_single_面板中的某個狀態。 – 2014-10-10 17:56:21

+0

我也嘗試過兩種方式,但舊圖像仍然在新版本 – KhoiUCD 2014-10-13 15:12:37

回答

1

不過,老面板的圖像沒有完全清洗乾淨,

的可能原因退房Backgrounds With Transparency和一對夫婦的解決方案。

基本上當使用透明背景Swing不知道是否需要重新繪製背景,因此您需要強制重新繪製。

+0

我試過了,但第一次成功了,之後舊面板的圖像依然出現在新面板下 – KhoiUCD 2014-10-13 13:43:27