需要帮助来纠正我的挥杆应用输出

时间:2012-10-28 13:45:44

标签: java swing event-handling graphics2d

我正在尝试创建一个通常演示src_in AlphaComposite的应用程序。我需要的输出是: -

enter image description here

但我得到以下输出: - enter image description here

我在哪里做错了?这是我的完整代码。

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class SrcInDemo extends JFrame{
private JCheckBox jcb;
SrcInDemo()
{
super("SrcIn Demonstration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);

add(new comp(),BorderLayout.CENTER);

setVisible(true);
add(jcb=new JCheckBox("Shadow"),BorderLayout.SOUTH);
jcb.addChangeListener(new ChangeListener(){

    @Override
    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub
        repaint();
    }});
}
class comp extends JComponent
{
private BufferedImage image,landscape;
comp()
{
    try{
    image=ImageIO.read(/*getClass().getResource("picture.png")*/new File("src/picture.png"));
    landscape=ImageIO.read(/*getClass().getResource("landscape.jpg")*/new File("src/landscape.jpg"));
    }catch(IOException e){System.out.printf("error loading image");}
}
public void paintComponent(Graphics g)
{
//The following commented lines were there in the original code of Filthy Rich Clients
//But I have removed them as it was supposed to create a back image.I have created  g2 
//directly.
            /* BufferedImage temp = new BufferedImage(getWidth(), getHeight(),
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = temp.createGraphics();*/

    Graphics2D g2=(Graphics2D)g.create();
    if(jcb.isSelected())
    {
        int x=(getWidth()-image.getWidth())/2;
        int y=(getHeight()-image.getHeight())/2;
        g2.drawImage(image,x+4,y+4,null);

        Composite oldComposite=g2.getComposite();
        g2.setComposite(AlphaComposite.SrcIn);
        g2.setColor(Color.BLACK);
        g2.fillRect(0,0,getWidth(),getHeight());
        g2.setComposite(oldComposite);
        g2.drawImage(image,x,y,null);
    }
    else
    {
        int x=(getWidth()-image.getWidth())/2;
        int y=(getHeight()-image.getHeight())/2;
        g2.drawImage(image,x,y,null);
        Composite oldComposite=g2.getComposite();
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(landscape,x,y,null);
        g2.setComposite(oldComposite);
    }
            //g.drawImage(temp, 0, 0, null);
}
}

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

2 个答案:

答案 0 :(得分:0)

我已经从Filthy Rich Clients下载了这些示例并运行了示例,没有任何问题。鉴于您的代码是相同的,我建议您的图像存在问题。

Alpha合成在图像的alpha通道上工作。

例如,如果我将盾牌图像改为......(红色,含50%alpha)

enter image description here

我最终......

enter image description here

答案 1 :(得分:0)

经过长时间的研究和搜索后,我找到了解决问题的方法。如果错了,请纠正。我们走了: -

TYPE_INT_ARGB用于具有透明背景的图像.TYPE_INT_ARGB告诉我们的程序我们的图像具有透明背景。如果我们不使用它,那么图像被视为矩形(白色背景+图像)。那么这就是为什么在我的程序中,景观图像也出现在盾形图像之外。实际上它不在盾形图像之外,但它在内部,因为现在盾形图像被认为是一个矩形图像(盾+白色背景)。同样是原因对于阴影case.Hence中的黑色背景我必须使用TYPE_INT_ARGB。