Changeset 5500 in josm for trunk/src/org


Ignore:
Timestamp:
2012-09-04T00:13:37+02:00 (12 years ago)
Author:
Don-vip
Message:

Improves selection graphical performance by replacing the external calls to Component.getGraphics() with proper Swing paint mechanism

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r5311 r5500  
    44import static org.openstreetmap.josm.tools.I18n.marktr;
    55
     6import java.awt.Color;
    67import java.awt.Cursor;
     8import java.awt.Graphics;
    79import java.awt.Point;
     10import java.awt.Polygon;
    811import java.awt.Rectangle;
    912import java.awt.geom.AffineTransform;
     
    108111    protected EastNorth center = calculateDefaultCenter();
    109112
     113    private static final Object paintRequestLock = new Object();
     114    private Rectangle paintRect = null;
     115    private Polygon paintPoly = null;
     116   
    110117    public NavigatableComponent() {
    111118        setLayout(null);
     
    12661273        Cursors = c;
    12671274    }
     1275   
     1276    @Override
     1277    public void paint(Graphics g) {
     1278        synchronized (paintRequestLock) {
     1279            if (paintRect != null) {
     1280                Graphics g2 = g.create();
     1281                g2.setColor(Color.BLACK);
     1282                g2.setXORMode(Color.WHITE);
     1283                g2.drawRect(paintRect.x, paintRect.y, paintRect.width, paintRect.height);
     1284            }
     1285            if (paintPoly != null) {
     1286                Graphics g2 = g.create();
     1287                g2.setColor(Color.WHITE);
     1288                g2.drawPolyline(paintPoly.xpoints, paintPoly.ypoints, paintPoly.npoints);
     1289            }
     1290        }
     1291        super.paint(g);
     1292    }
     1293
     1294    /**
     1295     * Requests to paint the given {@code Rectangle}.
     1296     * @param r The Rectangle to draw
     1297     * @see #requestClearRect
     1298     * @since 5500
     1299     */
     1300    public void requestPaintRect(Rectangle r) {
     1301        if (r != null) {
     1302            synchronized (paintRequestLock) {
     1303                paintRect = r;
     1304            }
     1305            repaint();
     1306        }
     1307    }
     1308   
     1309    /**
     1310     * Requests to paint the given {@code Polygon} as a polyline (unclosed polygon).
     1311     * @param p The Polygon to draw
     1312     * @see #requestClearPoly
     1313     * @since 5500
     1314     */
     1315    public void requestPaintPoly(Polygon p) {
     1316        if (p != null) {
     1317            synchronized (paintRequestLock) {
     1318                paintPoly = p;
     1319            }
     1320            repaint();
     1321        }
     1322    }
     1323   
     1324    /**
     1325     * Requests to clear the rectangled previously drawn.
     1326     * @see #requestPaintRect
     1327     * @since 5500
     1328     */
     1329    public void requestClearRect() {
     1330        synchronized (paintRequestLock) {
     1331            paintRect = null;
     1332        }
     1333        repaint();
     1334    }
     1335
     1336    /**
     1337     * Requests to clear the polyline previously drawn.
     1338     * @see #requestPaintPoly
     1339     * @since 5500
     1340     */
     1341    public void requestClearPoly() {
     1342        synchronized (paintRequestLock) {
     1343            paintPoly = null;
     1344        }
     1345        repaint();
     1346    }
    12681347}
  • trunk/src/org/openstreetmap/josm/gui/SelectionManager.java

    r5152 r5500  
    22package org.openstreetmap.josm.gui;
    33
    4 import java.awt.Color;
    54import java.awt.Component;
    6 import java.awt.Graphics;
    75import java.awt.Point;
    86import java.awt.Polygon;
     
    5755         * Called, when the left mouse button was released.
    5856         * @param r The rectangle that is currently the selection.
    59          * @param alt Whether the alt key was pressed
    60          * @param shift Whether the shift key was pressed
    61          * @param ctrl Whether the ctrl key was pressed
     57         * @param e The mouse event.
    6258         * @see InputEvent#getModifiersEx()
    6359         */
     
    119115     * Register itself at the given event source.
    120116     * @param eventSource The emitter of the mouse events.
     117     * @param lassoMode {@code true} to enable lasso mode, {@code false} to disable it.
    121118     */
    122119    public void register(NavigatableComponent eventSource, boolean lassoMode) {
     
    199196        Rectangle r;
    200197        if (!lassoMode) {
    201             paintRect();
     198            nc.requestClearRect();
    202199            r = getSelectionRectangle();
    203200
    204201            lasso = rectToPolygon(r);
    205202        } else {
     203            nc.requestClearPoly();
    206204            lasso.addPoint(mousePos.x, mousePos.y);
    207205            r = lasso.getBounds();
     
    216214
    217215    /**
    218      * Draw a selection rectangle on screen. If already a rectangle is drawn,
    219      * it is removed instead.
     216     * Draws a selection rectangle on screen.
    220217     */
    221218    private void paintRect() {
    222219        if (mousePos == null || mousePosStart == null || mousePos == mousePosStart)
    223220            return;
    224         Graphics g = nc.getGraphics();
    225         g.setColor(Color.BLACK);
    226         g.setXORMode(Color.WHITE);
    227 
    228         Rectangle r = getSelectionRectangle();
    229         g.drawRect(r.x,r.y,r.width,r.height);
    230     }
    231 
     221        nc.requestPaintRect(getSelectionRectangle());
     222    }
     223   
    232224    private void paintLasso() {
    233225        if (mousePos == null || mousePosStart == null || mousePos == mousePosStart) {
    234226            return;
    235227        }
    236 
    237         Graphics g = nc.getGraphics();
    238         g.setColor(Color.WHITE);
    239 
    240         int lastPosX = lasso.xpoints[lasso.npoints - 1];
    241         int lastPosY = lasso.ypoints[lasso.npoints - 1];
    242         g.drawLine(lastPosX, lastPosY, mousePos.x, mousePos.y);
    243 
    244228        lasso.addPoint(mousePos.x, mousePos.y);
     229        nc.requestPaintPoly(lasso);
    245230    }
    246231
     
    302287     * @param alt Whether the alt key was pressed, which means select all
    303288     * objects that are touched, instead those which are completely covered.
     289     * @return The collection of selected objects.
    304290     */
    305291    public Collection<OsmPrimitive> getSelectedObjects(boolean alt) {
     
    368354    }
    369355
     356    /**
     357     * Enables or disables the lasso mode.
     358     * @param lassoMode {@code true} to enable lasso mode, {@code false} to disable it.
     359     */
    370360    public void setLassoMode(boolean lassoMode) {
    371361        this.lassoMode = lassoMode;
Note: See TracChangeset for help on using the changeset viewer.