gpt4 book ai didi

java - 如何在 JPanel 上显示多个图像?

转载 作者:行者123 更新时间:2023-11-30 06:48:30 25 4
gpt4 key购买 nike

我一直在努力解决这个问题,但我似乎无法解决。

我想在同一个 JPanel 的屏幕上显示多个图像,但由于某种原因,它只显示来自 paint 组件的最后一个图像

我正在尝试制作水果忍者风格的游戏,并希望在动画发生之前将水果从框架中抽出。

有没有人知道如何做到这一点?任何帮助将不胜感激...

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 吗?

最佳答案

我建议看看 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
}

所以,现在所有的图像都应该在彼此之上绘制

Also unrelated to this current question, since I'm trying to make a FruitNinja game, how do I make it so the code registers that my mouse is there(so it slices the fruit when my mouse hovers over the fruit)? is it mouseListenter?

您可能正在寻找一个MouseMotionListener,看看How to Write a Mouse Listener了解更多详情

关于java - 如何在 JPanel 上显示多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44224377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com