Title: RE: [JAVA2D] Slow response

That's what so many people say, "don't mix Swing and AWT". They just don't have the experience and gave up on it too quick. These are the biggest problems people have:

1. Never put any AWT elements inside a JScrollPane's hierarchy.
2. If you override the paint() method for an AWT container, you MUST call super.paint() or the Swing components inside your hierarchy will not get painted.

3. Understand what validate(), invalidate(), revalidate() and repaint() actually do.

You can't give up unless you try.

Jeff

-----Original Message-----
From: Karsten Trott [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 24, 2001 12:50 AM
To: Jeff Kardatzke
Cc: [EMAIL PROTECTED]
Subject: Re: [JAVA2D] Slow response


Well, I think this is a bad idea. DON'T MIX SWING and AWT- You will get
a looooot of problems, believe me.
A much better way for solving this problem is to change the vehicles and
the container that holds the vehicles in the following way:
a) create an object for the vehicle (not derived from a JComponent in
any way) and add a function to draw it into a given Graphics2D context:
    class Vehicle {
        // do whatever you need here, i.e.:
        int xPos,YPos;
        // add additional functions

        // add a drawing function:
        public void draw(Graphics2D g2) {
            g2.setColor(whatevercoloryouwant);
            g2.drawRectangle(x,y,10,10);
            // add more....
        }
    }

b) create a container (derived from JComponent) and overload the
add/remove and paint functions:
class Street extends JComponent {
    ArrayList vehicles;
    public Street() {
        vehicles = new ArrayList();
    }
    public void add(Vehicle vehicle) {
        vehicles.add(vehicle);
    }

    public void paintComponents(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        // synchronize the list of vehicles!!
        synchronized (vehicles) {
            Iterator allVehicles = vehicles.iterator();
            while (allVehicles.hasNext()) {
                Vehicle v = (Vehicle)allVehicles.next();
                v.draw(g2);
            }
        }
    }
}

Something like that should solve your problem. The problem you described
is that you used lots of objects derived from JComponent (or its
children) and the swing environment has to draw lots of things just to
draw borders, icons, .... - and has to check for the correct order,
blablabla, which takes lots of time.
This method will draw only (frameless) objects on the screen.

I can easily draw some hundred up to thousands of elements each second.

Karsten
PS: btw. if it happens that some objects are outside the visible screen,
you should check if they overlap the visible screen part and discard the
drawing (speeds up by factor of n by not drawing them, if they are not
visible - even if the java runtime will not draw them either)

> How many vehicles are you painting?
>
> I've written many apps that have situations like this. I used to write
> them the way you're describing, where I'd use an individual
> Compoenent/JComponent for each object I was drawing. It made hit
> detection really easy as well as drag&drop. Unfortunately, if you want
> to get any real performance this strategy will not work.  It's also
> very error prone.  There's too much reliance on the underlying
> windowing system to do a lot of heavy lifting for you.
>
> The technique I highly recommend and now use all the time is doing
> your own lightweight implementation. Do everything in AWT, and only
> use Swing for really complex widgets like JTree/JTable/JTabbedPane,
> etc. There's a lot of tricky rules about AWT/Swing mixing, but they're
> very well known. You also have to deal with delegating mouse events
> out to the visual objects, which isn't that hard. You also need to
> deal with the painting of the children, not that hard in AWT.  These
> systems are much more complicated to build, but are highly robuts and
> perform very well. On a current project I can get fluid motion(>30
> fps) of about 50 objects that interact with a DB for every frame. A
> prior implementation in Swing like you described below of this yielded
> ~0.5 fps.  It also was subject to the random whims of Swing global
> repaint calls and drawing artifacts.
>
> That's my 0.02
>
> Jeff Kardatzke
>
> -----Original Message-----
> From: Wong, Fook Hoi [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, July 22, 2001 9:37 PM
> To: [EMAIL PROTECTED]
> Subject: [JAVA2D] Slow response
>
> hi,
>
> I am using java to create a program which simulates moving vehicles.
> The
> platform is a dual CPU PC (800Mhz  & 512MB ram) with Windows2000 OS.
>
> Basically, the APPLICATION consists of 2 panels, one (a JLayeredPane)
> is
> used for the painting of the icons representing the vehicles which are
>
> updated every second via calls to JComponent.setLocation() for each of
> the
> icons.  The other panel  ('control panel')  consist of JTextFields and
>
> JButtons with which i interact with the application - such as zooming,
> etc.
>
> The PROBLEM i am facing is that as more vehicles are added to the
> JLayeredPane, the 'control panel' reaction time slows down.  Above a
> certain load, the response for a click on a button can slow to 3 or 4
> seconds.  As an example, when i click on a JComboBox, the list will be
>
> displayed only after 4 secs.  The interesting thing is that at all
> times,
> the CPU load (via windows Task Manager) only indicates about 50% load
> on
> both processors, memory usage is also way below 512MB .  Also the
> icons
> still seem to be repainted regularly at 1 second intervals.   I dont
> understand what is causing the delay in the updates on the 'control
> panel'.
>
> Any clues / help in this is greatly appreciated.
>
>
> ==========================================================================
>
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
> body
> of the message "signoff JAVA2D-INTEREST".  For general help, send
> email to
> [EMAIL PROTECTED] and include in the body of the message "help".

--
Karsten Trott
Technical Lead, Intuitive Design Interface , Get2Chip.com, Inc.
Phone: +49-89-5908-2333
Fax:   +49-89-5908-1328


Reply via email to