2016-10-24 3 views
-2

그래서 클래스를 만들려고하는 프로그램이있어서 농장과 태양이있는 국가 장면을 만들어야합니다. 태양은 위아래로 튀어 나와야합니다. 내가 직면하고있는 현재의 문제는 태양이 계속 내려 가고, 튀어 오르지 않을 것이라는 것입니다. 여기 내 코드는 다음과 같습니다.Dr.Java를 사용하여 바운스하는 방법을 알고 싶습니다.

import javax.swing.*; 
import java.awt.*; 
/** 
* Date: Oct 14, 2016 
* Author: 
* Description: Shows a country side with a farm and a sun bouncing up and down. 
*/ 
public class WeAreInThePictures extends JFrame 
{ 
    ImageIcon sun, farm, bG; //assigns sun, farm and background to an image variable 
    static int x = 0, y = -50; //starting position of the sun 
    static int ySpeed = 10; //speed in y direction 
    static double delay = 1.0; 


    public WeAreInThePictures() { 
    super ("We Are In The Pictures!"); 
    setSize (852, 480); 
    bG = new ImageIcon ("1.jpg"); 
    sun = new ImageIcon ("sun.png"); 
    farm = new ImageIcon ("farm.png"); 
setVisible (true); 
    } 

    public static void main(String[] args) { 
    new WeAreInThePictures(); 
    } 

    public void paint (Graphics g) 
    { 
    for (int i = 0; i < 1000; i++) 
{ 
    bG.paintIcon (this, g, 0, 0); 
    farm.paintIcon (this, g, 500, 50); 

    y = y + ySpeed; 
    if (ySpeed > 0) 
    { 
    sun.paintIcon (this, g, x, y); 

    for (int j = 0; j < 550000; j++) 
    { 
     delay = Math.pow (delay, 1); 
    } 
    } 
    else if (y > 50) 
    { 
    ySpeed = ySpeed - 1; 
    } 
    else if (y <= 0) 
    { 
    ySpeed = ySpeed - 1; 
    } 
    } 
} 
} 

누군가가 나에게 무엇이 잘못 되었습니까, 어떻게 수정해야하며 왜 문제가 발생하는지 설명 할 수 있습니까?

답변

0

귀하의 문제는 스윙 그래픽 튜토리얼 또는이 사이트에서 스윙 애니메이션 (이유)과 관련하여 비슷한 질문을하지 않고이 코드를 작성한 것으로 보입니다. JFrame에서 직접 그리는 중입니다. 일을해서는 안되는 것, 페인트 메서드 내에서 객체 변이 코드가 있습니다. 다시는 수행해서는 안되는 작업입니다. 당신이 튜토리얼에서 권장

대신 할, 대부분이이 사이트에 다른 비슷한 질문, 사실, 요청하기 전에 문제 검색 : 인 JPanel의의의 paintComponent 방법에

  • 그리기 JFrame에 표시됩니다.
  • 애니메이션 루프에 스윙 타이머 사용
  • paintComponent 메서드가 아닌 타이머의 ActionListener에서 속도의 방향을 변경하십시오.
관련 문제