0

At below, its my code to make a renewable graphics shows life game itself.

public paint(Graphics g)
{
     g.setClip();//Set              
     g.setColor(Color.BLACK);

     Processing.....

     while(true)
     {
          g.clearRect();//Clear

          g.drawLine(); //Draw;

          Calling SwingWorking to prepare next statement
     }
}

seems it's stupid to do like this way because when running it, the frame keep flashing and laggy. I try to use Thread.sleep(), but it just slow down the frequency of flash.

So, my problem is how to make it well and avoid flashing. The parts of code that I don't put on are all about data processing for instance variables, if you need it,please notice me, much appreciate for help.


Thanks, @MadProgrammer

Here is what I got, first, the flash is because when program calling repaint() and paint(), it cost too much time to make animation smoothly, in this case is clearRect() and drawLine().

I'll use double buffered to solve it.


Update,

Thanks guys, I read those examples. I just use a frame without any buffer method to show graphics before, what a mistake. I used bufferStrategy to solve it.

TomN
  • 574
  • 3
  • 18
  • 2
    `JFrame` is not double buffered, so I would't override it's `paint` method. – MadProgrammer Jan 12 '16 at 04:07
  • 2
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jan 12 '16 at 04:07
  • 2
    Based on a single, out-of-context, code snippet, I would recommend you start by looking at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html), [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/), [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for some possible ideas of what's causing the problem and possible solutions – MadProgrammer Jan 12 '16 at 04:09
  • *" it cost too much time to make animation smoothly"* - I have a few hundred examples which might suggestion otherwise. If you "really" need control over the painting process then you will need to use a [`BufferStrategy`](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html) and [BufferStrategy and BufferCapabilities](https://docs.oracle.com/javase/tutorial/extra/fullscreen/bufferstrategy.html) – MadProgrammer Jan 12 '16 at 21:33

1 Answers1

1

Not an answer, but a demonstration

it cost too much time to make animation smoothly

To me, this means you don't understand how the API works, you can do some complex animations with Swing if you put your mind to it.

A while ago I created an "animated sequence" engine, which took a series of images and animated them on top of each other

This examples uses 5 separate images/layers (with a paint effect put over the top)

Layers

Each layer is given a different "speed" so that they move at different speeds through out the base time of the animation (for example, a speed of 1 will cause the layer to rotate only once over the base duration of the sequence, in this example, 20 seconds)

The original images are all 1024x256, so that an inconsiderate size

Sequence

Add ontop of that the sequence is playing in transparent window with the addition of an alpha effect (bleeding off at the edges), this is not a simple animation.

The gif is playing at rough 8fps, the actual animation runs at roughly 200fps

(Sorry, the code for this is quite large and makes use of a number of other libraries, like the Timing Framework, so it's impossible to post)

My point is, the problem isn't with the API (Swing/Graphics) alone, but with how you are using it

For more examples you could have a look at:

and most of these examples aren't even trying for any major optimization or other performance technqiues

You should also have a look at Painting in AWT and Swing and Performing Custom Painting for more details about how the painting process works

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366