1

在计时器的每个刻度之后,填充矩形 (Color.ORANGE) 直到整个矩形都被着色,然后再次将矩形运行为空。“停止”按钮,将停止绘画。但是当代码慢慢地使矩形变白时,矩形是完全白色的。

如何在橙色矩形上绘画?

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

public class MyFrame extends JFrame implements ActionListener
{ 
    private JPanel beerPannel;
    private JButton startBtn;
    private JButton stopBtn;
    private Timer beerTimer;
    private int timeCountUp = 0;
    private int timeCountDown = 22;


    public MyFrame() {
        setLayout(new FlowLayout()); 

        // create textPlane
        beerPannel = new JPanel();
        beerPannel.setPreferredSize(new Dimension(250, 300)); 
        beerPannel.setBackground(Color.WHITE);

        // create startBtn
        startBtn = new JButton("Start");
        startBtn.addActionListener(this);

        // create stopBtn
        stopBtn = new JButton("Stop");
        stopBtn.addActionListener(this);

        // make timer
        beerTimer = new Timer(1000, this);

        // add to the screen
        add(beerPannel); add(startBtn); add(stopBtn);

        // config pop-up window 
        setSize(300, 380); 
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }

    public void actionPerformed(ActionEvent event) {

        // start with a empty glass of beer
        Graphics beerGlass = beerPannel.getGraphics(); // set beerGlass ass writePanel
        // x -> y -> width -> height
        beerGlass.drawRect(50, 50, 150, 220); 

        // onClick startBtn
        if(event.getSource() == startBtn) {
            // start Timer
            beerTimer.start();
        }
        // onClick stopBtn          
        if(event.getSource() == stopBtn) {
            // stop Timer   
            beerTimer.stop();   
        }
        // als beer timer is gestart
        if(event.getSource() == beerTimer) {
            // count timeClicks
            int telUp = timeCountUp++;

            // standardt vars
            int width = 148;
            int x = 51;

            // start with a empty glass of beer

            // set color
            Graphics fillBeerGlass = beerPannel.getGraphics();

            if(telUp <= 22) { 
                // set height
                int height = 10 * telUp - 2;
                // set y
                int y = 259 - height + 10;      

                fillBeerGlass.setColor(Color.ORANGE); 
                fillBeerGlass.drawRect(x, y, width, height); 
                fillBeerGlass.fillRect(x, y, width, height); 

            } else {
                // count timeClicks
                int telDown = timeCountDown--;
                // verklein inhoud
                // set height
                int height = 10 * telDown - 2;
                // set y
                int y = 259 - height + 10;      

                // set color
                fillBeerGlass.setColor(Color.WHITE); 
                fillBeerGlass.drawRect(x, y, width, height); 
                fillBeerGlass.fillRect(x, y, width, height); 


                // if count 0, stop timer 
                if(telDown == 0) {
                    beerTimer.stop();
                }
            }
        }
    }
}
4

1 回答 1

1

正如 mKorbel 在评论中提到的,这不是尝试在 Swing 中绘画的正确方法。相反,您需要创建一个具有状态并重新绘制自身的组件。然后有一个Timer改变状态(例如从 50% 满到 55% 满),并强制重绘。计时器可以使用相同的方法上下移动状态。

于 2013-10-28T14:09:31.427 回答