Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不重新绘制()的情况下用java制作2d图形?_Java_Graphics_2d Games - Fatal编程技术网

如何在不重新绘制()的情况下用java制作2d图形?

如何在不重新绘制()的情况下用java制作2d图形?,java,graphics,2d-games,Java,Graphics,2d Games,我想在不同的时间集中绘制一些矩形,当我使用paint()绘制第一个矩形时,我使用paint()绘制第二个矩形,然后第一个矩形消失。我发现一个图像覆盖了一个previos图像。如果不重新创建以前的内容(例如不使用repaint()),我该怎么办?我很感激每一个答案:d public static class Screen extends Frame { int WIDTH = 900; static int[][] map = new int[700][

我想在不同的时间集中绘制一些矩形,当我使用paint()绘制第一个矩形时,我使用paint()绘制第二个矩形,然后第一个矩形消失。我发现一个图像覆盖了一个previos图像。如果不重新创建以前的内容(例如不使用repaint()),我该怎么办?我很感激每一个答案:d

public static class Screen extends Frame 
    {

        int WIDTH = 900;
        static int[][] map = new int[700][700];
        public Screen() 
        {
            super("Clash of Tank");
            setSize(WIDTH, WIDTH);
            addWindowListener(new WindowAdapter() 
            {
                public void windowClosing(WindowEvent e) 
                {
                    System.exit(0);
                }
            });
        }


        public void paint(Graphics g) {
            BufferedImage image = (BufferedImage) createImage(700,700);
            Graphics g2 = image.getGraphics();
            drawTank(g2);
            g.drawImage(image, 325, 35, this);
        }


        void drawTank(Graphics g) {

            g.setColor(Color.BLACK);
            for(int i = 0; i<=690; i+=1)
                for(int j = 0; j<=690; j+=1)
                    {
                        if(map[i][j]==0)
                            g.setColor(Color.BLACK);
                        if((map[i][j]==1))
                            g.setColor(Color.BLUE);
                        if((map[i][j]==2))
                            g.setColor(Color.RED);
                        if((map[i][j]==3))
                            g.setColor(Color.GREEN);
                        if((map[i][j]==4))
                            g.setColor(Color.YELLOW);
                        g.fillRect(i, j, 1, 1);
                    }



        }

    }


public class ClashofTanks 
{
    public static void main(String[] args) throws Exception 
    {
        Screen t = new Screen(); 
        t.setVisible(true);
        t.repaint();
    }
}   
公共静态类屏幕扩展帧
{
内部宽度=900;
静态int[]map=newint[700][700];
公共屏幕()
{
超级(“坦克碰撞”);
设置大小(宽度、宽度);
addWindowListener(新的WindowAdapter()
{
公共无效窗口关闭(WindowEvent e)
{
系统出口(0);
}
});
}
公共空间涂料(图g){
BuffereImage=(BuffereImage)createImage(700700);
Graphics g2=image.getGraphics();
抽油槽(g2);
g、 drawImage(图,325,35,本);
}
真空吸水池(图g){
g、 设置颜色(颜色为黑色);
对于(inti=0;i你的是一个经典,你要求一个特定的解决方案来解决一个错误的方法来做某事

关键是应该使用
repaint()
,但是应该存储一组矩形,并在
paint
paintComponent
方法中以for循环的形式绘制它们(如果使用Swing),或者将它们绘制到BuffereImage并在绘制方法中绘制。不要忘记也调用super paint或paintComponent方法