如何从Java Applet创建BufferedImage(用于屏幕截图)?

时间:2016-01-03 18:21:29

标签: java swing canvas applet awt

我正在为游戏Runescape创建一个轻量级客户端,它具有各种用户功能,如自定义键绑定/热键。我对java和swing很不错,但是AWT和applets我最多也是平庸的。我能够将applet下载并显示在JPanel上,然后显示在JFrame中,游戏到目前为止运行完美。但是,当我尝试向客户端实现屏幕截图功能时,添加功能时遇到了问题。

很难发布一个简洁的工作示例,所以我将根据我正在阅读的其他一些SO答案发布我正在尝试的方法。我尝试从Applet,applet Canvas,它处理的JPanel和JFrame创建BufferedImage。

以下是我尝试从Applet画布创建屏幕截图的一些内容:

public BufferedImage getScreenShot() {
    //getting the applet canvas
    Canvas canvas = (Canvas) this.getApplet().getComponent(0);
    //bufferedImage to draw to
    BufferedImage image = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_INT_ARGB);

    //Graphics2D g2=(Graphics2D)image.getGraphics();
    //test.print(g2);

    //g2.dispose();

    Graphics g = image.getGraphics();
    Image image2 = canvas.createImage(canvas.getWidth(), canvas.getHeight());
    canvas.print(g);

    System.out.println("Canvas Size: " + canvas.getSize().width + " x " + canvas.getSize().height);
    return image;
}

这是我在JPanel类中尝试过的,它只包含Applet:

public BufferedImage getScreenShotFINAL(){
    BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    applet.paint(bi.createGraphics());
    return bi;
}

我理解这里的想法是创建一个离屏的BufferedImage,然后创建一个Graphics2d对象,然后调用applet的paint()方法绘制到屏幕外的图像。我从here尝试了这个解决方案:

public BufferedImage getScreenShotFINAL2() {
    Dimension size = applet.getSize();
    BufferedImage offScreenImage = (BufferedImage) applet.createImage(size.width, size.height);
    Graphics2D g2 = offScreenImage.createGraphics();
    g2.setBackground(applet.getBackground());

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.clearRect(0, 0, size.width, size.height);

    applet.paint(g2);

    return offScreenImage;
}

当保存BufferedImages这些和其他方法时,我试图将文件显示为黑色,只是默认的背景颜色。有没有一个很好的方法来从JPanel获得一个包含Applet的BufferedImage,所以我永远不必直接处理Applet?另一件事,我不得不重写java Canvas类,以便让游戏显示(因为它是双缓冲我相信)。我尝试从这里开始使用一堆解决方案,但没有成功。任何建议将不胜感激。如果您愿意,我还可以提供完整项目或自定义Canvas项目的链接。谢谢!

修改

这是覆盖java AWT的Canvas类。没有写它,遵循这个教程,我认为这是问题所在,因为paint()方法只调用clearRect():

package java.awt;

import Client.Client;
import java.awt.image.BufferStrategy;
import java.awt.peer.CanvasPeer;
import javax.accessibility.*;
import loaders.ClientPool;

public class Canvas extends Component implements Accessible {

private Client client = null;
private static final String base = "canvas";
private static int nameCounter = 0;
 private static final long serialVersionUID = -2284879212465893870L;

public Canvas() {
    super();
}

public Canvas(GraphicsConfiguration config) {
    this();
    setGraphicsConfiguration(config);
}
@Override
void setGraphicsConfiguration(GraphicsConfiguration gc) {
    synchronized(getTreeLock()) {
        CanvasPeer peer = (CanvasPeer)getPeer();
        if (peer != null) {
            gc = peer.getAppropriateGraphicsConfiguration(gc);
        }
        super.setGraphicsConfiguration(gc);
    }
}

@Override
public Graphics getGraphics(){
    if (this.client == null) {
        this.client = ClientPool.getClient(this);
    }

    if (client != null) {
        //call custom draw functions for specific client.  for drawing/double buffering
        return client.drawGraphics((Graphics2D) super.getGraphics());
    }
    return super.getGraphics();
}

@Override
String constructComponentName() {
    synchronized (Canvas.class) {
        return base + nameCounter++;
    }
}

@Override
public void addNotify() {
    synchronized (getTreeLock()) {
        if (peer == null)
            peer = getToolkit().createCanvas(this);
        super.addNotify();
    }
}

@Override
public void paint(Graphics g) {
    g.clearRect(0, 0, width, height);
}

@Override
public void update(Graphics g) {
    g.clearRect(0, 0, width, height);
    super.paint(g);
}

@Override
boolean postsOldMouseEvents() {
    return true;
}

@Override
public void createBufferStrategy(int numBuffers) {
    super.createBufferStrategy(numBuffers);
}

@Override
public void createBufferStrategy(int numBuffers,
    BufferCapabilities caps) throws AWTException {
    super.createBufferStrategy(numBuffers, caps);
}

@Override
public BufferStrategy getBufferStrategy() {
    return super.getBufferStrategy();
}

@Override
public AccessibleContext getAccessibleContext() {
    if (accessibleContext == null) {
        accessibleContext = new AccessibleAWTCanvas();
    }
    return accessibleContext;
}

protected class AccessibleAWTCanvas extends AccessibleAWTComponent
{
    private static final long serialVersionUID = -6325592262103146699L;

    public AccessibleRole getAccessibleRole() {
        return AccessibleRole.CANVAS;
    }
}

}

0 个答案:

没有答案