我必须画一个笑脸,用图形和计时器让它的左眼眨一下,但它不眨眼

8fsztsew  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(272)

我启动计时器然后打电话 paintRect 重新绘制眼睛并产生眨眼效果,但它不起作用。我打电话来 paintEye 再次画左眼,但用黄色。
我还在努力学习 ActionListener 工作,所以我不确定我是否得到它的权利,这看起来不像无论如何。

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class Content extends JPanel {

    public Content() {
    }

    @Override
    public void paintComponent(Graphics g) {

        //face
        g.setColor(Color.yellow);
        g.fillOval(10, 10, 200, 200);

        //eyes
        g.setColor(Color.black);
        g.fillOval(55, 65, 30, 30);
        g.fillOval(135, 65, 30, 30);

        //mouth
        g.setColor(Color.black);
        g.fillOval(50, 110, 120, 60);

        //touchUp
        g.setColor(Color.YELLOW);
        g.fillRect(50, 110, 120, 30);
        g.fillOval(50, 120, 120, 40);

        time.start();
        paintEye(super.getComponentGraphics(g));

        //eyesAgain
        g.setColor(Color.black);
        g.fillOval(55, 65, 30, 30);
    }

    public void paintEye(Graphics grphcs) {

        grphcs.setColor(Color.yellow);
        grphcs.fillOval(55, 65, 30, 30);
    }

    Timer time = new Timer(100, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            repaint();
        }
    });

}
pdkcd3nj

pdkcd3nj1#

你有问题(如评论中所述)
计时器的actionlistener应该更改示例字段的状态,比如一个名为 blink . 我只需切换它的值: blink = !blink; ,然后打电话 repaint() 您的paintcomponent应该使用这个布尔值来决定要画什么,是睁眼还是闭眼
不要在paintcomponent方法中启动或停止计时器,而是在其他地方(如构造函数中)这样做。绘制方法只用于绘制和绘制,因为您不能完全控制何时调用或是否调用它,而且您实际上只需要调用它 start() 在程序开始时打开计时器一次。
您的paintcomponent方法必须调用其super的方法,首先要清除任何“脏”像素,例如闭眼或睁眼图像:

@Override 
protected void paintComponent(Graphics g) {
    super.paintComponent(g); //*****here

    // .... rest of the code goes below

    if (blink) {
        // ....
    } else {
        // ....
    }
}

e、 一个简单的切换程序

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

import javax.swing.*;

public class Foo01 extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final Color COLOR_1 = Color.RED;
    private static final Color COLOR_2 = Color.BLUE;
    private static final int TIMER_DELAY = 100;
    private static final int RECT_X = 100;
    private static final int RECT_W = 400;
    private boolean blink = false;

    public Foo01() {
        setPreferredSize(new Dimension(PREF_W, PREF_H));
        new Timer(TIMER_DELAY, e -> {
            blink = !blink;
            repaint();
        }).start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (blink) {
            g.setColor(COLOR_1);
        } else {
            g.setColor(COLOR_2);
        }
        g.fillRect(RECT_X, RECT_X, RECT_W, RECT_W);
    }

    private static void createAndShowGui() {
        Foo01 mainPanel = new Foo01();

        JFrame frame = new JFrame("Foo01");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

}

相关问题