2014-04-12 2 views
0

나는 어떤 모양을 채우기 위해 구현 한 알고리즘을 가지고 있습니다 ... 그러나 어떤 지연도없이 모양을 즉시 채 웁니다 ... 나는 그것이 홍수 채우기 알고리즘이 어떻게 보이는지 알 수 있도록 애니메이션 유형을 보여주고 싶습니다. 모양이 채워질 때 작동합니다. 다각형을 채우는 애니메이션 효과를위한 스윙 타이머?

public static void floodFill(BufferedImage image, int x,int y, int fillColor) 
    { 
     java.util.ArrayList<Point> examList=new java.util.ArrayList<Point>(); 

     int initialColor=image.getRGB(x,y); 
     examList.add(new Point(x,y)); 

     while (examList.size()>0) 
     { 
     Point p = examList.remove(0); // get and remove the first point in the list 
     if (image.getRGB(p.x,p.y)==initialColor) 
     { 
      x = p.x; y = p.y; 
      image.setRGB(x, y, fillColor); // fill current pixel 

      examList.add(new Point(x-1,y)); 
      examList.add(new Point(x+1,y));   
      examList.add(new Point(x,y-1)); 
      examList.add(new Point(x,y+1));  
     } 
     } 
    } 

가 시작 타이머가 배치되어야한다 : 여기

내 알고리즘?

답변

3

기본적으로 지정된 시간 동안 기다렸다가 업데이트를 수행 할 수있는 방법이 필요합니다.

Swing과 같은 GUI 프레임 워크 내에서 작업 할 때 UI 스레드가 화면을 최신 상태로 유지하지 못하게하기 때문에 단순히 UI 스레드에서 절전 모드로 전환 할 수 없습니다. 마찬가지로 메서드가 존재할 때까지 UI 스레드는 페인트 요청을 처리 할 수 ​​없습니다. 더 상황, 당신이 "처럼"뭔가를 할 수없는

...

다시 반복 호출을 예약 할 수 javax.swing.Timer를 사용
public static void floodFill(final BufferedImage image, int x, int y, final int fillColor) { 
    final java.util.ArrayList<Point> examList = new java.util.ArrayList<Point>(); 

    final int initialColor = image.getRGB(x, y); 
    examList.add(new Point(x, y)); 

    Timer timer = new Timer(40, new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (!examList.isEmpty()) { 
       Point p = examList.remove(0); // get and remove the first point in the list 
       if (image.getRGB(p.x, p.y) == initialColor) { 
        int x = p.x; 
        int y = p.y; 
        image.setRGB(x, y, fillColor); // fill current pixel 

        examList.add(new Point(x - 1, y)); 
        examList.add(new Point(x + 1, y)); 
        examList.add(new Point(x, y - 1)); 
        examList.add(new Point(x, y + 1)); 

       } 
       repaint(); // Assuming your painting the results to the screen 
      } else { 
       ((Timer)e.getSource()).stop(); 
      } 
     } 
    }); 
    timer.start(); 
} 

(이 예에서는, 모든 40 밀리 초), 프로세스 다음을 목록의 요소는,이 효과적으로 자세한 내용

+0

이봐, 당신의 대답은 완벽에 대한 How to use Swing Timers를 참조

지연 루프의 일종으로 역할 :하지만 어떻게 내가 애니메이션 빠른 비트를 실행 할 수 있습니까? 나는 타이머 40의 값을 변경하고 그것을 감소 ​​시켰지만 변화가 없었다 ... 어떤 생각? – user2228135

+1

actionPerformed 메소드에서 수행중인 작업량을 늘리십시오. – MadProgrammer

+0

작업량을 늘리려고했지만 ... 정의한 모양을 채우기 시작했습니다 ... 다소 혼란스러운 느낌이 들었습니다 ... – user2228135