- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何快速有效地设置 BufferedImage
的所有像素?透明化,以便我可以简单地为每一帧重新绘制我想要的 Sprite 图形?
我正在用 Java 设计一个简单的游戏引擎来更新背景和前景 BufferedImage
并将它们绘制成复合 VolatileImage
为了有效缩放,绘制到 JPanel
.这种可扩展模型允许我添加更多层并迭代每个绘图层。
我将我的应用程序简化为下面给出的一个类来演示我的问题。使用箭头键在图像上移动一个红色方 block 。挑战在于我想将更新游戏图形与将合成图形绘制到游戏引擎分离。我已经研究了这个问题的看似彻底的答案,但无法弄清楚如何将它们应用到我的应用程序中:
这里是没有正确清除像素的临界区。注释掉的部分来 self 已经阅读过的堆栈溢出答案,但它们将背景绘制为不透明的黑色或白色。我知道 foregroundImage
在我的实现中从透明像素开始,因为您可以看到 backgroundImage
的随机像素噪声应用程序开始时在红色 Sprite 后面。此时图像没有被清除,所以之前绘制的图像仍然存在。
/** Update the foregroundGraphics. */
private void updateGraphics(){
Graphics2D fgGraphics = (Graphics2D) foregroundImage.getGraphics();
// set image pixels to transparent
//fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
//fgGraphics.setColor(new Color(0,0,0,0));
//fgGraphics.clearRect(0, 0, width, height);
//fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// draw again.
fgGraphics.setColor(Color.RED);
fgGraphics.fillRect(sx, sy, spriteSize, spriteSize);
fgGraphics.dispose();
}
这是我的整个示例代码:
/**
* The goal is to draw two BufferedImages quickly onto a scalable JPanel, using
* a VolatileImage as a composite.
*/
public class Example extends JPanel implements Runnable, KeyListener
{
private static final long serialVersionUID = 1L;
private int width;
private int height;
private Object imageLock;
private Random random;
private JFrame frame;
private Container contentPane;
private BufferedImage backgroundImage;
private BufferedImage foregroundImage;
private VolatileImage compositeImage;
private Graphics2D compositeGraphics;
private int[] backgroundPixels;
private int[] foregroundPixels;
// throttle the framerate.
private long prevUpdate;
private int frameRate;
private int maximumWait;
// movement values.
private int speed;
private int sx;
private int sy;
private int dx;
private int dy;
private int spriteSize;
/** Setup required fields. */
public Example(){
width = 512;
height = 288;
super.setPreferredSize(new Dimension(width, height));
imageLock = new Object();
random = new Random();
frame = new JFrame("BufferedImage Example");
frame.addKeyListener(this);
contentPane = frame.getContentPane();
contentPane.add(this, BorderLayout.CENTER);
// used to create hardware-accelerated images.
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
backgroundImage = gc.createCompatibleImage(width, height,Transparency.TRANSLUCENT);
foregroundImage = gc.createCompatibleImage(width, height,Transparency.TRANSLUCENT);
compositeImage = gc.createCompatibleVolatileImage(width, height,Transparency.TRANSLUCENT);
compositeGraphics = compositeImage.createGraphics();
compositeGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
compositeGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
backgroundPixels = ((DataBufferInt) backgroundImage.getRaster().getDataBuffer()).getData();
foregroundPixels = ((DataBufferInt) foregroundImage.getRaster().getDataBuffer()).getData();
//initialize the background image.
for(int i = 0; i < backgroundPixels.length; i++){
backgroundPixels[i] = random.nextInt();
}
// used to throttle frames per second
frameRate = 180;
maximumWait = 1000 / frameRate;
prevUpdate = System.currentTimeMillis();
// used to update sprite state.
speed = 1;
dx = 0;
dy = 0;
sx = 0;
sy = 0;
spriteSize = 32;
}
/** Renders the compositeImage to the Example, scaling to fit. */
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draw the composite, scaled to the JPanel.
synchronized (imageLock) {
((Graphics2D) g).drawImage(compositeImage, 0, 0, super.getWidth(), super.getHeight(), 0, 0, width, height, null);
}
// force repaint.
repaint();
}
/** Update the BufferedImage states. */
@Override
public void run() {
while(true){
updateSprite();
updateGraphics();
updateComposite();
throttleUpdateSpeed();
}
}
/** Update the Sprite's position. */
private void updateSprite(){
// update the sprite state from the inputs.
dx = 0;
dy = 0;
if (Command.UP.isPressed()) dy -= speed;
if (Command.DOWN.isPressed()) dy += speed;
if (Command.LEFT.isPressed()) dx -= speed;
if (Command.RIGHT.isPressed()) dx += speed;
sx += dx;
sy += dy;
// adjust to keep in bounds.
sx = sx < 0 ? 0 : sx + spriteSize >= width ? width - spriteSize : sx;
sy = sy < 0 ? 0 : sy + spriteSize >= height ? height - spriteSize : sy;
}
/** Update the foregroundGraphics. */
private void updateGraphics(){
Graphics2D fgGraphics = (Graphics2D) foregroundImage.getGraphics();
// set image pixels to transparent
//fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
//fgGraphics.setColor(new Color(255, 255, 255, 255));
//fgGraphics.clearRect(0, 0, width, height);
//fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// draw again.
fgGraphics.setColor(Color.RED);
fgGraphics.fillRect(sx, sy, spriteSize, spriteSize);
fgGraphics.dispose();
}
/** Draw the background and foreground images to the volatile composite. */
private void updateComposite(){
synchronized (imageLock) {
compositeGraphics.drawImage(backgroundImage, 0, 0, null);
compositeGraphics.drawImage(foregroundImage, 0, 0, null);
}
}
/** Keep the update rate around 60 FPS. */
public void throttleUpdateSpeed(){
try {
Thread.sleep(Math.max(0, maximumWait - (System.currentTimeMillis() - prevUpdate)));
prevUpdate = System.currentTimeMillis();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
/** Ignore key typed events. */
@Override
public void keyTyped(KeyEvent e) {}
/** Handle key presses. */
@Override
public void keyPressed(KeyEvent e) {
setCommandPressedFrom(e.getKeyCode(), true);
}
/** Handle key releases. */
@Override
public void keyReleased(KeyEvent e) {
setCommandPressedFrom(e.getKeyCode(), false);
}
/** Switch over key codes and set the associated Command's pressed value. */
private void setCommandPressedFrom(int keycode, boolean pressed){
switch (keycode) {
case KeyEvent.VK_UP:
Command.UP.setPressed(pressed);
break;
case KeyEvent.VK_DOWN:
Command.DOWN.setPressed(pressed);
break;
case KeyEvent.VK_LEFT:
Command.LEFT.setPressed(pressed);
break;
case KeyEvent.VK_RIGHT:
Command.RIGHT.setPressed(pressed);
break;
}
}
/** Commands are used to interface with key press values. */
public enum Command{
UP, DOWN, LEFT, RIGHT;
private boolean pressed;
/** Press the Command. */
public void press() {
if (!pressed) pressed = true;
}
/** Release the Command. */
public void release() {
if (pressed) pressed = false;
}
/** Check if the Command is pressed. */
public boolean isPressed() {
return pressed;
}
/** Set if the Command is pressed. */
public void setPressed(boolean pressed) {
if (pressed) press();
else release();
}
}
/** Begin the Example. */
public void start(){
try {
// create and display the frame.
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
Example e = new Example();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
// start updating from key inputs.
Thread t = new Thread(this);
t.start();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
/** Start the application. */
public static void main(String[] args){
Example e = new Example();
e.start();
}
}
编辑:
- 修复了初始化 backgroundPixels
的 for 循环中的拼写错误随机。
最佳答案
原来我在选择方法时犯了错误。我注意到我正在清除一个一像素宽的框,它是我的图形的轮廓。这是因为我不小心使用了 drawRect()
而不是 fillRect()
。更改我的代码后,它现在可以工作了。以下是我能够开始工作的示例。
使用 AlphaComposite.CLEAR
的示例(使用任何不透明颜色绘制):
// clear pixels
fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
fgGraphics.setColor(new Color(255,255,255,255));
fgGraphics.fillRect(0, 0, width, height);
fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// draw new graphics
使用 AlphaComposite.SRC_OUT
的示例(使用任何带有 alpha 零的颜色绘制):
// clear pixels
fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OUT));
fgGraphics.setColor(new Color(255,255,255,0));
fgGraphics.fillRect(0, 0, width, height);
fgGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// draw new graphics
关于java - 将 BufferedImage 的像素设置为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149206/
为什么这个脚本不起作用?仅当页面宽度超过 915 像素时,我希望单击按钮时滚动页面 100 像素。我试图通过仅在宽度超过 915 像素时允许该函数运行来实现此目的,但它没有发生。怎么办? $(docu
我需要您帮助我建立一个网站。我是一个新手,已经得到了一个设计为 900 像素宽的主体,但他们给了我一个 1200 像素宽的图像。他们希望图像跨越整个 1200 像素的宽度,因此页面两侧基本上会有 30
我有一个在 y 轴上展开的 UIScrollview 并调用这个委托(delegate)方法: -(void)scrollViewDidScroll:(UIScrollView *)scrollVie
我有一个固定的标题这个标题在我滚动时改变高度和图像标志但是当我调整窗口大小时我希望图像保持比例但随着我缩小浏览器而变得更小标志只有在限制时缩小浏览器靠近图像,但我希望在调整浏览器大小时图像变小。 我该
在我的项目中,我使用 ArcGIS API for JavaScript https://developers.arcgis.com/javascript/但是对于(在这里插入非常大的坏词)我无法覆盖
有没有办法使用 jQuery,根据窗口滚动的距离做不同的事情? 这是我现在使用的代码; $(document).scroll(function() { // If scroll distanc
这基本上是 Jetpack Joyride 中运动的基本版本,但不是 Joyrider 以每秒 100 像素的速度下降,而是字母“x”从控制台的正中间以每秒 100 像素的速度下降和点击事件会导致它以
我像这样处理 MINMAXINFO: case WM_GETMINMAXINFO: { LPMINMAXINFO p_info = (LPMINMAXINFO)lPar
我对 javascript 有点陌生,我一直在查找 documentElement、clientWidth 和 clientHeight 并试图找出为什么它将我的 Canvas 设置为 300px x
我正在编写一些软件来读取 DICOM 文件,但我不确定如何处理具有未定义长度的标签。标准是这样说的 “如果值字段具有显式长度,则值长度字段应包含等于长度(以字节为单位)的值 值字段。否则,值字段 有一
我对 OpenGL 有点陌生,但我很确定我的问题在于所使用的像素格式,或者我的纹理是如何生成的...... 我正在使用 16 位 RGB5_A1 像素格式在平面 2D 四边形上绘制纹理,但在这个阶段我
有没有办法获取直播电视流,例如在像素级别上进行分析。 我的目标是检查直播电视流(例如使用java),例如广播电台 Logo 是否可见。 有机会通过 Google 电视观看此直播吗? 是否有机会通过笔记
我正在尝试构建一个函数,它以给定角度从特定坐标延伸,并循环遍历该线上的像素,直到遇到黑色像素. 如果角度为 180 度,这很容易实现。在这种情况下,搜索只会向下扩展,在每次迭代中将列坐标加 1。然而,
我已经研究了一段时间,但找不到任何解决方案。 这是我的代码 如果您将此代码复制并粘贴到本网站的 HTML 区域:http://jsfiddle.net/T3Nnu/3/ 如果您查看 Facebo
我有一个网页 - http://bit.ly/YHFX5B如果你看一下页脚,你会发现它后面有一些额外的白色像素/线条。我不明白他们是从哪里来的。 请告知他们可能来自哪里。 谢谢,丹 最佳答案 在 #f
如何在没有状态栏和操作栏的情况下获取屏幕高度(像素)或者如果有人告诉我如何获取状态栏和操作栏的高度,它也会有所帮助。我已经找到了屏幕高度,但它包括状态栏和操作栏.我将支持库 v7 用于操作栏。我在网上
Java 字符串根据宽度(像素)换行 在一些场景下,我们经常会通过判断字符串的长度,比如个数来实现换行,但是中文、英文、数字、其实在展示的时候同样长度的字符串,其实它的宽度是不一样的,这也是们我通
我创建了一个不错的简单可扩展列表。它应该像单选列表一样工作,您应该只能选择一个元素。我还没有实现这部分,因为我对列表的大小有疑问: class ExpandableListRadio extends
我使用以下代码滚动到元素顶部,但我想滚动到元素顶部上方 10px,不知道如何执行此操作,有什么建议吗?谢谢! $('html, body').stop(true,true).animate({
我有一个链接,可以在滚动时更改其垂直位置。当我点击此链接时,我想(平滑地)转到页面上的某个位置,该位置距离页面顶部正好 1080 像素。 我无法实现它,希望有人能帮助我。 链接: 脚本: $(do
我是一名优秀的程序员,十分优秀!