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

  • SEARCH
  • Home
  • 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 9006253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:21:40+00:00 2026-06-16T01:21:40+00:00

I asked a question last week about Gui and Timers… well after lots of

  • 0

I asked a question last week about Gui and Timers… well after lots of research i found nothing that would work with my code… If anyone could take my code and somehow manipulate it to work that would be amazing… I want my code to display a box and then making it overlap with a box with the same color as the back ground and then a new box being displayed right beside it with a delay

first code:

import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;

public class MovingSomething extends JPanel
{
    public void paintComponent (final Graphics g)
    {     
        int i = 20;
        int cons = 50;
        int red = 40;
        int green = 50;

        g.setColor(Color.GREEN);
        g.fillRect(50, 50, 100, 100);

        while (i >= 0)
        {
            i -= 1;

            g.setColor(Color.RED);
            g.fillRect(red, cons, 100, 100);
            red += 10;

            g.setColor(Color.GREEN);
            g.fillRect(green, cons, 100, 100);
            green += 10;
        }
    }
}
  • 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-16T01:21:41+00:00Added an answer on June 16, 2026 at 1:21 am

    Just for your little help to start with, I am posting this code. Though Please do try to learn from it and ask valid questions, that might will arise, to get a taste of the whole thingy. If you had searched it a little bit harder, no doubt, you could have reached this wonderful Doc, that explains exactly your needs in this example.

    Code to programatically repaint the component whenever the user clicks or drags the mouse

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MovingSquare
    {
        private int x = 0;
        private int y = 100;
        private final int WIDTH = 100;
        private CustomPanel canvas;
        private Timer drawingTimer;
        private ActionListener timerAction = 
            new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if ((x + WIDTH == 500))
                {
                    x = 0;
                    canvas.setValues(x, y, Color.BLUE);
                }
                else
                {
                    x += WIDTH;
                    canvas.setValues(x, y, Color.BLUE);
                }
            }
        };
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Moving Sqaure");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            canvas = new CustomPanel();
            frame.setContentPane(canvas);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
    
            drawingTimer = new Timer(1000, timerAction);
            drawingTimer.start();
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new MovingSquare().displayGUI();
                }
            });
        }
    }
    
    class CustomPanel extends JPanel
    {
        private final int WIDTH = 500;
        private final int HEIGHT = 500;
        private final int WSQUARE = 100;
        private final int HSQUARE = 50;
    
        private int x = 0;
        private int y = 100;
        private Color cSquare = Color.BLUE;
    
        /* 
         * This is where we updating the state
         * of different variables needed, and
         * thus calling repaint.
         */
        public void setValues(int x, int y, Color color)
        {
            cSquare = color;
            repaint(this.x, this.y, WSQUARE, HSQUARE);
            this.x = x;
            this.y = y;
            repaint(x, y, WSQUARE, HSQUARE);
        }
    
        /*
         * Always make this one customary
         * habbit, to override this method
         * when you extending a JComponent.
         */
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(WIDTH, HEIGHT));
        }
    
        /* 
         * This is where the actual Painting
         * Portion of the whole thingy will
         * reside. Better is, not to put any
         * calculations in this part, just
         * update the state at some another
         * location and convey it to repaint
         * as needed.
         */
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(cSquare);
            g.fillRect(x, y, WSQUARE, HSQUARE);
        }
    }
    

    LATEST EDIT :

    Please try this modified code, the CustomPanel Class is same as before :

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class MovingSquare
    {
        private int x = 0;
        private int y = 100;
        private final int WIDTH = 100;
        private final int HEIGHT = 100;
        private Random random;
        private CustomPanel canvas;
        private Timer drawingTimer;
        private ActionListener timerAction = 
            new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                /*if ((x + WIDTH > 500) && (y + HEIGHT > 500))
                {
                    x = random.nextInt(500 - WIDTH);
                    canvas.setValues(x, y, Color.BLUE);
                }
                else
                {
                    x += WIDTH;
                    canvas.setValues(x, y, Color.BLUE);
                }*/
                x = random.nextInt(500 - WIDTH);
                y = random.nextInt(500 - HEIGHT);
                canvas.setValues(x, y, new Color(
                    random.nextFloat(), random.nextFloat()
                    , random.nextFloat(), random.nextFloat()));
            }
        };
    
        public MovingSquare()
        {
            random = new Random();
        }
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Moving Sqaure");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            canvas = new CustomPanel();
            frame.setContentPane(canvas);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
    
            drawingTimer = new Timer(1000, timerAction);
            drawingTimer.start();
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new MovingSquare().displayGUI();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a similar question last week but did not get an answer that
So I asked a question last year about having an add item button that'll
Last week user Masse asked a question about recursively listing files in a directory
I asked this question last week about how to manually serialize objects, and I
I asked a question last week about how to loop a YouTube video from
This is similar to this question I asked last week. My dataGrid is populated
I previously asked a question about fetching the last 100 mentions for a person
This is related to another question I asked last week, but the current issue
I asked a question yesterday about a problem that Im having with a program
I asked a similar question last week and didn't get a very good response,

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.