Ich habe versucht, dies für immer herauszufinden, aber ich kann es nicht herausfinden.Wie zeige ich mehrere Bilder auf JPanel an?
Ich mag würde mehr als ein Bild auf dem Bildschirm in der gleichen JPanel angezeigt werden, aber aus irgendeinem Grunde, zeigt es nur das letzte Bild aus der Lackkomponente
Ich versuche, ein Obst Ninja-Stil Spiel zu machen und möchte die Früchte aus dem Rahmen zeichnen, bevor Animationen stattfinden.
Hat jemand eine Idee, wie dies zu tun? Jede Hilfe sehr geschätzt ...
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);
}
}
auch in keinem Zusammenhang mit dieser aktuellen Frage würde, da ich versuche, ein FruitNinja
ähnliches Spiel zu machen, Wie mache ich es so, dass der Code registriert, dass meine Maus dort ist (also schneidet es die Frucht, wenn meine Maus über die Frucht schwebt)? ist es MausListenter?
Die einfachste Lösung ist mit einem geeigneten Layout-Manager und 'JLabel's – MadProgrammer
Ich würde vorschlagen, einen Blick auf [Malerei in AWT und Swing] mit (http://www.oracle.com/technetwork/java/ painting-140037.html) um zu verstehen, warum du das Problem hast, das du hast – MadProgrammer
Fertig damit. – Martin