如何在paintComponent方法中向图像添加颜色层? [英] How to add a color layer to the image in paintComponent method?
问题描述
我用Java编写了第一款游戏.所以我有Board类扩展了JPanel,在这个类中我有paintComponent方法.卡片图像是BufferedImages.
I write my first game in Java. So I have Board class extends JPanel, and inside this class I have paintComponent method. Cards images are BufferedImages.
现在...我刚刚完成了计算玩家可能移动的方法.为了明确起见,我有敌方牌场,并且每当敌方CardField在玩家移动范围内时,我想在敌方cardField图像中添加一些图层.
Now... I just finished method, that calculate possible player moves. To clarify, I have enemy card Fields, and I want to add some layer to enemy cardField image every time when enemy CardField is within player move.
我在paintComponent方法中的代码是这样的:
My code in paintComponent method is something like this:
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
}
我的目标是:
正常:
突出显示:
感谢您的帮助:)
干杯!
推荐答案
例如,您可以利用AlphaComposite
更改图形的呈现方式.
You could make use of a AlphaComposite
to change the way in which the graphics are rendered, for example...
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();
}
}
}
现在,您实际上可以预渲染此图像(具有正常"和突出显示"的图像),但这取决于您的需求
Now, you could actually pre-render this (have a "normal" and "highlighted" image), but that comes down to your needs
nb:我使用了当前外观的List.selectionBackground
颜色作为填充色,您可以将其替换为所需的任何颜色
nb: I've used the List.selectionBackground
color from the current look and feel for the fill color, you can replace it with what ever color you want