Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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图像显示问题_Java_Image_Swing_Graphics_Jpanel - Fatal编程技术网

Java图像显示问题

Java图像显示问题,java,image,swing,graphics,jpanel,Java,Image,Swing,Graphics,Jpanel,亲爱的世界上的人们 我的一群朋友正在尝试用Java制作一个级别编辑器 我们有一个Jpanel而不是Jframe,我们正试图将小图像从保存为字符串的文件路径放到Jpanel上。最后,我们需要一个图像列表,您可以直接访问。到目前为止,我们已经尝试了一些方法,但没有成功 我们可以加载图像,但是我们不能让这些图像实际显示,解决上述问题的最佳方法是什么 下面是我们目前所拥有的一个样本 EnemyPlacementGrid = new JPanel(); EnemyPlacementGrid.addMous

亲爱的世界上的人们

我的一群朋友正在尝试用Java制作一个级别编辑器

我们有一个Jpanel而不是Jframe,我们正试图将小图像从保存为字符串的文件路径放到Jpanel上。最后,我们需要一个图像列表,您可以直接访问。到目前为止,我们已经尝试了一些方法,但没有成功

我们可以加载图像,但是我们不能让这些图像实际显示,解决上述问题的最佳方法是什么

下面是我们目前所拥有的一个样本

EnemyPlacementGrid = new JPanel();
EnemyPlacementGrid.addMouseListener(new MouseAdapter() {
    //@Override
    public int mouseX;
    public int mouseY;
    public void mouseClicked(MouseEvent arg0) { //what happens when you click in the EnemyPlacementGrid
        System.out.println("Correct Area for placement");
        mouseX = arg0.getX();
        mouseY = arg0.getY();
        //System.out.println("X:" + mouseX + ", Y:" + mouseY );
        Enemy newEnemy = workingEnemy.cloneSelf();
        newEnemy.setLocation(mouseX, mouseY);
        System.out.println("newEnemy object: " + newEnemy);
        System.out.println(newEnemy.weaponList);
        currentWave.addEnemy(newEnemy);
        System.out.print(currentLevel);
    }
});
非常感谢您的任何帮助

更新:

到目前为止,我有一个图像出现,但我不能更新说的图像。注:代码如下:

public void run() {
                try {
                     BufferedImage img = ImageIO.read(new File(IMG_PATH));
                     ImageIcon icon = new ImageIcon(img);
                     WaveScreen frame = new WaveScreen();

                     JPanel panel = (JPanel)frame.getContentPane();  
                     JLabel label = new JLabel();  
                     label.setIcon(new ImageIcon("images/map_on.png"));// your image here  
                     panel.add(label);  


                    frame.setVisible(true);
                    panel.add(label);
                    panel.repaint(); 

                } catch (Exception e) {
                    e.printStackTrace();
                } 
更新,从注释中尝试的方法:

Graphics2D g = null;
                        Graphics2D g2 = (Graphics2D)g;
                        Image imageVariable = new ImageIcon("images/map_on.png").getImage();
                       g.drawImage(imageVariable, mouseX, mouseY, null);

嗯,我想说的是尝试使用图形,这意味着您需要覆盖绘制方法;我建议您将mouseX和mouseY作为全局变量

// creating global image variable for use later
Image imageVariable = new ImageIcon("image path").getImage();

public void paintComponent(Graphics g) {
   // here you could either create a Graphics2D object
   // Graphics2D g2 = (Graphics2D)g;
   // or you could use the g parameter as it is, doesn't matter.
   // use the global variable for the image to be drawn onto the screen
   // use the global value of the mouseX and mouseY for where you click the mouse
   // to place the image, and this should be it 
   g.drawImage(imageVariable, mouseX, mouseY, null);
}

希望这有帮助

嗯,我想说的是尝试使用图形,这意味着您需要覆盖绘制方法;我建议您将mouseX和mouseY作为全局变量

// creating global image variable for use later
Image imageVariable = new ImageIcon("image path").getImage();

public void paintComponent(Graphics g) {
   // here you could either create a Graphics2D object
   // Graphics2D g2 = (Graphics2D)g;
   // or you could use the g parameter as it is, doesn't matter.
   // use the global variable for the image to be drawn onto the screen
   // use the global value of the mouseX and mouseY for where you click the mouse
   // to place the image, and this should be it 
   g.drawImage(imageVariable, mouseX, mouseY, null);
}

希望这有帮助

如果游戏很简单,user2277872的解决方案就可以了,您可以使用java的graphics2D。但是,如果您计划开发一款更复杂的游戏—大量的交互、大量的纹理,那么用于2D图形的默认Java框架将被证明太慢

如果你打算玩这样的游戏,我强烈建议你要么学习OpenGL,要么使用现有的图形框架,比如

JMonkeyEngine 或 光滑的


更多信息:

如果游戏很简单,user2277872的解决方案可以使用,您可以使用java的graphics2D。但是,如果您计划开发一款更复杂的游戏—大量的交互、大量的纹理,那么用于2D图形的默认Java框架将被证明太慢

如果你打算玩这样的游戏,我强烈建议你要么学习OpenGL,要么使用现有的图形框架,比如

JMonkeyEngine 或 光滑的


更多信息:

显示图像的代码在哪里?我在任何地方都看不到。希望这可能会有所帮助,特别是最后一个链接,对于这种情况:-源代码中的一行空白总是足够的。{或}之前的空行通常也是多余的。显示图像的代码在哪里?我在任何地方都看不到。希望这可能会有所帮助,特别是最后一个链接,对于这种情况:-源代码中的一行空白总是足够的。{或}之前的空行通常也是多余的。每次运行paint方法时是否获取图像?更明智的做法是加载一次图像,并仅在绘制期间绘制。是的,你是正确的。我完全没有考虑这个问题。谢谢你,威尔update@bas@user2277872 paintComponent不是更理想吗?我想可能是的,但我一直使用paint而不是paintComponent。覆盖顶级组件的paint是不明智的,因为它不是双缓冲的,您还必须与框架装饰抗衡。除非您有明显的理由不这样做,否则我强烈建议您始终使用paintComponent。它将允许您在需要时混合组件和自定义绘制-每次运行绘制方法时如何设置图像?更明智的做法是加载一次图像,并仅在绘制期间绘制。是的,你是正确的。我完全没有考虑这个问题。谢谢你,威尔update@bas@user2277872 paintComponent不是更理想吗?我想可能是的,但我一直使用paint而不是paintComponent。覆盖顶级组件的paint是不明智的,因为它不是双缓冲的,您还必须与框架装饰抗衡。除非您有明显的理由不这样做,否则我强烈建议您始终使用paintComponent。它将允许您在需要时混合组件和自定义绘画-IMHO