2015-09-01 125 views
0

我用Java编写我的第一个游戏。所以我有Board类扩展JPanel,并且在这个类中我有paintComponent方法。卡片图像是BufferedImages。如何将颜色层添加到paintComponent方法中的图像?

现在...我刚刚完成了方法,计算可能的球员移动。为了说明一下,我有敌人卡场,并且每当敌方卡场在玩家移动范围内时,我想为敌人卡场图添加一些图层。

我在的paintComponent方法的代码是这样的:

if(!field.isWithinMove()){ 
//draw normal state card 
    g.drawImage(field.getLoadedCard().getCardImage(), 
    field.getX(), field.getY(), Card.cardWidth, Card.cardHeight, null); 
} 
else{ 
//there should be a card picture with layer of color 
} 

我的目标是:

正常:

Normal

突出显示:

enter image description here

我将不胜感激您的帮助:)

干杯!

回答

4

您正在寻找alpha组件。颜色可以表示为RGB或RGBA。区别在于RGBA增加了一个额外的透明通道。

... 

Color overlay = new Color(0 , 0 , 200 , 
     150//alpha component 
); 

g.setColor(overlay); 
g.fillRectangle(...);//fill in position of the image here 

越高阿尔法复合材料中,颜色的不透明度越高,其中,255是像传统的RGB彩色画没有阿尔法分量和:因此,基本上可以使用这种附加的信道油漆过的图像的矩形0就像根本不绘画(完全透明)。

+0

非常感谢您! :) –

5

你可以让使用AlphaComposite来改变其中的图形渲染,例如方式...

Normal and highlighted

import java.awt.AlphaComposite; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage img; 

     public TestPane() { 
      try { 
       img = ImageIO.read(source of your image here); 
      } catch (IOException ex) { 
       Logger.getLogger(JavaApplication393.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(189 * 2, 270); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      // Normal 
      g2d.drawImage(img, 0, 0, this); 
      // Highlighted 
      g2d.drawImage(img, img.getWidth(), 0, this); 
      g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); 
      g2d.setColor(UIManager.getColor("List.selectionBackground")); 
      g2d.fillRect(img.getWidth(), 0, img.getWidth(), img.getHeight()); 
      g2d.dispose(); 
     } 

    } 

} 

现在,你实际上可以预渲染这个(有“正常”和“突出”的形象),但归结到您的需求

注:我从当前外观使用的颜色List.selectionBackground和感觉填充颜色,可以用什么前夕更换R颜色你想

看一看Trail: 2D GraphicsCompositing Graphics更多细节

相关问题