[자바] 간단한 프로그램인데...

시렌의 이미지

import javax.swing.*;
import java.awt.*;

public class SimpleAnimation 
{
	int x = 70;
	int y = 70;

	public static void main(String[] args)
	{
		SimpleAnimation gui = new SimpleAnimation();
		gui.go();
	}

	public void go()
	{
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		MyDrawPanel drawPanel = new MyDrawPanel();

		frame.getContentPane().add(drawPanel);
		frame.setSize(300, 300);
		frame.setVisible(true);

		for (int i = 0; i < 130; i++) 
		{
			x++;
			y++;

			drawPanel.repaint();

			try
			{
				Thread.sleep(50);
			}
			catch(Exception ex)
			{
			}
		}

		class MyDrawPanel extends JPanel
		{
			public void paintComponent(Graphics g)
			{
				g.setColor(Color.white);
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
				g.setColor(Color.green);
				g.fillOval(x, y, 40, 40);
			}
		    
		}
    }
};

간단하게 애니메이션 비슷하게 만들어볼려고 내부클래스를 써서 했는데 컴파일 에러가 나네요.

MyDrawPanel drawPanel = new MyDrawPanel();
에서 에러가 나는데...뭐가 틀린거죠? 도통 모르겠네요;;

fox9의 이미지

MyDrawPanel 내부클래스 정의가 사용되는 곳보다 밑에 있어서 그런듯 한데 MyDrawPanel 클래스 정의 부분은 맨 위로 올려보세요 8)

시렌의 이미지

이제 되네요-_-

책에는 저렇게해도 된다는 식으로 나와있어서 저렇게 했더만;;

답변감사합니다.

pore70의 이미지

이 프로그램의 문제는 전역/지역 변수 그리고 범위 개념을 알고 있는 가의 문제입니다. 단순히 작동하게 하기위해 수정하기 보다는
개념을 이해하시기 바랍니다