2017-05-28 45 views
2

我一直试图弄清楚这一点,但我似乎无法弄清楚。如何在JPanel上显示多个图像?

我想显示在相同的JPanel,但由于某种原因,在屏幕上多个图像,只显示来自涂料成分

我试图做一个水果忍者风格的游戏,最后的图像并希望在动画发生之前从框架中取得成果。

有谁有如何做到这一点?任何帮助,将不胜感激的想法...

import javax.swing.*;//imports JPanel class 
import java.awt.*;//imports the Graphics class 

public class FruitNinja extends JPanel { 

    private Image dojo; 
    private Image apple; 
    private Image orange; 
    private Image pineapple; 
    private Image strawberry; 
    private Image banana; 

    private Timer timer; 
    private int x, y; 

    public FruitNinja() { // a constructor to set up graphics windo 
     super(); 
     setBackground(Color.WHITE); 
     loadImage(); 
     x = 25; 
     y = 25; 


    } 



    private void loadImage() { 
     ImageIcon ii = new ImageIcon("Dojo.jpg"); 
     dojo = ii.getImage(); 

     ImageIcon oo = new ImageIcon("Orange.ico"); 
     orange = oo.getImage(); 

     ImageIcon ss = new ImageIcon("Strawberry.png"); 
     strawberry = ss.getImage(); 

     ImageIcon bb = new ImageIcon("Banana.png"); 
     banana = bb.getImage(); 

     ImageIcon pp = new ImageIcon("Pineapple.png"); 
     pineapple = pp.getImage(); 

     ImageIcon aa = new ImageIcon("Apple.png"); 
     apple = aa.getImage(); 


    } 

    public void paintComponent(Graphics g){ // draw graphics in the panel 

     super.paintComponent(g);// to make panel display correctly 
     g.drawImage(dojo, 0,0, this); 
     //draws out dojo 


     super.paintComponent(g);// to make panel display correctly 
     g.drawImage(apple, 0,0, this); 

     super.paintComponent(g);// to make panel display correctly 
     g.drawImage(orange, 0,0, this); 

     super.paintComponent(g);// to make panel display correctly 
     g.drawImage(pineapple, 0,0, this); 

     super.paintComponent(g);// to make panel display correctly 
     g.drawImage(banana, 0,0, this); 

     super.paintComponent(g);// to make panel display correctly 
     g.drawImage(strawberry, 0,0, this); 

     //draws out the fruits somewhere 
    } 

    /* 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     x += 5; 
     y += 5; 

     if (y > getHeight()) { 
     y = 25; 
     x = 25; 
     } 
     repaint(); 
    } 
*/ 

    public static void main(String[] args) { 

     FruitNinja panel = new FruitNinja(); // window for drawing 
     JFrame f = new JFrame(); // the program itself 
     f.setTitle("Fruit Ninja"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//when the X button is clicked, the program quits 
     f.setSize(1280,800);//size of the frame 
     Container pane = f.getContentPane();//pane refers to the interior of the JFrame 

     FruitNinja p1 = new FruitNinja(); 

     pane.add(p1);//add the FacePanel object to the interior of the frame 
     f.setVisible(true); 

    } 
} 

也无关,这个当前问题,因为我想做一个像FruitNinja游戏,我该如何做到这一点,因此代码注册了我的鼠标在那里(所以当我的鼠标悬停在水果上时,它会切割水果)?它是mouseListenter吗?

+0

最简单的解决方案是使用一个合适的布局管理器和'JLabel's – MadProgrammer

+0

我建议具有[在AWT和Swing绘画](http://www.oracle.com/technetwork/java/看看painting-140037.html)来理解你为什么遇到你遇到的问题 – MadProgrammer

+0

完成这件事。 – Martin

回答

3

我建议在看看Painting in AWT and Swing理解为什么你有你有

一说paintComponent做它准备Graphics上下文部件的涂装作业的问题,它通常做这与上述成分的背景颜色填充的

所以根据你的代码,那就表明你只需要调用paintComponent一次,在方法的开始

public void paintComponent(Graphics g){ // draw graphics in the panel 

    super.paintComponent(g);// to make panel display correctly 
    g.drawImage(dojo, 0,0, this); 
    //draws out dojo 
    g.drawImage(apple, 0,0, this); 
    g.drawImage(orange, 0,0, this); 
    g.drawImage(pineapple, 0,0, this); 
    g.drawImage(banana, 0,0, this); 
    g.drawImage(strawberry, 0,0, this); 
    //draws out the fruits somewhere 
} 

所以,现在所有的图片应涂上的相互

也无关,这个当前问题上面,因为我想做一个水果忍者游戏,我怎么让它所以代码寄存器我的鼠标在那里(所以当我的鼠标悬停在水果上时,它会切割水果)?它是mouseListenter吗?

你可能寻找一个MouseMotionListener,看看How to Write a Mouse Listener更多细节

+0

好的,谢谢你的帮助!我非常感谢您提供的信息以及额外的信息,以帮助像我这样的新程序员。再次感谢您的快速回复。 – Martin

1

您已经扩展JPanel并在JPanel你在同xy,这是0已制定。并且在重载的paintComponent()方法中,您正在调用super类方法的次数。正因为如此,你只能看到最后绘制的图像。

更改此:

super.paintComponent(g); 
    g.drawImage(apple, 0,0, this); 

    super.paintComponent(g); 
    g.drawImage(orange, 0,0, this); 

    super.paintComponent(g); 
    g.drawImage(pineapple, 0,0, this); 

    super.paintComponent(g); 
    g.drawImage(banana, 0,0, this); 

    super.paintComponent(g); 
    g.drawImage(strawberry, 0,0, this); 

要:

super.paintComponent(g); 

    g.drawImage(apple, 0,0, this); 
    g.drawImage(orange, 0,0, this); 
    g.drawImage(pineapple, 0,0, this); 
    g.drawImage(banana, 0,0, this); 
    g.drawImage(strawberry, 0,0, this); 

不过你看不到其他图像,因为所有的图像都在彼此的顶部。为了避免这种情况,你可以做这样的事情:

super.paintComponent(g); 

    g.drawImage(dojo, x,0, this); 
    g.drawImage(apple, x,100, this); 
    g.drawImage(orange, 300,100, this); 
    g.drawImage(pineapple, x,300, this); 
    g.drawImage(banana, 300,0, this); 
    g.drawImage(strawberry, 300,300, this); 

您可以更改xy cordinate只要你想。但这仍然是令人困惑和恼人的,也是很耗时的。那么,有一个更好的方法来做到这一点。那是Layout Manager

想一想在您的程序中使用Layout Manager的方法。这些链接可能会对您有所帮助:

在你的问题的最后一部分,你提到的有关

如何让我这么码寄存器,我的鼠标是有

你需要得到指针的当前位置(xy)。

是不是mouseListenter?

不,它不是,除非你没有很多事件。 mouseListenter用于组件上的鼠标事件(按下,释放,单击,输入和退出)。

根据上述说明,您需要的是MouseMotionListener。它用于组件上的鼠标移动事件。

UPDATE(按评论):

我真的不知道如何使用的MouseMotionListener?我已经能够 动画的水果/图像,所以它现在围绕我的框架飞行。 我如何使用MouseMotionListener将该水果移出框架 我将鼠标悬停在框架上?

这是出了问题的范围,但作为一个帮助,

嘛,这个你必须将所有图像添加到JLabelš否则,你可以添加监听到面板上。由于有多个图像,您必须创建标签。要做到这一点,我已经创造了新的方法,代码应该是这样的:

public void moveImage(ImageIcon icon){ 
    JLabel dojoLabel = new JLabel(); 
    dojoLabel.setIcon(icon); 
    this.add(dojoLabel); 

    dojoLabel.addMouseMotionListener(new MouseMotionAdapter() { 
     public void mouseMoved(MouseEvent evt){ 
      dojoLabel.setLocation(0,0); 
      repaint(); 
     } 
    }); 
} 
loadImage()调用内部

现在这种方法解析ImageIcon。不能将Image类型添加到JLabel。这就是为什么你需要添加ImageIcon

private void loadImage() { 
     ImageIcon ii = new ImageIcon("Dojo.jpg"); 
     dojo = ii.getImage(); 
     //..... 
     //move method here 
     moveImage(ii); 
} 
+1

好的,谢谢你的详细回复。谢天谢地,我能够通过只打一次超级电话并在不同地区移动我的水果来解决我遇到的问题?我不确定你是如何使用MouseMotionListener的?我已经能够对水果/图像进行动画处理,因此它现在正在我的框架周围飞舞。一旦我将鼠标悬停在框架上,如何使用MouseMotionListener将该水果移出框架?我在网上搜索了MouseMotion Listener,并有一个教程,但我仍然不确定。 – Martin

+0

@Martin这个问题可能会有所帮助:https://stackoverflow.com/questions/17867537/java-drawimage-how-do-i-undraw-or-delete-a-image – Blasanka

+0

是的,我不太清楚如何使用MouseMotionListener?想想你与我联系的是删除图像的事情。 – Martin