我一直试图弄清楚这一点,但我似乎无法弄清楚。如何在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吗?
最简单的解决方案是使用一个合适的布局管理器和'JLabel's – MadProgrammer
我建议具有[在AWT和Swing绘画](http://www.oracle.com/technetwork/java/看看painting-140037.html)来理解你为什么遇到你遇到的问题 – MadProgrammer
完成这件事。 – Martin