2013-01-21 79 views
2

我目前正在使用swing設計Pac人。我有使用以下語句在屏幕上繪製的PNG圖像。Java Swing中糟糕的PNG渲染(低顏色深度)

wall = new ImageIcon(GamePanel.class.getResource("wall.png")).getImage(); 
g2d.drawImage(wall, x, y, this); 

我遇到的問題是,它似乎呈現實際的文件非常低的顏色深度演繹。它看起來確實保持透明度(灰色背景是Panel bg顏色),但是它會丟失顏色深度。

實際圖像看起來像這樣:enter image description here運行時,它看起來像這樣:enter image description here

有沒有人有辦法解決嗎?謝謝!

回答

2

解決。 paint()方法中使用了repaint()。刪除它,它的工作。

感謝您的幫助。

+1

*「已解決。」*恭喜!很高興你把事情解決了。 :) –

+0

@ user1305850:對不起,如果我是importunate。 :-)在你的問題中,黑暗的圖像讓我想起了重複應用默認'AlphaComposite.SRC_OVER'規則的結果,所以你的回答似乎是合理的。 OTOH,你接受另一個人的答案會得到兩分! – trashgod

3

試試這個設置上​​: -

Map<RenderingHints.Key, Object> map = new HashMap<RenderingHints.Key, Object>(); 
    map.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
    map.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
    map.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
    RenderingHints renderHints = new RenderingHints(map); 
    g2d.setRenderingHints(renderHints); 
+0

這不起作用不幸... – user1305850

4

有些東西似乎第二圖像中肯定是不對的。在這裏看到它在一個黑色的BG,它看起來非常不同 - 沒有'光環'。

Test Yellow Dot Image

import java.awt.*; 
import java.net.URL; 
import javax.swing.*; 

public class TestYellowDotImage { 

    public static JLabel getColoredLabel(Icon icon, Color color) { 
     JLabel label = new JLabel(icon); 
     label.setBackground(color); 
     label.setOpaque(true); 

     return label; 
    } 

    public static void main(String[] args) throws Exception { 
     URL url = new URL("http://i.stack.imgur.com/1EZVZ.png"); 
     final Icon icon = new ImageIcon(url); 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       JPanel gui = new JPanel(new GridLayout(0, 4)); 

       gui.add(new JLabel(icon)); 
       gui.add(getColoredLabel(icon, Color.BLACK)); 
       gui.add(getColoredLabel(icon, Color.WHITE)); 
       gui.add(getColoredLabel(icon, Color.RED)); 

       JOptionPane.showMessageDialog(null, gui); 
      } 
     }; 
     // Swing GUIs should be created and updated on the EDT 
     // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html 
     SwingUtilities.invokeLater(r); 
    } 
} 
+1

+1必須是渲染;我看到了和你的結果類似的結果。 – trashgod

+0

嘗試使用'ImageIO'加載圖像,而不是使用'ImageIcon'。在加載過程中'ImageIO'塊,'ImageIcon'異步加載。 –

+0

IIUC,'ImageIcon(url)'說:「圖像將被預加載...」在內部,它使用'loadImage()',其中「加載圖像,僅當圖像加載時才返回」。 – trashgod

3

使用Grid,可以將鼠標懸停在像素看阿爾法分量在該圖標的邊緣如何滾降。

public Grid(String name) { 
    this.setBackground(new Color(0xFFFFFFC0)); 
    Icon icon = null; 
    try { 
     icon = new ImageIcon(new URL("http://i.stack.imgur.com/1EZVZ.png")); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(System.err); 
    } 
    ... 
} 

image

IconTest顯示圖標與變化的α和默認AlphaComposite.SRC_OVER規則如何呈現。

enter image description here

/*** @see https://stackoverflow.com/a/14432025/230513 */ 
public class IconTest { 

    private static final int N = 8; 
    private static final Random r = new Random(); 

    public static void main(String[] args) throws MalformedURLException { 
     final URL url = new URL("http://i.stack.imgur.com/1EZVZ.png"); 
     final Icon icon = new ImageIcon(url); 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame f = new JFrame("IconTest"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       JPanel p = new JPanel(new GridLayout(N, N)); 
       for (int i = 0; i < N * N; i++) { 
        final JLabel label = new JLabel(icon); 
        label.setOpaque(true); 
        label.setBackground(new Color(0, 0, 255, i)); 
        p.add(label); 
       } 
       f.add(p); 
       f.pack(); 
       f.setLocationRelativeTo(null); 
       f.setVisible(true); 
      } 
     }); 
    } 
} 
+0

當我在機器上運行此代碼時,我得到了相同的結果。所以它必須是我以前的代碼。我會發布它的全部內容。 – user1305850