Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:53:59+00:00 2026-06-15T14:53:59+00:00

i have two class .. class panel extends JPanel, and another class that control

  • 0

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 ..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:54:00+00:00Added an answer on June 15, 2026 at 2:54 pm

    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;
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have two files. One extends JFrame, and another Extends JPanel. Whenever I change
I have panel with two toolbars . How can I to implement custom class
I have a class that extends JFrame, and it has a BorderLayout. It has
My projects constists of two classes, GoBoard extends JPanel. GoTest.java: import javax.swing.*; import java.awt.Graphics;
I have a class Panel which overrides onDraw() method as below.I have two images
I have a class GridPanel extends JPanel , with a static inner class ToolSelectComboBox
I have two Java class hierarchies that share a common ancestor and implement a
I have two class libraries MyLibrary.dll and MyLibraryEditor.dll for a Unity runtime and editor
I have two class files hudlayer.m and actionlayer.m I have a method named jump
So i have two Class's Class A and Class B . I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.