2014-11-15 9 views
0

세로 막대는 애플릿의 높이까지 채워야합니다. 상단에 도달하면 새 막대가 이전 옆에 채우기 시작합니다. 문제 : http://bayimg.com/DAEoeaagm채우기 막대 (진행률 막대) JApplet 페인트() 스레드()

enter image description here

IMG 그것은 어떻게해야 : http://bayimg.com/dAeOgAaGm

enter image description here

새로운 줄은 이전 paint()/막대가 얼마나

IMG를 클리어 충전이 시작되면 코드 :

import java.awt.Color; 
import java.awt.Graphics; 

import javax.swing.JApplet; 


public class fillingbar extends JApplet implements Runnable{ 

    int shifting=0,filling=0; 
    public void init() 
    { 
     Thread t= new Thread(this); 
     t.start(); 
     setSize(400,250); 
    } 

    public void paint(Graphics g) 
    { 
     super.paint(g); 

      g.setColor(Color.GREEN); 
      g.fillRect(shifting,getHeight()-filling,20,filling); 

      g.setColor(Color.BLACK); 
      g.drawRect(shifting, getHeight()-filling, 20, filling); 
    } 

    public void run() 
    { 
     while(true) 
     { 
      repaint(); 
      try{ 
       if(shifting<getWidth()) 
       { 
        if(filling<getHeight()) 
         filling+=10;      
        else { 
         shifting+=20; 
        filling=0; 
        } 
       }  
       Thread.sleep(50); 
      }catch(Exception E){ 
       System.out.println("Exception caught"); 
      } 

     } 
    } 

} 

답변

2
  1. 페인트 방법에 하나의 사각형 만 그리기 때문에 하나만 표시됩니다.
  2. 그 이상을 그릴 필요가있는 경우 for 루프를 사용하여 루프를 작성합니다 (예 : ArrayList<Rectangle>).
  3. 또 다른 방법은 로컬로 이동하고 paintComponent 내부에서 간단한 수학을 수행하여 그릴 내용과 위치를 확인하는 것입니다. 예를 들어 for 루프 안에 for (int i = 0; i < filling/getHeight(); i++) {의 완성 된 막대를 그려 넣고 완료 ​​막대는 filling % getHeight()까지 채 웁니다.
  4. JApplet 내에서 직접 그려서는 안되며 JPanel의 paintComponent 메소드에서 직접 그려서는 안됩니다.
  5. 스윙 타이머는 (적어도 나를 위해) 스레드보다 사용하기가 쉽고 더 안전 할 수 있습니다.

    Animated GIF of the JApplet

    import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Graphics; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.KeyEvent; 
    import java.lang.reflect.InvocationTargetException; 
    
    import javax.swing.*; 
    
    @SuppressWarnings("serial") 
    public class FillingBar2 extends JApplet { 
        @Override 
        public void init() { 
         try { 
         SwingUtilities.invokeAndWait(new Runnable() { 
          public void run() { 
           FillingBarPanel fillingBarPanel = new FillingBarPanel(); 
           add(fillingBarPanel); 
           add(new JButton(new StartAction(fillingBarPanel)), BorderLayout.PAGE_END); 
           setSize(getPreferredSize()); 
          } 
         }); 
         } catch (InvocationTargetException | InterruptedException e) { 
         System.err.println("Big Problems"); 
         e.printStackTrace(); 
         } 
        } 
    } 
    
    @SuppressWarnings("serial") 
    class StartAction extends AbstractAction { 
        private FillingBarPanel fillingBarPanel; 
    
        public StartAction(FillingBarPanel fillingBarPanel) { 
         super("Start"); 
         putValue(MNEMONIC_KEY, KeyEvent.VK_S); 
         this.fillingBarPanel = fillingBarPanel; 
        } 
    
        @Override 
        public void actionPerformed(ActionEvent evt) { 
         fillingBarPanel.start(); 
        } 
    } 
    
    @SuppressWarnings("serial") 
    class FillingBarPanel extends JPanel { 
        private static final int BAR_WIDTH = 20; 
        private static final int TIMER_DELAY = 100; 
        private static final int PREF_W = 400; 
        private static final int PREF_H = 250; 
        private int filling = 0; 
        private Timer timer; 
    
        public FillingBarPanel() { 
         timer = new Timer(TIMER_DELAY, new TimerListener()); 
        } 
    
        public void start() { 
         if (timer != null && !timer.isRunning()) { 
         timer.start(); 
         } 
        } 
    
        @Override 
        protected void paintComponent(Graphics g) { 
         super.paintComponent(g); 
         int shifting = 0; 
         for (int i = 0; i < filling/getHeight(); i++) { 
         shifting = i * BAR_WIDTH; 
         g.setColor(Color.GREEN); 
         g.fillRect(shifting, 0, BAR_WIDTH, getHeight()); 
    
         g.setColor(Color.BLACK); 
         g.drawRect(shifting, 0, BAR_WIDTH, getHeight()); 
         } 
         shifting = BAR_WIDTH * (filling/getHeight()); 
         g.setColor(Color.GREEN); 
         g.fillRect(shifting, getHeight() - (filling % getHeight()), BAR_WIDTH, getHeight()); 
    
         g.setColor(Color.BLACK); 
         g.drawRect(shifting, getHeight() - (filling % getHeight()), BAR_WIDTH, getHeight()); 
        } 
    
        private class TimerListener implements ActionListener { 
         @Override 
         public void actionPerformed(ActionEvent evt) { 
         filling += 10; 
         repaint(); 
         } 
        } 
    
        @Override 
        public Dimension getPreferredSize() { 
         if (isPreferredSizeSet()) { 
         return super.getPreferredSize(); 
         } 
         return new Dimension(PREF_W, PREF_H); 
        } 
    
    } 
    
    :

는 예를 들어, 이는 다음 코드에 의해 생성 될 수있다