1 java swing image paint jpanel
我尝试从PNG图片构建我的个人JFrame.但Mac OSX 10.8和Windows 7之间存在不同的行为.(我必须使用JDK 6)
这是我的代码:
[...]
public Fenetre()
{
this.setLocationRelativeTo(null);
this.setUndecorated(true);
this.setBackground(new Color(0,0,0,0));
try {
image = ImageIO.read(this.getClass().getResource("/Images/frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.setSize(image.getWidth(),image.getHeight());
this.setLayout(null);
panel = new JPanel();
JButton quit = new JButton("Quitter");
panel.add(quit);
Dimension size = panel.getPreferredSize();
panel.setBounds(67, 45, size.width, size.height);
this.add(panel);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2 =(Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); // SRC_ATOP > Windows
g2.drawImage(image, 0, 0, this);
panel.update(panel.getGraphics());
}
[...]
Run Code Online (Sandbox Code Playgroud)
Mac OSX 10.8(AlphaComposite = SRC)上的结果:
http://imageshack.us/photo/my-images/15/maczr.png/
然后,在Windows 7(AlphaComposite = SRC_ATOP)上,在启动时和移动它时,我可以看到:
http://imageshack.us/photo/my-images/16/windowsqu.jpg/
怎么样?
你的代码不完整,但在我看来,你正在重写JFrame的paint()方法.你永远不应该这样做(除非你知道你在做什么,你调用super.paint(..))!
如果要在框架中显示图像,则:
a)将带有图像的JLabel添加到框架
b)或通过覆盖paintComponent()方法在JPanel上进行自定义绘制,然后将面板添加到框架中.
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |