Java图形,使窗口以不同的颜色闪烁



这是我在这里的第一篇文章。我目前正在学习java中的基本GUI和Graphics。我对这门语言还很陌生,这也是我的第一门语言。当我学习java时,我喜欢试验和玩我从阅读中获得的新工具。因此,我想制作一个窗口,里面填充的颜色会闪烁,目前,我想保持简单,让它从橙色闪烁到青色,然后再闪烁回来。然而,现在,我的程序所做的只是从白色开始,用青色和文本填充,然后停止任何更改,我不确定为什么会这样。

请给我指正确的方向,谢谢!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ColorFlash extends JFrame{
    private static class Display extends JPanel{ 
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            for(int h = 200; h > 25; h--){
                g.setColor(Color.ORANGE);
                g.fillRect(0, 0, 500, 500);
                g.setColor(Color.CYAN);
                g.fillRect(0, 0, 500, 500);
                g.setColor(Color.ORANGE);
                g.drawString("Hello world!", 30, 35);
                g.drawOval(100, 100, 60, 60);
            }
        }
    }
    private static class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    }
    public static void main (String [] args){
        Display displayPanel = new Display();
        JButton okButton = new JButton("OK");
        ButtonHandler listener = new ButtonHandler();
        okButton.addActionListener(listener);
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(displayPanel, BorderLayout.CENTER);
        //content.add(okButton, BorderLayout.SOUTH);
        //subContent.add(displayPanel, BorderLayout.SOUTH);
        //content.add(okButton, BorderLayout.SOUTH);
        content.add(okButton, BorderLayout.SOUTH);
        JFrame window = new JFrame("GUI Test");
        window.setContentPane(content);
        window.setSize(500, 500);
        window.setLocation(100,100);
        window.setVisible(true);
    }
}

从AWT和Swing 中的自定义绘画和绘画开始

基本上,正在发生的是,循环中最后绘制的内容就是实际绘制到屏幕上的内容

您需要的是某种计时器/触发器,它可以更改要绘制的颜色并重新绘制组件。

查看如何使用摆动定时器了解更多详细信息

例如。。。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestPaint {
    private static class Display extends JPanel {
        private Color[] colors = new Color[]{Color.ORANGE, Color.CYAN};
        private int colorIndex;
        public Display() {
            Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setBackground(colors[colorIndex % 2]);
                    colorIndex++;
                }
            });
            timer.start();
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.ORANGE);
            g.drawString("Hello world!", 30, 35);
            g.drawOval(100, 100, 60, 60);
        }
    }
    public static void main(String[] args) {
        new TestPaint();
    }
    public TestPaint() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Display());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

您可能还想看看Initial Threads

最新更新