为什么图形不绘画?

时间:2013-12-06 22:08:14

标签: java 2d gpu

我搜索谷歌这个问题,但我无法真正解决它们。

有关以下代码的更多信息。

以下是两段相关代码:

//prepare
            g = (Graphics2D) strategy.getDrawGraphics();
            g.setColor(Color.white);
            g.fillRect(0,0,getWidth(),getHeight());

            for(ListIterator<Chunk> it = chunkvector.listIterator();it.hasNext();){
                Chunk r = it.next();
                r.drawChunk(g);
            }

            for(ListIterator<Entity> it = vectorpainter.listIterator();it.hasNext();){
                Entity r = it.next();
                r.drawObjects(g);
            }
            //Chat

            if(coni.getVerified()){
            try {
                chat.update(g);
            } catch (IOException e) {
                println("I/O Exception while updating chat! "+e.toString());
            }


            //Framerate Anzeige
            if(fps_on){
                g.setColor(Color.red);
                g.drawString("FPS: "+fps, 20, 20);
            }
            //Tickberechnung & Anzeige
            tick++;
            if(tick>=65536)
                    tick=0;
            if(tick_on){
                g.setColor(Color.red);
                g.drawString("Tick: "+tick,80,20);
            }

            //end drawing
            g.dispose();
            strategy.show();
有趣的是: 大块,实体都画了,白色矩形,但是fps和tick从来没有(当然都是真的),我甚至写过System.out.print(“...”);进入if子句,它被执行! :S 我认为这是关于GPU加速的,所以我添加了第二个代码块,如何加载图像。一个实体也是drawString,(显示头顶的玩家名称)并且它可以工作,但在主线程中没有? :(

第二

public Image[] loadPics(String path, int cnt){
    Image[] anim = new Image[cnt];
    BufferedImage source = null;
    BufferedImage temp;

    URL pic_url = getClass().getResource(path);
    if (pic_url == null) {
        fail("Can't find ref: "+path);
    }
    try{
        source = ImageIO.read(pic_url);
    }catch(IOException e){
        fail("Failed to load: "+path);
    }
    // create an accelerated image of the right size to store our sprite in
    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

    for(int x=0;x<cnt;x++){
        temp = source.getSubimage(x*source.getWidth()/cnt, 0, 
                source.getWidth()/cnt, source.getHeight());
        anim[x]= gc.createCompatibleImage(temp.getWidth(), temp.getHeight(), Transparency.BITMASK);
        anim[x].getGraphics().drawImage(temp,0,0,null);

    }
    System.out.println(pic_url+" - loaded.");
    return anim;        
}

第二个我从教程中复制过,也许有点编辑。在我用paintComponent(Graphics g)绘制之前...这个有用,教程说我可以使用这个方法获得更好的性能,所以我试过,并希望以某种方式管理它。 现在它正在绘制一个运行循环,当我将所有内容复制到paintComponent时,不会绘制任何图像。

对于那些设法通过所有文本的人来做一些额外的工作: 该程序在大学电脑上运行良好 - 不是那么好的硬件^^,而且我的有点迟钝,这应该是计算能力的两倍多。有任何想法吗? :( Java(c)1.7

非常感谢,即使你刚读过这篇文章! :)

1 个答案:

答案 0 :(得分:0)

好的,这可以关闭。我的绘图问题与上面的代码无关。 问题是,我想画的东西不在画面中。我通过发表如下声明来实现:

fillRect(50,50,2/3*frame.getHeight(),130);

2/3解析为0,乘以frameHeight ......

Trick是写(int)(2./3.*frame.getHeight()) 有一次我也尝试在面板部分绘制一个字符串,其中关闭,最大化按钮,当然也不会工作。

无论如何,谢谢了!