⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 edgedpanel.java

📁 经典的货郎担问题解决办法
💻 JAVA
字号:
//-----------------------------------------------------------------------------
//  com.coyotegulch.ui
//
//  A Package of Generic Tools for AWT-based User Interfaces
//  
//  EdgedPanel.java
//  version 3.1.0
//
//  Copyright 1996-2001 Scott Robert Ladd. All rights reserved.
// 
//  For more information about this program, contact:
//
//      Scott Robert Ladd
//      scott@coyotegulch.com
//      http://www.coyotegulch.com
//
//-----------------------------------------------------------------------------
// This program 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 2
// of the License, or (at your option) any later version.
// 
// This program 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 this program; if not, write to the 
//
//      Free Software Foundation, Inc. 
//      59 Temple Place - Suite 330 
//      Boston, MA 02111-1307, USA.
//-----------------------------------------------------------------------------

package com.coyotegulch.ui;

import java.awt.*;

public class EdgedPanel
    extends Panel
{
    // constants
    public static final int FLAT        = 0;
    public static final int RAISED      = 1;
    public static final int PUSHED      = 2;

    // edge characteristics
    protected int m_edge      = RAISED;
    protected int m_thickness = 2;
    protected int m_inset     = 0;

    // exceptions
    protected final static IllegalArgumentException err1 = new IllegalArgumentException("Argument out of range");

    // constructors
    public EdgedPanel()
    {
        super();
    }

    public EdgedPanel
        (
        int edge
        )
    {
        super();

        if ((edge < 0) || (edge > PUSHED))
            throw err1;

        m_edge = edge;
    }

    public EdgedPanel
        (
        int edge,
        int thickness
        )
    {
        super();

        if ((edge < 0) || (edge > PUSHED) || (thickness < 1))
            throw err1;

        m_edge      = edge;
        m_thickness = thickness;
    }

    public EdgedPanel
        (
        int edge,
        int thickness,
        int inset
        )
    {
        super();

        if ((edge < 0) || (edge > PUSHED) || (inset < 0) || (thickness < 1))
            throw err1;

        m_edge      = edge;
        m_thickness = thickness;
        m_inset     = inset;
    }

    // properties
    public int getEdge()
    {
        return m_edge;
    }

    public void setEdge
        (
        int edge
        )
    {
        if ((edge < 0) || (edge > PUSHED))
            throw err1;

        m_edge = edge;
    }

    public int getThickness()
    {
        return m_thickness;
    }

    public void setThickness
        (
        int thickness
        )
    {
        if (thickness < 1)
            throw err1;

        m_thickness = thickness;
    }

    public int getExtraInset()
    {
        return m_inset;
    }

    public void setExtraInset
        (
        int inset
        )
    {
        if (inset < 0)
            throw err1;

        m_inset = inset;
    }

    // methods
    public void update
        (
        Graphics g
        )
    {
        paint(g);
    }

    public void paint
        (
        Graphics g
        )
    {
        Dimension dim = getSize();
        Color tl; // top-left color
        Color br; // bottom-right color

        // set edge colors
        switch (m_edge)
        {
        case FLAT:
            tl = getForeground();
            br = tl;
            break;
        case PUSHED:
            tl = getBackground().darker();
            br = getBackground().brighter();
            break;
        default: // RAISED!
            tl = getBackground().brighter();
            br = getBackground().darker();
        }

        // draw edge lines
        int h = dim.height - 1;
        int w = dim.width  - 1;

        // int dec;

        for (int line = 0; line < m_thickness; ++line)
        {
            // draw top and left
            g.setColor(tl);

            g.drawLine
                (
                0,
                line,
                w - line,
                line
                );

            // draw left
            g.drawLine
                (
                line,
                0,
                line,
                h - line
                );

            // draw bottom and right
            g.setColor(br);

            g.drawLine
                (
                line,
                h - line,
                w,
                h - line
                );

            g.drawLine
                (
                w - line,
                line,
                w - line,
                h
                );
        }

        // fill center
        g.setColor(getBackground());
        g.fillRect(m_thickness,m_thickness,w - 2 * m_thickness,h - 2 * m_thickness);
    }

    public Dimension getPreferredSize()
    {
        Dimension dim = super.getPreferredSize();

        dim.width  += m_thickness + m_inset;
        dim.height += m_thickness + m_inset;

        return dim;
    }

    public Dimension getMinimumSize()
    {
        Dimension dim = super.getPreferredSize();

        dim.width  += m_thickness + m_inset;
        dim.height += m_thickness + m_inset;

        return dim;
    }

    public Insets getInsets()
    {
        return new Insets
                    (
                    m_thickness + m_inset,
                    m_thickness + m_inset,
                    m_thickness + m_inset,
                    m_thickness + m_inset
                    );
    }               
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -