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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:54:40+00:00 2026-06-04T21:54:40+00:00

I am working on a concurrent canvas written in Java which will make the

  • 0

I am working on a concurrent canvas written in Java which will make the users think that they are drawing on the canvas in parallel.

In order to achieve the user perceived parallelism, I get them to create these Runnable objects that I go ahead and put on the EventQueue using SwingUtilities.invokeLater().

In order to test it, I simulated the users using a couple of Threads and added a bit of delay(around 50ms) between each call to invokeLater(), in order to see if it actually looks like the drawing is happening in parallel.

The problem is that while it worked fine with that added delay between the invokeLater() calls, taking out that delay results in the drawings being drawn properly sometimes, partially being drawn and vanishing sometimes and just not being drawn at other times.

I am pretty stumped about what could be going wrong so any if anyone has any ideas, please let me know.

Following is the code with the delay commented out:

public void run(){
    //add tasks on to the event queue of the EDT 
    for(int i = 0; i<numLines; i++){
        DrawLineTask task = new DrawLineTask(g, x1, y1+i, x2, y2+i, lineColor);
        SwingUtilities.invokeLater(task);
//          try {
//    Thread.sleep(new Double(Math.random()*50).longValue());//random sleeping times to             make it appear more realistic
//          } catch (InterruptedException e) {
//              e.printStackTrace();
//          }
    }

Cheers

EDIT: Here is the code for the DrawLineTask as requested. Its pretty simple as its just an extension of the Runnable class that draws a line using the standard Java function at the given parameters.

public class DrawLineTask implements Runnable {
Graphics g;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
Color color = Color.BLACK;

public DrawLineTask(Graphics g, int x1, int y1, int x2, int y2){
    this.g = g;
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
}

public DrawLineTask(Graphics g, int x1, int y1, int x2, int y2, Color color){
    this.g = g;
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.color = color;
}

@Override
public void run() {
    g.setColor(color);
    g.drawLine(x1, y1, x2, y2);
}

}
  • 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-04T21:54:42+00:00Added an answer on June 4, 2026 at 9:54 pm

    AFAIK, you’re not supposed to keep a reference to a Graphics object and draw on it when you want to. Instead, you’re supposed to wait for the paintComponent() method to be called by Swing, and do your drawings in this method.

    So, your task should just change the state of your component, and ask for an asynchronous or synchronous repaint (using repaint() or paintImmediately()). Swing will then invoke the paintComponent() method with a Graphics object that you can use to paint the appropriate lines, based on the state of your component.

    See http://java.sun.com/products/jfc/tsc/articles/painting/ for more details and explanations.

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

Sidebar

Related Questions

I'm working on a .net portal which would be having lots of concurrent users.
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put
I'm currently working on some concurrent code that would appear to have a few
I am working on a java program that is essentially a chat room. This
I've recently started working on a new application which will utilize task parallelism. I
So I have Dictionary<string, SomeClass> which will be accessed heavily by multiple concurrent threads
I'm working on an MVC 3 site which is hosted in Windows Azure that
Im working on a java application that involves threads. So i just wrote a
I'm new to concurrent programing and have been working on code which has a
I am working with threads, and decided to use the most modern API (java.util.concurrent

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.