如何使用GradientPaint每秒绘制jpanel?

mop*_*opr -3 java swing jpanel paintcomponent

我有两个类..类面板扩展JPanel,

和另一个每秒控制那个jpanel的油漆的类..(我用swing.Timer)

我的代码失败了

继续我到目前为止尝试..

类面板扩展了JPanel:

@Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Control control = new Control(this,g);
        repaint();
    }
Run Code Online (Sandbox Code Playgroud)

班级控制:

public class Control implements ActionListener{

    private int XX=0;
    private int YY=0;

    private Graphics2D g2;
    private JPanel panel;

    Timer tim = new Timer(1000, this);


    public Control(JPanel el,Graphics g) {


        this.g2=(Graphics2D)g.create();
        this.panel=el;
        tim.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        XX++;
        YY++;

    /////////////////////
    //my priority 
        GradientPaint gp = new GradientPaint(XX, YY, Color.BLUE, panel.getWidth(), panel.getHeight(), Color.WHITE);
    //////////////////////

        g2.setPaint(gp);
        g2.fillRect(0, 0, panel.getWidth(), panel.getHeight());
        panel.repaint();
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要每秒更改GradientPaint的起点

然后每秒在jpanel上绘制它

我该怎么办?

谢谢 ..

mKo*_*bel 5

  1. 不要创建任何Object内部paint/ paintComponent(Control control = new Control(this,g);),之前准备这个/这些Object(你可以把元素放到数组里面,paintComponent只能循环到内部),这个想法在运行时创建一堆Object,直到NPE成为

  2. 你的代码设计错误,必须阅读Oracles 2D图形教程,这里有很多例子

  3. 要重写PreferredSizeJPanel

  4. 你可以创建BufferedImage的


Mad*_*mer 5

mKorbel所说的一切......

  • 永远不要保持Graphics对paint子系统传递给你的上下文的引用.它在涂料周期之间变化
  • 永远不会调用repaint或任何可能导致方法中的重绘请求的paintXxx方法,它将导致重绘管理器在将来的某个时间安排新的重绘,并最终使CPU 100%循环
  • 在AWT和Swing中绘画
  • 秋千中的自定义绘画
  • 如何摆动计时器


public class TestPaintTimer {

    public static void main(String[] args) {
        new TestPaintTimer();
    }

    public TestPaintTimer() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new GradientPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class GradientPanel extends JPanel {

        private Color startColor = Color.RED;
        private Color endColor = Color.BLUE;
        private float progress = 0f;
        private float direction = 0.1f;

        public GradientPanel() {
            Timer timer = new Timer(125, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    progress += direction;
                    if (progress > 1f) {
                        direction *= -1;
                        progress = 1f;
                    } else if (progress < 0) {
                        direction *= -1;
                        progress = 0f;
                    }

                    startColor = calculateProgress(Color.RED, Color.BLUE, progress);
                    endColor = calculateProgress(Color.BLUE, Color.RED, progress);

                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            LinearGradientPaint lgp = new LinearGradientPaint(
                            new Point(0, 0),
                            new Point(0, getHeight()),
                            new float[]{0f, 1f},
                            new Color[]{startColor, endColor});
            g2d.setPaint(lgp);
            g2d.fill(new Rectangle(getWidth(), getHeight()));
            g2d.dispose();
        }

        public Color calculateProgress(Color startValue, Color endValue, float fraction) {
            int sRed = startValue.getRed();
            int sGreen = startValue.getGreen();
            int sBlue = startValue.getBlue();
            int sAlpha = startValue.getAlpha();

            int tRed = endValue.getRed();
            int tGreen = endValue.getGreen();
            int tBlue = endValue.getBlue();
            int tAlpha = endValue.getAlpha();

            int red = calculateProgress(sRed, tRed, fraction);
            int green = calculateProgress(sGreen, tGreen, fraction);
            int blue = calculateProgress(sBlue, tBlue, fraction);
            int alpha = calculateProgress(sAlpha, tAlpha, fraction);

            return new Color(red, green, blue, alpha);
        }

        public int calculateProgress(int startValue, int endValue, float fraction) {
            int value = 0;
            int distance = endValue - startValue;
//        value = Math.round((float)distance * fraction);
            value = (int) ((float) distance * fraction);
            value += startValue;

            return value;
        }

    }

}
Run Code Online (Sandbox Code Playgroud)