Search code examples
javaswingjpanelpaintcomponent

how to paint jpanel every seconds with GradientPaint?


i have two class .. class panel extends JPanel,

and another class that control the paint of that jpanel every seconds.. (i use swing.Timer)

my code below is fail

heres i try so far..

class panel extends JPanel :

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

class Control :

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

i need the start point of GradientPaint change every second

then paint it in jpanel every second

what should i do?

thanks ..


Solution

  • Everything that mKorbel has said and...

    • NEVER maintain a reference to a Graphics context passed to you by the paint sub system. It changes between paint cycles
    • NEVER call repaint or any method that might cause a repaint request from within the paintXxx methods, it will cause the repaint manager to schedule a new repaint at some time in the future and eventually cycle your CPU 100%
    • Painting in AWT and Swing
    • Custom Painting in Swing
    • How to Swing Timer


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