Draw a focus rectangle on a graphics context. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

Draw a focus rectangle on a graphics context.

Demo Code

/*//from w  w w .  j  a v a2  s . c o  m
This file is part of leafdigital util.

util is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

util is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with util.  If not, see <http://www.gnu.org/licenses/>.

Copyright 2011 Samuel Marshall.
 */
//package com.java2s;
import java.awt.*;

public class Main {
    /**
     * Draw a focus rectangle on a graphics context.
     * @param g2 Graphics context
     * @param iX X position (e.g. 0)
     * @param iY Y position
     * @param iWidth Actual desired width (e.g. getWidth())
     * @param iHeight Actual desired height
     */
    public static void drawFocus(Graphics2D g2, int iX, int iY, int iWidth,
            int iHeight) {
        int FOCUSTRANSPARENCY = 96;
        Color beforeColor = g2.getColor();
        Stroke beforeStroke = g2.getStroke();
        g2.setColor(new Color(255, 255, 255, FOCUSTRANSPARENCY));
        g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL, 1.0f, new float[] { 1.0f, 1.0f },
                0.0f));
        g2.drawRect(iX, iY, iWidth - 1, iHeight - 1);
        g2.setColor(new Color(0, 0, 0, FOCUSTRANSPARENCY));
        g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL, 1.0f, new float[] { 1.0f, 1.0f },
                1.0f));
        g2.drawRect(iX, iY, iWidth - 1, iHeight - 1);
        g2.setColor(beforeColor);
        g2.setStroke(beforeStroke);
    }
}

Related Tutorials