Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 填充条(进度条)JApplet paint()螺纹()_Java_Swing_Applet_Paint_Japplet - Fatal编程技术网

Java 填充条(进度条)JApplet paint()螺纹()

Java 填充条(进度条)JApplet paint()螺纹(),java,swing,applet,paint,japplet,Java,Swing,Applet,Paint,Japplet,垂直条应填充到小程序的高度。当到达顶部时,一个新的条应开始填充上一个条的旁边。问题:当新条开始填充时,先前的paint()/bar被清除 img现状: img应如何: 守则: import java.awt.Color; import java.awt.Graphics; import javax.swing.JApplet; public class fillingbar extends JApplet implements Runnable{ int shifting=0

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

img现状:

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

        }
    }

}
导入java.awt.Color;
导入java.awt.Graphics;
导入javax.swing.JApplet;
公共类fillingbar扩展了JApplet实现Runnable{
整数移位=0,填充=0;
公共void init()
{
螺纹t=新螺纹(此);
t、 start();
设置大小(400250);
}
公共空间涂料(图g)
{
超级油漆(g);
g、 setColor(Color.GREEN);
g、 fillRect(移位,getHeight()-填充,20,填充);
g、 设置颜色(颜色为黑色);
g、 drawRect(移位,getHeight()-填充,20,填充);
}
公开募捐
{
while(true)
{
重新油漆();
试一试{
如果(换档
  • 在绘制方法中只绘制一个矩形,因此只有一个矩形会显示是有意义的
  • 如果需要绘制更多,请使用for循环,该循环可能会在矩形
    ArrayList
    中循环
  • 另一种方法是将移位设置为局部,并在paintComponent内部进行一些简单的数学运算,以查看要绘制的内容和位置。例如,在For循环中绘制已完成的条形图,
    For(int i=0;i
    ,以及尚未完成的条形图,直到
    filling%getHeight()
  • 您不应该直接在JApplet中绘制,而应该使用JPanel的paintComponent方法
  • Swing定时器比线程更容易使用(至少对我来说),而且更安全

  • 例如,这可以通过以下代码创建:

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

    导入java.awt.BorderLayout;
    导入java.awt.Color;
    导入java.awt.Dimension;
    导入java.awt.Graphics;
    导入java.awt.event.ActionEvent;
    导入java.awt.event.ActionListener;
    导入java.awt.event.KeyEvent;
    导入java.lang.reflect.InvocationTargetException;
    导入javax.swing.*;
    @抑制警告(“串行”)
    公共类FillingBar2扩展了JApplet{
    @凌驾
    公共void init(){
    试一试{
    SwingUtilities.invokeAndWait(新的Runnable(){
    公开募捐{
    FillingBarPanel FillingBarPanel=新的FillingBarPanel();
    添加(填充条形面板);
    添加(新JButton(新StartAction(fillingBarPanel)),BorderLayout.PAGE_END);
    设置大小(getPreferredSize());
    }
    });
    }catch(InvocationTargetException | InterruptedException e){
    System.err.println(“大问题”);
    e、 printStackTrace();
    }
    }
    }
    @抑制警告(“串行”)
    类StartAction扩展了AbstractAction{
    私人填充条面板填充条面板;
    公共启动按钮(填充条面板填充条面板){
    超级(“启动”);
    putValue(助记符键,KeyEvent.VK_S);
    this.fillingBarPanel=fillingBarPanel;
    }
    @凌驾
    已执行的公共无效操作(操作事件evt){
    fillingBarPanel.start();
    }
    }
    @抑制警告(“串行”)
    类FillingBarPanel扩展了JPanel{
    专用静态最终整流罩宽度=20;
    专用静态最终int定时器_延迟=100;
    专用静态最终整型预加值W=400;
    专用静态最终int PREF_H=250;
    私有整数填充=0;
    私人定时器;
    公共填充栏面板(){
    定时器=新定时器(定时器延迟,新定时器延迟();
    }
    公开作废开始(){
    if(timer!=null&&!timer.isRunning()){
    timer.start();
    }
    }
    @凌驾
    受保护组件(图形g){
    超级组件(g);
    int移位=0;
    对于(int i=0;i