填充栏(进度条)JApplet paint()Thread()

时间:2014-11-15 14:01:50

标签: java swing applet paint japplet

垂直条应填充到小程序的高度。到达顶部时,新栏应该开始填充前一个栏。问题:当新栏开始填充时,前一个paint() /栏被清除

img如何:http://bayimg.com/DAEoeaagm

enter image description here

img应该如何:http://bayimg.com/dAeOgAaGm

enter image description here

代码:

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");
            }

        }
    }

}

1 个答案:

答案 0 :(得分:2)

  1. 你只在paint方法中绘制一个矩形,因此只有一个会显示。
  2. 如果您需要绘制更多内容,请使用循环遍历矩形ArrayList<Rectangle>的for循环。
  3. 另一种方法是移动局部并在paintComponent中做一些简单的数学运算,以查看要绘制的内容和位置。例如,在for循环中绘制已完成的条形for (int i = 0; i < filling / getHeight(); i++) {,以及尚未完成的条形图,直至filling % getHeight()
  4. 您不应直接在JApplet中绘制,而应在JPanel的paintComponent方法中绘制。
  5. Swing Timer比线程更容易使用(至少对我而言),并且可以更安全。

  6. 例如,可以通过以下代码创建:

    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);
       }
    
    }