2017-12-29 48 views
0

自分のJava GUIにイメージファイルsrc/happyFace.gifを表示するのに問題があります。目標は、ウィンドウの端から跳ね返って斜めにプログラムウィンドウを滑っているように見える笑顔のイメージを表示することです。Javaでうれしい顔をする

ImageIconクラスが将来のスウィングリリースと互換性がないため、私の問題はsrc/ReboundPanel.javaのイメージ変数(ImageIcon型)にあると思います(オラクルのドキュメントによると:https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html)。これが当てはまる場合、ImageIconクラスはスイングライブラリでサポートできない可能性があります。私はこれについて私のスイングライブラリをチェックする方法を知らない。

のsrc/happyFace.gif

enter image description here

マイ出力ウィンドウ

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

「私の問題はis_だと思う」とは何ですか? [mcve]であなたの質問をし、[編集]してください。 – AxelH

+0

問題をご指定ください。アイコンは表示されていないのですか?またはアニメーション/インタラクションが壊れていますか? – Lino

+0

申し訳ありませんが、このアイコンは表示されていません。現在のウィンドウでスクリーンショットを追加します – beginner

答えて

2

変化溶液のこの種の唯一のテストの目的のために使用されるべきであることをimage = new ImageIcon("happyFace.gif");

enter image description here

image = new ImageIcon("src/happyFace.gif");に注意。 Andrew Thompsonのコメントに記載されているように、画像を保存して読み込む正しい方法は、embedded resourceです。

関連する問題