2013-03-05 169 views
2

我正在嘗試將圖像添加到帶有圓角和陰影的this RoundedPane。但我沒有得到它。帶有陰影和圓形邊緣的java swing中的圖像

我所做的面板從這個類

public class JPanelConFondo extends JPanel { 

    private Image imagen; 

    public JPanelConFondo() { 
    } 

    public JPanelConFondo(String nombreImagen) { 
     if (nombreImagen != null) { 
      imagen = new ImageIcon(getClass().getResource(nombreImagen)).getImage(); 
     } 
    } 

    public JPanelConFondo(Image imagenInicial) { 
     if (imagenInicial != null) { 
      imagen = imagenInicial; 
     } 
    } 

    public void setImagen(String nombreImagen) { 
     if (nombreImagen != null) { 
      imagen = new ImageIcon(getClass().getResource(nombreImagen)).getImage(); 
     } else { 
      imagen = null; 
     }  
     repaint(); 
    } 

    public void setImagen(Image nuevaImagen) { 
     imagen = nuevaImagen;  
     repaint(); 
    } 

    @Override 
    public void paint(Graphics g) { 
     if (imagen != null) { 
      g.drawImage(imagen, 0, 0,null, this);  
      setOpaque(false); 
     } else { 
      setOpaque(true); 
     }  
     super.paint(g); 
    } 
} 

延伸,但把圖象不能得到保持圓角和陰影重繪。

並試圖添加一個JLabel與圖像的面板,但沒有達到預期的結果。

我將不勝感激任何幫助。 感謝

+1

不知道爲什麼被低估,太努力工作 – mKorbel 2013-03-05 20:47:00

回答

7

在鏈路調整my answer你提供我能做到這一點(與How to make a rounded corner image in Java +1幫助筆者):

enter image description here

import java.awt.AlphaComposite; 
import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.awt.RenderingHints; 
import java.awt.geom.RoundRectangle2D; 
import java.awt.image.BufferedImage; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class ShadowTest { 

    private JFrame frame; 

    public ShadowTest() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new ShadowTest(); 
      } 
     }); 
    } 

    private void initComponents() { 
     frame = new JFrame(); 
     frame.setTitle("Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//app exited when frame closes 
     frame.setResizable(false); 
     frame.setLayout(new GridBagLayout()); 
     GridBagConstraints gc = new GridBagConstraints(); 
     gc.fill = GridBagConstraints.HORIZONTAL; 
     gc.insets = new Insets(10, 10, 10, 10); 
     frame.add(new RoundedPanel(), gc); 

     //pack frame (size components to preferred size) 
     frame.pack(); 
     frame.setVisible(true);//make frame visible 
    } 
} 

class RoundedPanel extends JPanel { 

    /** 
    * Stroke size. it is recommended to set it to 1 for better view 
    */ 
    protected int strokeSize = 1; 
    /** 
    * Color of shadow 
    */ 
    protected Color shadowColor = Color.black; 
    /** 
    * Sets if it drops shadow 
    */ 
    protected boolean shady = true; 
    /** 
    * Sets if it has an High Quality view 
    */ 
    protected boolean highQuality = true; 
    /** 
    * Double values for Horizontal and Vertical radius of corner arcs 
    */ 
    //protected Dimension arcs = new Dimension(0, 0); 
    protected Dimension arcs = new Dimension(20, 20);//creates curved borders and panel 
    /** 
    * Distance between shadow border and opaque panel border 
    */ 
    protected int shadowGap = 10; 
    /** 
    * The offset of shadow. 
    */ 
    protected int shadowOffset = 4; 
    /** 
    * The transparency value of shadow. (0 - 255) 
    */ 
    protected int shadowAlpha = 150; 
    int width = 300, height = 300; 
    BufferedImage image; 
    BufferedImage roundedImage; 

    public RoundedPanel() { 
     super(); 
     setOpaque(false); 
     try { 
      image = ImageIO.read(new URL("http://www.wallpaperpimper.com/wallpaper/Anime_&_Manga/Naruto/Naruto-Vs-Sasuke-1-D6UGW3NLAZ-800x600.jpg")); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     roundedImage = makeRoundedCorner(image, 20);//make image round 

     width = roundedImage.getWidth()+arcs.width/2;//set width and height of panel accordingly 
     height = roundedImage.getHeight()+arcs.height/2; 
    } 

    public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) { 
     int w = image.getWidth(); 
     int h = image.getHeight(); 
     BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 

     Graphics2D g2 = output.createGraphics(); 

     // This is what we want, but it only does hard-clipping, i.e. aliasing 
     // g2.setClip(new RoundRectangle2D ...) 

     // so instead fake soft-clipping by first drawing the desired clip shape 
     // in fully opaque white with antialiasing enabled... 
     g2.setComposite(AlphaComposite.Src); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(Color.WHITE); 
     g2.fill(new RoundRectangle2D.Float(0, 0, w, h, cornerRadius, cornerRadius)); 

     // ... then compositing the image on top, 
     // using the white shape from above as alpha source 
     g2.setComposite(AlphaComposite.SrcAtop); 
     g2.drawImage(image, 0, 0, null); 

     g2.dispose(); 

     return output; 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Color shadowColorA = new Color(shadowColor.getRed(), 
       shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha); 
     Graphics2D graphics = (Graphics2D) g; 

     //Sets antialiasing if HQ. 
     if (highQuality) { 
      graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
        RenderingHints.VALUE_ANTIALIAS_ON); 
     } 

     //Draws shadow borders if any. 
     if (shady) { 
      graphics.setColor(shadowColorA); 
      graphics.fillRoundRect(
        shadowOffset,// X position 
        shadowOffset,// Y position 
        width - strokeSize - shadowOffset, // width 
        height - strokeSize - shadowOffset, // height 
        arcs.width, arcs.height);// arc Dimension 
     } else { 
      shadowGap = 1; 
     } 

     //Draws the rounded opaque panel with borders. 
     graphics.setColor(getBackground()); 
     graphics.fillRoundRect(0, 0, width - shadowGap, 
       height - shadowGap, arcs.width, arcs.height); 
     graphics.setColor(getForeground()); 
     graphics.setStroke(new BasicStroke(strokeSize)); 
     graphics.drawRoundRect(0, 0, width - shadowGap, 
       height - shadowGap, arcs.width, arcs.height); 

     graphics.drawImage(roundedImage, 0, 0, this);//draw the rounded image 

     //Sets strokes to default, is better. 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(width, height); 
    } 
} 
+1

我愛你! 非常感謝。 – user60108 2013-03-05 21:13:38

+0

@ user60108很高興有人幫忙 – 2013-03-05 21:16:13

1

實際的答案將取決於你對影子的定義是...

結帳

對於實施例。他們都使用類似的掩膜技術來生成非矩形形狀的陰影,這應該有助於

+0

OP已經鏈接你的第二個項目符號說*我正在試圖添加一個圖像到[RoundedPane](http://stackoverflow.com/questions/13368103/jpanel-drop-shadow/ 13369038#13369038)與圓角和陰影。但我沒有得到它。*因​​此,我猜他看到你的答案現在需要一個圖像添加到它的圓形邊界。第一項子彈+1好陰影效果 – 2013-03-05 21:14:08

+0

@DavidKroukamp哇,錯過了。不能說這很難,可能需要一些裁剪,它的基本思路是順便給你的答案+1 – MadProgrammer 2013-03-05 21:17:14