2017-05-22 66 views
0

你好,我試圖讓圖像透明,然後得到一個新的圖像,並將其設置爲jpanel背景。我知道,我知道你可能會說已經有答案了,但沒有,沒有任何作品,我不知道爲什麼,我真的很感謝一些幫助。圖像透明度和JPanel圖像背景

下面是代碼:

import java.awt.AlphaComposite; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.Time; 

import javax.swing.Timer; 

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

public class DvdInterface extends JPanel { 

    private String cathegory; 
    private JPanel hiddenPnl = new JPanel(); 
    private JPanel imagePnl; 
    private ImageIcon imageLoading; 
    private TextAnimation textMoving = new TextAnimation(); 
    private ImageIcon bgImg; 
    private Timer tm; 
    private int x = 0; 
    private int y = 240; 
    private DvdInterface dvd; 



    public DvdInterface(){ 

     components(); 

     setVisible(true); 


    } 



    private void components(){ 


     this.setLayout(null); 
     JLabel text = new JLabel("Movie selector"); 
     JLabel select = new JLabel("Please select a movie"); 

     JLabel empty2 = new JLabel(""); 
     JLabel empty3 = new JLabel(""); 
     JLabel empty4 = new JLabel(""); 

     imageLoading = new ImageIcon("D:/Java Eclipe Workspace/Dvd sorter/Loading.jpg"); 
     Image img = imageLoading.getImage(); 
     Image tempImg = img.getScaledInstance(200, 250, Image.SCALE_SMOOTH); 

     imageLoading = new ImageIcon(tempImg); 

     JLabel labelImg = new JLabel(imageLoading, JLabel.CENTER); 


     labelImg.setBounds(198, 202, 200, 250); 
     text.setBounds(235, 150, 183, 57); 

     paintSelect(select); 
     paintSelect(text); 

     add(select); 
     add(labelImg); 
     add(text); 



     tm = new Timer(100, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 

       select.setBounds(205, 200, 250, 270); 

       if(x >= 0){ 

        x = x + 10; 

       } 

       if(x == 250){ 

        x = x - 150; 


       } 

       select.setForeground(new Color(0, 255, 100, x)); 

      } 
     }); 

     tm.start(); 

    } 




    public void setCathegory(String cathegory) { 
     this.cathegory = cathegory; 
    } 

    public void paintSelect(JLabel select){ 


     select.setFont(new Font("Engravers MT", Font.BOLD, 10)); 


    } 

} 
+0

[有兩個例子,這將回答您的基本問題(http://stackoverflow.com/questions/22162398/how-to- set-a-background-picture-in-jpanel/22162430#22162430) - 然後你只需要添加你想要展示給那些容器的東西。我會警惕'空'佈局 – MadProgrammer

+0

請仔細閱讀答案,背景圖像的基本功能封裝在'JLabel'或'JPanel'中 - 然後顯示在'JFrame'內 - 因爲你無法顯示一個用戶界面 - 這是不可能的。但是,您可以創建一個[無邊界窗口](https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html),它可以讓您執行[this](http:// stackoverflow。 COM /問題/ 11703794 /如何對設置的JFrame背景透明 - 丁JPanel的或 - JLabel的背景不透明/ 11705029#11705029)或 – MadProgrammer

+0

[本](http://stackoverflow.com/questions/ 13557261 /爲什麼-的圖形 - 不出現功能於JFrame中/ 13557495#13557495) – MadProgrammer

回答

1

甲能夠畫Image爲背景

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.Transparency; 
import java.awt.image.BufferedImage; 
import javax.swing.JPanel; 

public class BackgroundPane extends JPanel { 

    private BufferedImage img; 
    private BufferedImage scaled; 

    public BackgroundPane() { 
    } 

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

    public void setBackground(BufferedImage value) { 
     if (value != img) { 
      this.img = value; 
      repaint(); 
     } 
    } 

    @Override 
    public void invalidate() { 
     super.invalidate(); 
     if (getWidth() > img.getWidth() || getHeight() > img.getHeight()) { 
      scaled = getScaledInstanceToFill(img, getSize()); 
     } else { 
      scaled = img; 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (scaled != null) { 
      int x = (getWidth() - scaled.getWidth())/2; 
      int y = (getHeight() - scaled.getHeight())/2; 
      g.drawImage(scaled, x, y, this); 
     } 
    } 

    public static BufferedImage getScaledInstanceToFill(BufferedImage img, Dimension size) { 

     double scaleFactor = getScaleFactorToFill(img, size); 

     return getScaledInstance(img, scaleFactor); 

    } 

    public static double getScaleFactorToFill(BufferedImage img, Dimension size) { 

     double dScale = 1; 

     if (img != null) { 

      int imageWidth = img.getWidth(); 
      int imageHeight = img.getHeight(); 

      double dScaleWidth = getScaleFactor(imageWidth, size.width); 
      double dScaleHeight = getScaleFactor(imageHeight, size.height); 

      dScale = Math.max(dScaleHeight, dScaleWidth); 

     } 

     return dScale; 

    } 

    public static double getScaleFactor(int iMasterSize, int iTargetSize) { 

     double dScale = (double) iTargetSize/(double) iMasterSize; 

     return dScale; 

    } 

    public static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) { 

     return getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true); 

    } 

    protected static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor, Object hint, boolean bHighQuality) { 

     BufferedImage imgScale = img; 

     int iImageWidth = (int) Math.round(img.getWidth() * dScaleFactor); 
     int iImageHeight = (int) Math.round(img.getHeight() * dScaleFactor); 

     if (dScaleFactor <= 1.0d) { 

      imgScale = getScaledDownInstance(img, iImageWidth, iImageHeight, hint, bHighQuality); 

     } else { 

      imgScale = getScaledUpInstance(img, iImageWidth, iImageHeight, hint, bHighQuality); 

     } 

     return imgScale; 

    } 

    protected static BufferedImage getScaledDownInstance(BufferedImage img, 
                              int targetWidth, 
                              int targetHeight, 
                              Object hint, 
                              boolean higherQuality) { 

     int type = (img.getTransparency() == Transparency.OPAQUE) 
          ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; 

     BufferedImage ret = (BufferedImage) img; 
     if (targetHeight > 0 || targetWidth > 0) { 
      int w, h; 
      if (higherQuality) { 
       // Use multi-step technique: start with original size, then 
       // scale down in multiple passes with drawImage() 
       // until the target size is reached 
       w = img.getWidth(); 
       h = img.getHeight(); 
      } else { 
       // Use one-step technique: scale directly from original 
       // size to target size with a single drawImage() call 
       w = targetWidth; 
       h = targetHeight; 
      } 

      do { 
       if (higherQuality && w > targetWidth) { 
        w /= 2; 
        if (w < targetWidth) { 
         w = targetWidth; 
        } 
       } 

       if (higherQuality && h > targetHeight) { 
        h /= 2; 
        if (h < targetHeight) { 
         h = targetHeight; 
        } 
       } 

       BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type); 
       Graphics2D g2 = tmp.createGraphics(); 
       g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); 
       g2.drawImage(ret, 0, 0, w, h, null); 
       g2.dispose(); 

       ret = tmp; 
      } while (w != targetWidth || h != targetHeight); 
     } else { 
      ret = new BufferedImage(1, 1, type); 
     } 
     return ret; 
    } 

    protected static BufferedImage getScaledUpInstance(BufferedImage img, 
                             int targetWidth, 
                             int targetHeight, 
                             Object hint, 
                             boolean higherQuality) { 

     int type = BufferedImage.TYPE_INT_ARGB; 

     BufferedImage ret = (BufferedImage) img; 
     int w, h; 
     if (higherQuality) { 
      // Use multi-step technique: start with original size, then 
      // scale down in multiple passes with drawImage() 
      // until the target size is reached 
      w = img.getWidth(); 
      h = img.getHeight(); 
     } else { 
      // Use one-step technique: scale directly from original 
      // size to target size with a single drawImage() call 
      w = targetWidth; 
      h = targetHeight; 
     } 

     do { 
      if (higherQuality && w < targetWidth) { 
       w *= 2; 
       if (w > targetWidth) { 
        w = targetWidth; 
       } 
      } 

      if (higherQuality && h < targetHeight) { 
       h *= 2; 
       if (h > targetHeight) { 
        h = targetHeight; 
       } 
      } 

      BufferedImage tmp = new BufferedImage(w, h, type); 
      Graphics2D g2 = tmp.createGraphics(); 
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); 
      g2.drawImage(ret, 0, 0, w, h, null); 
      g2.dispose(); 

      ret = tmp; 
      tmp = null; 

     } while (w != targetWidth || h != targetHeight); 
     return ret; 
    } 

} 

製作的使用的JPanel所述面板

BufferedImage backgroundImage = ImageIO.read(...); 
BackgroundPane backgroundPane = new BackgroundPane(); 
backgroundPane.setBackground(backgroundImage); 

所述的面板,用背景圖片,帶有標籤和按鈕

backgroundPane.setLayout(new GridBagLayout()); 
GridBagConstraints gbc = new GridBagConstraints(); 
gbc.gridwidth = GridBagConstraints.REMAINDER; 
backgroundPane.add(new JLabel("This is a label"), gbc); 
backgroundPane.add(new JButton("This is a button"), gbc); 

記住,大多數部件是由默認的不透明,所以你可能需要使用setOpaque(false),使之透明(JLabel是一個例外)

在某些時候,你需要將面板添加到基於窗口的類(如JFrame),因爲這是唯一的方式,您可以顯示它們