我一直試圖弄清楚這一點,但我似乎無法弄清楚。如何在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