2017-12-29 45 views
0

Java GUI에 이미지 파일 src/happyFace.gif를 표시하는 데 문제가 있습니다. 목표는 창 가장자리에서 튀어 오르는 각도로 프로그램 창을 가로 질러 미끄러지 듯 웃는 얼굴의 이미지를 표시하는 것입니다.Java로 행복한 얼굴 표출하기

ImageIcon 클래스가 미래의 스윙 릴리스와 호환되지 않을 수 있으므로 (오라클 설명서에 따라 : https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html) src/ReboundPanel.java의 이미지 변수 (ImageIcon 유형)에 문제가 있다고 생각합니다. 이것이 사실이라면 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 클래스에서
//******************************************************************** 
// 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

"내 문제는 _라고 생각합니다."그리고 정확히 무엇입니까? [질문]을보고 [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입니다.