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 952257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:51:53+00:00 2026-05-15T23:51:53+00:00

I was trying to create a 2D Animation in Java of a moving line

  • 0

I was trying to create a 2D Animation in Java of a moving line on Panel(A line moving from one point to another in the Panel). I hope its possible. Here’s the code I used.

private void movingline(int length) throws InterruptedException {
        for(int i = 0; i + length < width; i++){
            for(int j = 0; j + length < height; j++){
                 eraseline(); 
                 drawLine(Color.cyan, i, j, i+length, j+length);
                 erase = true;
            }
        }
    }

    private void eraseline() {
        if(erase){
            fillCanvas(Color.BLUE);
        }
    }

On running the code, the Panel doesn’t show up.

Here’s the code to draw the line.

 public void drawLine(Color c, int x1, int y1, int x2, int y2) {
        int pix = c.getRGB();
        int dx = x2 - x1;
        int dy = y2 - y1;
        canvas.setRGB(x1, y1, pix);
        if (dx != 0) {
            float m = (float) dy / (float) dx;
            float b = y1 - m*x1;
            dx = (x2 > x1) ? 1 : -1;
            while (x1 != x2) {
                x1 += dx;
                y1 = Math.round(m*x1 + b);
                canvas.setRGB(x1, y1, pix);
            }
        }
        repaint();
    }

On running the code the Panel doesn’t show up with the moving line. Any help would be much appreciated.

  • 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-05-15T23:51:53+00:00Added an answer on May 15, 2026 at 11:51 pm

    I think the biggest problem is that you’re trying to change the appearance of the GUI from (I’m guessing) a Thread that’s not the Event Dispatching Thread.

    The solution is to wrap the activity (specifically, the calls to eraseLine and drawLine) in a Runnable and call that Runnable using SwingUtilities.invokeAndWait().


    EDIT: Java’s graphics components don’t really let you manipulate the canvas yourself. Only the components themselves do any drawing, and then only when called on to paint themselves. Directly drawing on the canvas, even if you could get it to work, would work badly because you’d be interfering with what the component does.

    Rather than go into a lot more explanation, I’ve gone and implemented what I think is the “proper” way to do this.

    http://pastebin.com/etfmKbjj

    The coding’s commented where necessary, I hope it gives you some ideas. For more background, read the official tutorials on Swing and Graphics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.