Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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 获取图像并放入jframe时出现问题_Java_Image - Fatal编程技术网

Java 获取图像并放入jframe时出现问题

Java 获取图像并放入jframe时出现问题,java,image,Java,Image,实际上,我想用java制作一条蛇,现在我必须在我的框架中为蛇的身体添加图像。我寻找解决方案,但没有一个有效,我尝试了jlabel,或者仅仅是一个图像,它做了完全相同的事情。在搜索了一些github页面后,我发现我在其他地方遇到了一个问题,因为我有相同的图像代码 这是我的密码: 私人形象; 下口私家医院; 私人图像图标左嘴; 私人形象; 私人影像主体; 私有图像图标点; 私有整数[]lensnakeX=新整数[750]; 私有整数[]lensnakeY=新整数[750]; 私有整数移动=0; 私有

实际上,我想用java制作一条蛇,现在我必须在我的框架中为蛇的身体添加图像。我寻找解决方案,但没有一个有效,我尝试了jlabel,或者仅仅是一个图像,它做了完全相同的事情。在搜索了一些github页面后,我发现我在其他地方遇到了一个问题,因为我有相同的图像代码

这是我的密码:

私人形象; 下口私家医院; 私人图像图标左嘴; 私人形象; 私人影像主体; 私有图像图标点; 私有整数[]lensnakeX=新整数[750]; 私有整数[]lensnakeY=新整数[750]; 私有整数移动=0; 私有整数长度=3; 私有布尔左=假; private boolean up=false; 私有布尔向下=false; 私有布尔右=假; 私人定时器t; 专用整数延迟=140; 公共内容{ 初始游戏; } 公共游戏{ 挫折背景色。灰色; addKeyListenerthis; setFocusabletrue; setFocusTraversalKeysEnabledfalse; t=新时间显示,此为; t、 开始; } @凌驾 受保护的空心油漆组件g{ 超级油漆组件; 多德拉维格; } 公共空间doDrawing@NotNull图形g{ ifmoves==0{ lensnakeX[2]=50; lensnakeX[1]=75; lensnakx[0]=100; lensnakeY[2]=100; lensnakeY[1]=100; lensnakeY[0]=100; } //头衔 Graphics2D g2d=Graphics2D g; g、 setColorColor.WHITE; g、 24,10851,55; Font f=新FontSerif,Font.PLAIN,60; g2d.setFontf; g2d.setColorColor.BLACK; g2d.drawStringSnake,375,55; //游戏性 g、 setColorColor.BLACK; g、 填写2474851600; //背景 g、 setColorColor.black; g、 第25、75、850、575号图纸; 对于int i=0;i等等,在测试是否有一个图像出现之前,您写了所有这些?你能给我一个主函数的工作示例,这样我们就可以测试它了吗

以下是查看我创建的图像的简单代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestGround extends JPanel {

    private Image img;
    ImageIcon ii ;


    public TestGround(){
        setBackground(Color.BLACK);
        setFocusable(true);

        // load the image in the constructor:
        ImageIcon ii = new ImageIcon("src/resources/picon.png");
        img = ii.getImage();       
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
        doDrawing(g);
        Toolkit.getDefaultToolkit().sync();
    }


    private void doDrawing(Graphics g) {    
         //drawing a grid of lines
         Graphics2D g2d = (Graphics2D) g;
         // draw the image:
         g.drawImage(img, 0, 0, null);

    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        frame.add(new TestGround());
        frame.setTitle("Show Image");
        frame.setSize(800, 600);       
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    }

}

你好,谢谢你的回答。事实上,我已经把代码发给了一个朋友,他测试了它,它运行正常。所以我的代码有一个问题,但我不知道在哪里,因为图像是在调试时发现的,但它们没有出现在框架上也许你有一个旧版本的java工具包?我不知道你朋友有什么你没有的^^