How to make Graphics a separate thread.

Hi all!
I am performing dense 2D Delaunay triangulation on the Java Graphics context. I don't want to add any "sleeps" there in my code, so my app eats about 95-98% of CPU resources. I am becoming unable to normally use any components (i.e. buttons, combos etc) in my app because they are stuck and will responde after 10 secs or more. I tried to get out and did the things specified below.
Assuming I have the following code:
import javax.swing.*;
import java.awt.*;
public class Test extends JPanel {
int count = 0;
int loop = 100;
Graphics g0 = null;
Thread thr = null;
public void paint(Graphics g)
     super.paint(g);
     g0 = g;     
      thr = new Thread(new Runnable() {
          public void run() {
           while (loop < 600) {
                * Really I am doing a lot of things here,
                * so there is no problem with such a blind loop causing 100%CPU usage
                * or "too much threads" errors
               g0.drawString(""+(count++), loop, 200);
               if(count>9)count=1;
                * Uncomment this after one try.
                * Your JVM will crash instantly or a little later
                * depending on the sleep value below.
               //g0.drawLine(loop-10,100, loop-10, 200);
               /*Uncomment this for test and manipulate with the sleep time in range 1..500*/
               //try {Thread.sleep(500);}catch (InterruptedException ex) {}
               loop+=10;               
               repaint();               
           loop = 100;
      thr.setPriority(Thread.MAX_PRIORITY);
      thr.start();
public static void main(String[] args) {
     JFrame fr = new JFrame();
     Test test1 = new Test();
     test1.setDoubleBuffered(true);
     fr.setSize(700,500);
     fr.getContentPane().add(test1);     
     fr.setLocation(70,70);
     fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     fr.show();     
}After the crash my JVM(1.4.2_01-b06 on WinXP) says me "good bye" with this message or a similar long one with the whole dll list:
Another exception has been detected while we were handling last error.
Dumping information about last error:
ERROR REPORT FILE = (N/A)
PC = 0x02eb7516
SIGNAL = -1073741819
FUNCTION NAME = Java_sun_print_Win32PrintJob_endPrintRawData
OFFSET = 0x13E2
LIBRARY NAME = C:\JBuilderX\jdk1.4\jre\bin\awt.dll
Please check ERROR REPORT FILE for further information, if there is any.
Good bye.
An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x2EB7516
Function=Java_sun_print_Win32PrintJob_endPrintRawData+0x13E2
Library=C:\JBuilderX\jdk1.4\jre\bin\awt.dll
Current Java thread:
at sun.awt.windows.Win32DDRenderer.doDrawLineDD(Native Method)
at sun.awt.windows.Win32DDRenderer.clipAndDrawLine(Win32DDRenderer.java:76)
at sun.awt.windows.Win32DDRenderer.drawLine(Win32DDRenderer.java:126)
As I described, the crash happens only when I use drawLine()/draw()/drawRect() or another standard method of Graphics/Graphics2D context except drawString() method.
Did you try to solve a problem like mine is? Can you provide a RIGHT(I know that mine is simple, wrong and dummy ) alternative for separation of Graphics thread from the main one?
Thank you guys for at least reading my post
Waiting for a shameful reply to this message!
Goodbye!

Don't draw the graphics in the AWT event queue thread.
Draw them in the main thread and repaint the
component that displays them as they become
available. The basics of a component like that are
shown in message 15 this thread:
http://forum.java.sun.com/thread.jsp?forum=31&thread=55
795&start=15&range=15&hilite=false&q=
In the same thread under the reply #19 u wrote:
The problem with drawing in the paintComponent() method is that it executes in the GUI thread and this >slows down the rest of the GUI. Many of my GUI apps involve mathematical transformations to images >which take too long to run inside paintComponent().I have the same problem though I am overriding the paint() method, i.e. my GUI thread is being hung even I use the paint() method. Forget about the code above, so there is no thread spawning, no MAX_PRIORITY and no paint()/repaint() recursivity. Did you fixed that problem by such a double buffering like action you specified in the reply #15?
Thx!

Similar Messages

  • How to make graphics in Applet not to repaint after I click "refresh"

    Hi, pals
    I have some interactive graphics inside an Applet. User can drag and
    move the graphic objects. The problem is when user click "Refresh"
    button of the web browser, those objects return to their original
    positions. What I hope is to make those objects still lie on the new
    position that user has moved them to after they click "refresh" or
    "back" and "forward" button to return to the applet page.
    do I need to write some thread control?
    Think when you type new URL in the browser showing the applet and go to the new web page. The system will call
    Applet.stop() and destory() to destroy the applet resource used by the
    Applet webpage. When you go back to the applet page, the system will
    call Applet.init() and start() to recreate it. All previous object
    positions and states manipulated by user will return to original.
    For example, there's a circle centered at (50,50), you move it to
    center (100,100). Then you go to another webpage in the same web
    browser frame. When you use "Back" button to return to the Applet, the
    circle will be redraw at center (50,50). (why? because the browser call applet.init() and do all process as first time the applet download to your computer) What I hope is the circle still keeps
    at its last center (100,100) . This seems impossible to me
    since the applet has lost its memory about the center position
    (100,100) last time when you go to another page and the system call
    destory() to delete the applet.
    Any suggestion?

    I think you can disable page refreshing with javascript, but how, I do not know.
    I wish this is helpfull.
    Ahmad Elsafty

  • How  to make graphics in pdf link to real size gifs/png

    This must be an old question, but when we were going to online help form Frame documents, we chose RoboHelp because we had a perl script
    that would swap back the old (96ppi) screen captures in the html, and so the user would see the large GUIs clearly.
    We are looking to go to an html front end that points to within the pdf documents, but the  screenshots are still unclear, is there a way to have
    a tooltip over each image in the pdf to click to enlarge and then the user would double-click and the original gif/png would appear? The original GUI images would be in a directory with the online help.
    Thanks

    That makes sense. So I guess it looks like I have 2 options.
    1. Put the JS short code in and have it open the link in a new window if the user opens it in adobe. If a google chrome user opens it in the default viewer, they wont be able to use the link at all.
    2. Leave the link how it is, and it will be usable in both viewers, but will not open it in a new window.
    I am sureprised that opening the link in a new window isn't just an option in Acrobat Pro. I wouldn't think you'd have to make a workaround like this.
    Is there an alternative way to have a page link on the form so that people can go to another .pdf whil keeping the main one open?

  • HOW TO MAKE 2 IPODS SEPARATE ITUNES LIBRARY

    HELLO, THIS IS MY FIRST TIME POSTING A QUESTION. I PURCHASED MY IPOD NANO IN DECEMBER AND MY DAUGHTER HAS ONE ALSO, SHE HAS A DIFFERENT ONE THAN MINE, IT HAS LESS STORAGE. MY QUESTION IS, WHEN SHE PLUGS IN HERS TO UDATE AND CHARGE IT SAYS HER NAME AND THE TYPE OF IPOD SHE HAS. HER LIBRARY FILLED WITH ALL HER SONGS AND SHE UPDATES. WHEN I GO TO PLUG IN MINE IT SAYS MY NAME WITH MY IPOD, BUT IT'S THE SAME LIBRARY WITH ALL HER SONGS. SHE DID PLUG IN MINE AND DELETED ALL HER SONGS AND PUT MY SONGS IN MY IPOD. BUT WHEN SHE THEN PLUGGED HERS IN, ALL HER LIBRARY WAS LOST, DELTED AND IT'S WAS THE SONGS SHE PUT IN MINE. SO...HOW DO WE KEEP OUR IPOD/ITUNES IN SEPARTE ONE FOR HER AND HER IPOD AND ONE FOR ME AND MY IPOD? THANK YOU.

    It sounds as though you should look at syncing to a playlist rather than your whole library. That way you can control what goes onto each iPod.
    See this general help:
    http://docs.info.apple.com/article.html?artnum=304708
    And also this on using multiple iPods on one computer:
    http://docs.info.apple.com/article.html?artnum=300432
    I was thinking of method 2, but you could look at the other methods too.

  • How to make graphics move fluidly around screen?

    hi,
    I'm making a simple space invaders game using JPanel and have so far made the ship and aliens move around the screen (the aliens move automatically, the ship requires key presses).
    The ship and aliens are currently simple java 2D shapes and I've used AffineTransform to move them. The problem is the movements aren't fluid. Can someone give me some pointers (possibly some simple code) to make the various objects move fluidly.
    Thanks

    Don't transform them just move them

  • How to Make Graphic Work on the Web?

    Hi all
    I am very new to Illustrator. I am trying to create a banner for one of those Do It Yourself websites. I created my artwork, and saved it for web and devices. However when I upload it, it the image is really tiny. If I stretch it out, it loses quality and goes jaggy.
    What can I do about this? I've tried various things in Illustrator, have done a search on the web, but mainly come up with optimizing graphics, which I am doing already.
    PS the space in which my graphic goes is pre-determined. I cannot change the size of it, but  I want my graphic to fill that space.
    all advice welcome,
    thank you
    cheers
    Mettaphorica

    Mettaphorica,
    You have to set the size in pixels by pixels in the Image Size window; unless you create it in the desired pixel by pixel size.
    The banner should be created in a document with File>Document Color Mode>RGB Mode, and you may as well set the File>Document Setup to the unit Points or Pixels (which is the same to Illy, she is rather tolerant), and work in the actual pixel size.

  • How can make the header repeat in every page?

    I followed the instructions in pages' help nad it does work for text heardes, however it does not work with images and graphics.
    Daoes anyone know how to make graphic and/or images headers repeat in each page?
    Thanks in advance.
    Ruben

    Make them Master Objects:
    http://www.freeforum101.com/iworktipsntrick/viewtopic.php?t=182&highlight=letter head&mforum=iworktipsntrick
    Peter

  • How do I make a JSlider update my canvas with a separate thread?

    I want to have the option to update my canvas slowly (Thread.sleep commands), so I need a separate thread to do this.
    My problem is: I want the slider to make the canvas update continuously as the user moves the slider, but right now, I'm ending up with countless new threads being created...
    How do I:
    1) keep it at only one separate thread that controls redrawing, and
    2) make it stop when the user moves the slider farther
    Also, if I call MyCanvas.repaint(), which is the canvas class in my program, but outside the RepaintThread,
    will this cause my main thead to sleep?

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.
    Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
    From your problem description, a solution may be to use javax.swing.Timer and SwingWorker.
    The Java Tutorials: [Worker Threads and SwingWorker|http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html]
    db

  • How to make a function in separate thread

    i need to make a function that is running in a separate thread
    Thank's in advance

    Thank's
    but in my program i have a main class which is a server it runs forever.
    from the main class when i call a function it runs in a separate thread and returns the result to the callee and the main function continue it's work
    class server
    //this function when called must be in separate thread
    public int function(int x)
    //there could be wait here or sleep
    return x*2;
    public static void main(String[] args)
    // Run RMI server
    i hope u understand me!!!
    Thank's

  • How to make separate/individual text frame from one parent frame in indesign with javascript

    Hi all,
    Please suugest - how to make separate/individual text frame from one parent frame in indesign with javascript.
    Thanks
    Rohit

    @Larry – ah, your interpretation could be the right one…
    May I rephrase the question:
    "How to split threaded text frames to single ones?"
    "SplitStory.jsx" or "BreakFrame.jsx" under Scripts/Samples indeed could be the answer.
    From the comments in the code of "BreakFrame.jsx":
    //Removes the selected text frame (or text frames) from the
    //story containing the text frame and removes the text contained
    //by the text frame from the story.
    //If you want to split *all* of the text fames in the story, use the
    //SplitStory.jsx script.
    Uwe

  • How can I debug a sequence that has a subsequence is running in a separate thread?

    Hi,
    How can I debug a sequence that has a subsequence is running in a separate thread?
    I have to have a continues check for a  digital in signal to be able to terminate the sequence if a physical button is pushed.
    This is running in a separate thread, but this way I cannot debug the main sequence.
    Is there any workaround for this?
    Thanks,
    Andras

    This KB might help you:
    http://digital.ni.com/public.nsf/websearch/46D1E157756C37E686256CED0075E156?OpenDocument
    Let me know if this does not help.
    Allen P.
    NI

  • Need direction on how to make motion graphics -AF CS5

    I didn’t know how to word the header for my question so I’ll try to clarify it here. I’m new to AF so please bear with me. In the Adobe tutorial at the link below, he animates a flower. The flower is in front of a colored background and there are some added text that animate as well. I understand about layers but my question is, can I make a graphic like the one in the tutorial in Photoshop CS5 and import it into AF? I just ordered a copy of PS CS5 and have not gotten a chance to work with PS. When I searched the net about my question, I found many links describing how to make web graphics. Would I be making the same thing and saving them not as a web graphic? If I can make something similar to the flower in the tutorial, then I can go from there. I guess what is confusing for me is that the background and flower assets are mp4 (mpeg) video. I thought it would be two graphics in 2 layers, plus additional text as more layers. http://tv.adobe.com/watch/learn-after-effects-cs5/gs05-working-with-layers/

    Thank you Kevin.  I'll kinda figured that would be the be the steps but wasn't real sure. Once I get going in PS, I'll better understand how to do things.
    Wow, lots to learn.

  • How to make the main() thread wait?

    I would like to know how to make the main() thread wait for another thread?If I use wait() method in main() method it says "non-static method wait() cannot be referenced from a static context",since main()
    is static.

    Here is an example how you may wait for a Thread in the main -
    but be careful, this is no real OO:
    public class WaitMain {
    // this is the thread class - you may also create
    // a runnable - I use a inner class to
    // keep my example simple
         public static class ThreadWait extends Thread{
              public void doSomething(){
                   synchronized(syncObject){
                        System.out.println("Do Something");
                        syncObject.notify();
              public void run(){
                   // sleep 10 seconds - this is
                   // a placeholder to do something in the thread
                   try{
                        sleep(10000);
                        doSomething();
                        sleep(10000);
                   catch(InterruptedException exc){
    // this is the object we wait for -
    // it is just a synchronizer, nothing else
         private static Object syncObject = new Object();
         public static void main(String[] args) {
              System.out.println("This will start a thread and wait for \"doSomething\"");
              ThreadWait t= new ThreadWait();
              t.start();
              synchronized(syncObject){
                   try{
    // this will wait for the notify
                        syncObject.wait();
                        System.out.println("The doSomething is now over!");
                   catch(InterruptedException exc){
              // do your stuff

  • How to make a Thread loop?

    Hi, I was wondering how to make a thread start over again from the beginning once it finishes, if it hasn't been stopped already.

    package src.Utility;
    public class ThreaTest extends Thread{
          * @param args
         public static void main(String[] args) {
              // TODO Auto-generated method stub
              ThreaTest threaTest= new ThreaTest();
              while(true){
                   try{
                   if (!threaTest.isAlive()){
                        threaTest= new ThreaTest();
                        threaTest.start();
                   }//else{
                        //System.out.println("");
                   }catch (Exception e) {
                        e.printStackTrace();
         public void run(){
              System.out.println("-----11---------");
    }try the above code and then tell me if this loop works for you???

  • How to make a global scope component to be thread safe?

    One of my global component preserves states in its member variables, I am afraid that it's might messed up in multi-threading conditions, so how to make a nucleus global component to be thread safe?
    Edited by: user13344577 on Jul 14, 2011 9:45 AM

    Hi,
    Just to help integrate Shaik's and my own seemingly contradictory replies:
    If the member variables in your global component actually need to be globally available, so that multiple threads/sessions need to seem the same data, then you need to use synchronization or some scheme to make this data thread-safe when updating it. If the member variables are non-global data and are simply for convenience of having data available for multiple method calls or something like that, then you should do as I said (don't use synchronization) and use one of my suggestions to avoid this.
    I have seen customers frequently do what I replied about and store non-global data in a global component, and Shaik simply interpreted what "user13344577" is trying to do differently. Clarification is needed to know which of our answers better applies.
    Thanks.
    Nick Glover
    Oracle Support for ATG Products

Maybe you are looking for

  • Validation Error - JSF Custom Component

    I am trying to create a Custom Component. It is a pre-populated list of selection items. So all that the page author would have to do is use a tag like <jsfcust:mySelectOneComponent  value="#{requestScope.selectedItem}/>and this should display a drop

  • Switching from Flash-AS2 to Flex

    Hello !!! I am new in this flex tech I have made a project in AS2 with Flash. and communicating with the data through XMLs here is that link http://sanjoghelpline.in/Application_RWSS/default.asp Now I have to play with large set of data with graphs,

  • Substract in Adobe Forms

    Hi Experts, I am new to adobe forms. I'm trying to substract two dates in this kind of forms. I've found the possibility of add one quantity to other, but no substract.

  • Windows machine on local network can see all volumes mounted on my MBP

    I have a Macbook Pro running 10.6.4 with 4 volumes mounted (Macintosh HD, Scratch Drive, Files, & Media). On this same network I have a Windows 7 machine. I just noticed today that in the Windows File Explorer I can see all 4 of the volumes that I ha

  • Windows 8.1 Error (Blue Screen) DRIVER IRQL NOT LESS THAN OR EQUA

    i need help with this its been very annoying every time i try to play a game after few minutes win 8 show and error and blue screeen apprear's sorry we have encountered a problem DRIVER IRQL NOT LESS THAN OR EQUAL please help  me fix this my laptop H