Java图像显示问题

标签 java image swing graphics jpanel

亲爱的 stackoverflow 的优秀人员

我的一群 friend 正在尝试用 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);

最佳答案

嗯,我想说尝试使用 Graphics,这意味着您需要重写 Paint 方法;我建议您将 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);
}

希望这有帮助!

关于Java图像显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167628/

相关文章:

java - Spring 启动1.5.2.RELEASE : Rolling File logging

java - 检查控制台-计算机或程序错误?

javascript - 图像未在 Blade View 中加载

html - 父div的悬停属性不影响子图像?

java - 如果选择了某个 JCheckBox,如何删除它?

java - 使用 Spring3 mvc 时出现 ajax 问题。返回对象列表时出现 406 错误

java - XStream 中的单元素数组错误

image - 我可以创建一个始终以 100% 缩放级别打开的 PDF 吗?

Java Midi Sequencer - 使用播放头进行音符播放

java - 我应该使用什么组件在 Java 中绘制树状图?