-4

I need faster timing for swing timer. Miliseconds are not enough i tried to set it like 0.0000001 but it doesn't get faster;

final javax.swing.Timer tmr=new javax.swing.Timer(1, new ActionListener() {

            public void actionPerformed(ActionEvent e) {


                rsm.setRGB(i, j, rp[i][j]);
                ImageIcon img2 = new ImageIcon(rsm);
                background.setIcon(img2);
                i++;
                if (i==w-1){
                    j++ ;
                    i=0;
                }



            }
        });
Gogo-the-Cat
  • 57
  • 1
  • 11
  • 5
    The system clock is typically accurate +/- 15ms. If you need nanosecond precision, you'll have to start by building a new kind of computer (or at least [real-time clock](https://en.wikipedia.org/wiki/Real-time_clock)). – Elliott Frisch Jun 02 '16 at 22:48
  • Screen refresh speed is only about 60 Hz, maybe as high as 120 Hz, so changing the color more often than that will not be seen anyway. – Andreas Jun 02 '16 at 22:57
  • @Andreas There are [240 Hz displays](http://www.cnet.com/news/240hz-lcd-tvs-what-you-need-to-know/), while a typical rtc uses the same resonance as quartz (32.768 kHz). But you are correct in that OP also needs to design a new kind of *display*. – Elliott Frisch Jun 02 '16 at 23:17
  • Why are you using the 2D Array? Did you not pay attention to the suggestion in your other posting: http://stackoverflow.com/a/37582531/131872? There is no need to waste memory for the 2D array when you can directly access the pixel from the BufferedImage. – camickr Jun 03 '16 at 00:27
  • For [example](http://stackoverflow.com/questions/21636477/java-save-image-pixels-into-an-array-draw-image/21640161#21640161) or [example](http://stackoverflow.com/questions/27163399/how-can-i-draw-an-image-part-by-part/27163506#27163506) – MadProgrammer Jun 03 '16 at 05:22

2 Answers2

1

You can't make the Timer fire faster.

The solution is to paint more pixels each time the Timer fires. The human eye won't notice the difference anyways.

  1. You can calculate the total number of pixels using the width/height of the image.
  2. You know the Timer frequency which really should be set no lower than the 15m/s as suggested above.
  3. You know how long you want to take to display the entire image
  4. Do the math to determine how many pixels need to be displayed each time.
camickr
  • 321,443
  • 19
  • 166
  • 288
0

You have a couple of problems here:

  1. A timer won't fire if it's still executing code from a previous fire. So if your code takes 10 ms to execute, then setting the value to less than 10 will not accomplish anything.
  2. As people said above, it doesn't matter anyway, because screens only update so fast. Even if you could get your timer to fire billions of times a second, your screen can't display the changes that fast.
RobotKarel314
  • 417
  • 3
  • 14
  • this code is about to draw an image pixel by pixel at every fire. i dont want to get every pixel as visual but i wanted to finish to draw image faster. so this i why wanted to faster timer. – Gogo-the-Cat Jun 02 '16 at 23:47
  • @Gogo-the-Cat loop the code in your actionPerformed method 10 times so it does 10 pixels at a time. Or more / less depending on what looks good. It doesn't matter whether your timer fires 10 times our you do it when it's not visible anyways. – zapl Jun 03 '16 at 00:25