我用Java编写了我的第一个游戏.所以我有Board类扩展JPanel,在这个类里面我有paintComponent方法.卡片图像是BufferedImages.
现在......我刚刚完成方法,计算可能的球员动作.为了澄清,我有敌人卡片场,并且每当敌人的CardField在玩家移动时我想向敌人的cardField图像添加一些图层.
我在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
}
Run Code Online (Sandbox Code Playgroud)
我的目标是:
正常:
精彩推荐:
我将非常感谢你的帮助:)
干杯!
您可以使用a AlphaComposite来改变图形的渲染方式,例如......
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以实际预渲染(具有"正常"和"突出显示"图像),但这取决于您的需求
nb:我使用了List.selectionBackground当前外观和颜色的填充颜色,你可以用你想要的颜色替换它
有关详细信息,请查看Trail:2D图形和合成图形
| 归档时间: |
|
| 查看次数: |
229 次 |
| 最近记录: |