Java Graphics Draw Border paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int width, int height)

Here you can find the source of paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int width, int height)

Description

_more_

License

Open Source License

Parameter

Parameter Description
g _more_
borderType _more_
insets _more_
colors _more_
width _more_
height _more_

Declaration

public static void paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int width,
        int height) 

Method Source Code

//package com.java2s;
/**//w ww .ja  v a2s.  co  m
* Copyright (c) 2008-2015 Geode Systems LLC
* This Software is licensed under the Geode Systems RAMADDA License available in the source distribution in the file 
* ramadda_license.txt. The above copyright notice shall be included in all copies or substantial portions of the Software.
*/

import java.awt.*;

public class Main {
    /** _more_ */
    public static final int BORDER_RAISED = 1;
    /** _more_ */
    public static final int BORDER_SUNKEN = 2;
    /** _more_ */
    public static final int BORDER_MATTE = 4;
    /** _more_ */
    public static final Insets borderInsets = new Insets(1, 1, 1, 1);
    /** _more_ */
    public static final Color[] borderColors = { null, null, null, null };

    /**
     * _more_
     *
     * @param g _more_
     * @param borderType _more_
     * @param width _more_
     * @param height _more_
     */
    public static void paintBorder(Graphics g, int borderType, int width, int height) {
        paintBorder(g, borderType, borderInsets, borderColors, width, height);
    }

    /**
     * _more_
     *
     * @param g _more_
     * @param borderType _more_
     * @param insets _more_
     * @param colors _more_
     * @param width _more_
     * @param height _more_
     */
    public static void paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int width,
            int height) {
        paintBorder(g, borderType, insets, colors, 0, 0, width, height);
    }

    /**
     * _more_
     *
     * @param g _more_
     * @param borderType _more_
     * @param insets _more_
     * @param colors _more_
     * @param x _more_
     * @param y _more_
     * @param width _more_
     * @param height _more_
     */
    public static void paintBorder(Graphics g, int borderType, Insets insets, Color[] colors, int x, int y,
            int width, int height) {

        width--;
        height--;

        if (borderType == BORDER_RAISED) {
            g.setColor(Color.darkGray);
            g.drawLine(x, y + height, x + width - 1, y + height);
            g.drawLine(x + width, y + 0, x + width, y + height);

            g.setColor(Color.gray);
            g.drawLine(x + 1, y + height - 1, x + width - 2, y + height - 1);
            g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 1);

            g.setColor(Color.white);
            g.drawLine(x, y, x, y + height - 1);
            g.drawLine(x, y, x + width - 1, y);
        } else if (borderType == BORDER_SUNKEN) {
            g.setColor(Color.gray);
            g.drawLine(x, y, x + width - 1, y);
            g.drawLine(x, y, x, y + height - 1);
            g.setColor(Color.black);
            g.drawLine(x + 1, y + 1, x + width - 2, y + 1);
            g.drawLine(x + 1, y + 1, x + 1, y + height - 2);
            g.setColor(Color.white);
            g.drawLine(x, y + height, x + width, y + height);
            //        g.drawLine(1, height - 1, width - 1, height - 1); 
            g.drawLine(width, y, x + width, y + height);
            //        g.drawLine(width - 1, 1, width - 1, height - 1); 
        }
        /*    else if(borderType  == BORDER_ETCHED)
          {
          g.setColor(Color.gray);
          g.drawLine(x, y, x, y+height - 1);
          g.drawLine(x, y, x+width - 1, y);
          g.setColor(Color.white);
          g.drawLine(x+1,y+ 1, x+1, y+height - 2);
          g.drawLine(x+1,y+ 1, x+width - 3, y+1);
          g.setColor(Color.gray);
          g.drawLine(x, y+height - 1, x+width - 1,y+ height - 1);
          g.drawLine(width - 1, y+2, x+width - 1, y+height);
          g.setColor(Color.white);
          g.drawLine(x, y+height, x+width,y+ height);
          g.drawLine(width, y, x+width, y+height);
          }
        */
        else if (borderType == BORDER_MATTE) {
            width += 1;
            height += 1;
            if (insets.top != 0) {
                g.setColor(((colors[0] != null) ? colors[0] : Color.black));
                g.fillRect(x, y, width, insets.top);
            }

            if (insets.bottom != 0) {
                g.setColor(((colors[2] != null) ? colors[2] : Color.black));
                g.fillRect(x, y + height - insets.bottom, width, insets.bottom);
            }

            if (insets.left != 0) {
                g.setColor(((colors[1] != null) ? colors[1] : Color.black));
                g.fillRect(x, y, insets.left, height);
            }

            if (insets.right != 0) {
                g.setColor(((colors[3] != null) ? colors[3] : Color.black));
                g.fillRect(x + width - insets.right, y, insets.right, height);
            }

        }
    }

    /**
     * _more_
     *
     * @param g _more_
     * @param x1 _more_
     * @param y1 _more_
     * @param x2 _more_
     * @param y2 _more_
     * @param lineWidth _more_
     */
    public static void drawLine(Graphics g, int x1, int y1, int x2, int y2, int lineWidth) {
        if (lineWidth == 1) {
            g.drawLine(x1, y1, x2, y2);
        } else {
            g.drawLine(x1, y1, x2, y2);
            double halfWidth = ((double) lineWidth) / 2.0;
            double deltaX = (double) (x2 - x1);
            double deltaY = (double) (y2 - y1);
            double angle = ((x1 == x2) ? Math.PI : Math.atan(deltaY / deltaX) + Math.PI / 2);
            int xOffset = (int) (halfWidth * Math.cos(angle));
            int yOffset = (int) (halfWidth * Math.sin(angle));
            int[] xCorners = { x1 - xOffset, x2 - xOffset + 1, x2 + xOffset + 1, x1 + xOffset };
            int[] yCorners = { y1 - yOffset, y2 - yOffset, y2 + yOffset + 1, y1 + yOffset + 1 };
            g.fillPolygon(xCorners, yCorners, 4);

            Color c = g.getColor();
            g.setColor(Color.red);
            int hw = (int) halfWidth;
            int dw = hw * 2;
            //      g.fillArc(x1-hw,y1-hw,dw,dw,0,360);
            //      g.drawLine(x1,y1,x2,y2);
            g.setColor(c);

        }
    }

    /**
     * _more_
     *
     * @param value _more_
     *
     * @return _more_
     */
    public static Color getColor(String value) {
        return getColor(value, Color.black);
    }

    /**
     * _more_
     *
     * @param value _more_
     * @param dflt _more_
     *
     * @return _more_
     */
    public static Color getColor(String value, Color dflt) {
        if (value == null) {
            return dflt;
        }
        value = value.trim();
        if (value.equals("null")) {
            return null;
        }
        String s = value;
        String lookFor = ",";
        int i1 = s.indexOf(lookFor);
        if (i1 < 0) {
            lookFor = " ";
            i1 = s.indexOf(lookFor);
        }
        if (i1 > 0) {
            String red = s.substring(0, i1);
            s = s.substring(i1 + 1).trim();
            int i2 = s.indexOf(lookFor);
            if (i2 > 0) {
                String green = s.substring(0, i2);
                String blue = s.substring(i2 + 1);
                try {
                    return new Color(Integer.decode(red).intValue(), Integer.decode(green).intValue(),
                            Integer.decode(blue).intValue());
                } catch (Exception exc) {
                    System.err.println("Bad color:" + value);
                }
            }
        }

        try {
            return new Color(Integer.decode(s).intValue());
        } catch (Exception e) {
            s = s.toLowerCase();
            if (s.equals("blue")) {
                return Color.blue;
            }
            if (s.equals("black")) {
                return Color.black;
            }
            if (s.equals("red")) {
                return Color.red;
            }
            if (s.equals("gray")) {
                return Color.gray;
            }
            if (s.equals("lightgray")) {
                return Color.lightGray;
            }
            if (s.equals("white")) {
                return Color.white;
            }
            if (s.equals("green")) {
                return Color.green;
            }
            if (s.equals("orange")) {
                return Color.orange;
            }

            return dflt;
        }
    }

    /**
     * _more_
     *
     * @param source _more_
     * @param patterns _more_
     *
     * @return _more_
     */
    public static int indexOf(String source, String[] patterns) {
        int idx = -1;
        for (int i = 0; i < patterns.length; i++) {
            int tmpIdx = source.indexOf(patterns[i]);
            if ((idx == -1) || ((tmpIdx >= 0) && (tmpIdx < idx))) {
                idx = tmpIdx;
            }
        }

        return idx;
    }
}

Related

  1. drawButtonBorder(Graphics g, int x, int y, int w, int h, Color backgroundColor, Color edgeColor, Color cornerColor)
  2. drawDirectionalTriangle(Graphics2D bbg, double heading, double x, double y, double sideLength, Color fillColor, Color borderColor)
  3. drawEtchedBorder(Graphics g, int x, int y, int width, int height, Color dark, Color bright)
  4. drawHighlightBorder(Graphics g, int x, int y, int width, int height, boolean raised, Color shadow, Color highlight)
  5. drawSquareBorder(Graphics2D g, int x, int y, int width, int height, int rounding, Color color, float strokeWidth)
  6. paintBorder(Rectangle r, Graphics2D g2d)
  7. paintBorderDebugInfo(final Graphics g, final JComponent c, final Color color)
  8. paintBorderGlow(final Graphics2D g2, final Shape shape, final int glowWidth)
  9. paintBorderGlow(Graphics2D g2, int glowWidth, Shape clipShape, Color glowOuterHigh, Color glowOuterLow)