2017-12-29 27 views
0

Ich habe Probleme mit der Anzeige meiner Bilddatei src/happyFace.gif in meiner Java-GUI. Ziel ist es, ein Bild eines lächelnden Gesichts zu zeigen, das in einem Winkel über das Programmfenster zu gleiten scheint und von den Fensterrändern abprallt.Ein fröhliches Gesicht zum Anzeigen in Java erhalten

Ich denke, mein Problem ist mit der Image-Variable (Typ ImageIcon) in src/ReboundPanel.java, weil die ImageIcon-Klasse möglicherweise nicht mit zukünftigen Swing-Versionen kompatibel ist (laut Oracle-Dokumentation: https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html). Wenn dies der Fall ist, kann die ImageIcon-Klasse möglicherweise nicht von der Swing-Bibliothek unterstützt werden. Ich weiß nicht, wie ich meine Schaukelbibliothek dafür überprüfe.

src/happyFace.gif

enter image description here

Mein Ausgabefenster

enter image description here

src/Rebound.java:

//******************************************************************** 
// Rebound.java Java Foundations 
//******************************************************************** 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class Rebound{ 
//----------------------------------------------------------------- 
// Displays the main frame of the program. 
//----------------------------------------------------------------- 
    public static void main (String[] args){ 
     JFrame frame = new JFrame ("Rebound"); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new ReboundPanel()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

src/ReboundPanel.java:

//******************************************************************** 
// ReboundPanel.java Java Foundations 
// 
// Represents the primary panel for the Rebound program. 
//******************************************************************** 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
public class ReboundPanel extends JPanel{ 
    private final int WIDTH = 300, HEIGHT = 100; 
    private final int DELAY = 20, IMAGE_SIZE = 35; 
    private ImageIcon image; 
    private Timer timer; 
    private int x, y, moveX, moveY; 
    //----------------------------------------------------------------- 
    // Sets up the panel, including the timer for the animation. 
    //----------------------------------------------------------------- 
    public ReboundPanel(){ 
     timer = new Timer(DELAY, new ReboundListener()); 
     image = new ImageIcon ("happyFace.gif"); 
     x = 0; 
     y = 40; 
     moveX = moveY = 3; 
     setPreferredSize (new Dimension(WIDTH, HEIGHT)); 
     setBackground (Color.black); 
     timer.start(); 
    } 
    //----------------------------------------------------------------- 
    // Draws the image in the current location. 
    //----------------------------------------------------------------- 
    public void paintComponent (Graphics page){ 
     super.paintComponent (page); 
     image.paintIcon (this, page, x, y); 
    } 
    //***************************************************************** 
    // Represents the action listener for the timer. 
    //***************************************************************** 
    private class ReboundListener implements ActionListener{ 
     //----------------------------------------------------------------- 
     // Updates the position of the image and possibly the direction 
     // of movement whenever the timer fires an action event. 
     //----------------------------------------------------------------- 
     public void actionPerformed (ActionEvent event){ 
      x += moveX; 
      y += moveY; 
      if (x <= 0 || x >= WIDTH-IMAGE_SIZE) 
       moveX = moveX * -1; 
      if (y <= 0 || y >= HEIGHT-IMAGE_SIZE) 
       moveY = moveY * -1; 
      repaint(); 
     } 
    } 
} 
+1

"_Ich denke, mein Problem ist_", und was ist es genau? Bitte sehen Sie [fragen] und bearbeiten Sie Ihre Frage mit [mcve]. – AxelH

+0

können Sie bitte Ihr Problem angeben. Wird das Icon nicht angezeigt oder ist etwas mit der Animation/Interaktion kaputt? – Lino

+0

oh sorry, das Symbol wird nicht einmal angezeigt. Ich werde einen Screenshot mit meinem aktuellen Fenster hinzufügen – beginner

Antwort

2

In ReboundPanel Klasse,

Änderung image = new ImageIcon("happyFace.gif");

zu image = new ImageIcon("src/happyFace.gif");

enter image description here

Beachten Sie, dass diese Art der Lösung sollte nur zu Testzwecken verwendet werden. Wie in Andrew Thompson Kommentar erwähnt, ist die richtige Methode zum Speichern und Laden des Bildes mit einem embedded resource.