Java Tutorial/Swing/AbstractBorder — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 04:57, 1 июня 2010
Содержание
Bottom Border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.AbstractBorder;
public class BottomBorder extends AbstractBorder {
protected int thickness;
protected Color lineColor;
protected int gap;
public BottomBorder(Color color) {
this(color, 1, 1);
}
public BottomBorder(Color color, int thickness) {
this(color, thickness, thickness);
}
public BottomBorder(Color color, int thickness, int gap) {
lineColor = color;
this.thickness = thickness;
this.gap = gap;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
int i;
g.setColor(lineColor);
for (i = 0; i < thickness; i++) {
g.drawLine(x, y + height - i - 1, x + width, y + height - i - 1);
}
g.setColor(oldColor);
}
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, gap, 0);
}
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = 0;
insets.top = 0;
insets.right = 0;
insets.bottom = gap;
return insets;
}
/**
* Returns the color of the border.
*/
public Color getLineColor() {
return lineColor;
}
/**
* Returns the thickness of the border.
*/
public int getThickness() {
return thickness;
}
/**
* Returns whether or not the border is opaque.
*/
public boolean isBorderOpaque() {
return false;
}
public int getGap() {
return gap;
}
}
Rectangle border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.border.AbstractBorder;
public class RectangleBorder extends AbstractBorder {
protected Insets thickness;
protected Color lineColor;
protected Insets gap;
public RectangleBorder(Color color) {
this(color, new Insets(1, 1, 1, 1));
}
public RectangleBorder(Color color, Insets thickness) {
this(color, thickness, thickness);
}
public RectangleBorder(Color color, Insets thickness, Insets gap) {
lineColor = color;
this.thickness = thickness;
this.gap = gap;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
g.setColor(lineColor);
// top
for (int i = 0; i < thickness.top; i++) {
g.drawLine(x, y + i, x + width, y + i);
}
// bottom
for (int i = 0; i < thickness.bottom; i++) {
g.drawLine(x, y + height - i - 1, x + width, y + height - i - 1);
}
// right
for (int i = 0; i < thickness.right; i++) {
g.drawLine(x + width - i - 1, y, x + width - i - 1, y + height);
}
// left
for (int i = 0; i < thickness.left; i++) {
g.drawLine(x + i, y, x + i, y + height);
}
g.setColor(oldColor);
}
/**
* Returns the insets of the border.
*
* @param c
* the component for which this border insets value applies
*/
public Insets getBorderInsets(Component c) {
return gap;
}
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = gap.left;
insets.top = gap.top;
insets.right = gap.right;
insets.bottom = gap.bottom;
return insets;
}
/**
* Returns the color of the border.
*/
public Color getLineColor() {
return lineColor;
}
/**
* Returns the thickness of the border.
*/
public Insets getThickness() {
return thickness;
}
/**
* Returns whether or not the border is opaque.
*/
public boolean isBorderOpaque() {
return false;
}
public Insets getGap() {
return gap;
}
}
Right border
//com.amkai.borders;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Color;
import java.awt.ruponent;
import javax.swing.border.AbstractBorder;
public class RightSideBorder extends AbstractBorder
{
protected int thickness;
protected Color lineColor;
protected int gap;
public RightSideBorder(Color color) {
this(color, 1, 1);
}
public RightSideBorder(Color color, int thickness) {
this(color, thickness, thickness);
}
public RightSideBorder(Color color, int thickness, int gap) {
lineColor = color;
this.thickness = thickness;
this.gap = gap;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color oldColor = g.getColor();
int i;
g.setColor(lineColor);
for(i = 0; i < thickness; i++) {
g.drawLine(x+width-i-1, y, x+width-i-1, y+height);
}
g.setColor(oldColor);
}
/**
* Returns the insets of the border.
* @param c the component for which this border insets value applies
*/
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, 0,gap);
}
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = 0;
insets.top = 0;
insets.right = gap;
insets.bottom = 0;
return insets;
}
/**
* Returns the color of the border.
*/
public Color getLineColor() {
return lineColor;
}
/**
* Returns the thickness of the border.
*/
public int getThickness() {
return thickness;
}
/**
* Returns whether or not the border is opaque.
*/
public boolean isBorderOpaque() {
return false;
}
public int getGap() {
return gap;
}
}
Shadow Popup Border
/*
* Copyright (c) 2001-2009 JGoodies Karsten Lentzsch. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of JGoodies Karsten Lentzsch nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.border.AbstractBorder;
/**
* A border with a drop shadow intended to be used as the outer border
* of popups. Can paint the screen background if used with heavy-weight
* popup windows.
*
* @author Karsten Lentzsch
* @author Andrej Golovnin
* @version $Revision: 1.6 $
*
* @see ShadowPopup
* @see ShadowPopupFactory
*/
final class ShadowPopupBorder extends AbstractBorder {
/**
* The drop shadow needs 5 pixels at the bottom and the right hand side.
*/
private static final int SHADOW_SIZE = 5;
/**
* The singleton instance used to draw all borders.
*/
private static ShadowPopupBorder instance = new ShadowPopupBorder();
/**
* The drop shadow is created from a PNG image with 8 bit alpha channel.
*/
private static Image shadow
= new ImageIcon(ShadowPopupBorder.class.getResource("shadow.png")).getImage();
// Instance Creation *****************************************************
/**
* Returns the singleton instance used to draw all borders.
*/
public static ShadowPopupBorder getInstance() {
return instance;
}
/**
* Paints the border for the specified component with the specified
* position and size.
*/
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
// draw drop shadow
g.drawImage(shadow, x + 5, y + height - 5, x + 10, y + height, 0, 6, 5, 11, null, c);
g.drawImage(shadow, x + 10, y + height - 5, x + width - 5, y + height, 5, 6, 6, 11, null, c);
g.drawImage(shadow, x + width - 5, y + 5, x + width, y + 10, 6, 0, 11, 5, null, c);
g.drawImage(shadow, x + width - 5, y + 10, x + width, y + height - 5, 6, 5, 11, 6, null, c);
g.drawImage(shadow, x + width - 5, y + height - 5, x + width, y + height, 6, 6, 11, 11, null, c);
}
/**
* Returns the insets of the border.
*/
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, SHADOW_SIZE, SHADOW_SIZE);
}
/**
* Reinitializes the insets parameter with this Border"s current Insets.
* @param c the component for which this border insets value applies
* @param insets the object to be reinitialized
* @return the <code>insets</code> object
*/
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.top = 0;
insets.right = insets.bottom = SHADOW_SIZE;
return insets;
}
}
Swing Abstract Border
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.border.AbstractBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.rupoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
public class SwingBorder extends JFrame {
Container frameContainer;
JPanel panel = new JPanel();
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem fileExit = new JMenuItem("Exit");
JMenu borderMenu = new JMenu("Border");
String[] borderTypes = { "Bevel", "Compound", "Empty", "Etched", "Line", "Matte", "SoftBevel",
"Titled" };
JRadioButtonMenuItem[] borders = new JRadioButtonMenuItem[borderTypes.length];
AbstractBorder[] border = { new BevelBorder(BevelBorder.LOWERED),
new CompoundBorder(new LineBorder(Color.blue, 10), new LineBorder(Color.red, 5)),
new EmptyBorder(10, 10, 10, 10), new EtchedBorder(), new LineBorder(Color.blue, 10),
new MatteBorder(new ImageIcon("phone.gif")), new SoftBevelBorder(BevelBorder.RAISED),
new TitledBorder("TitledBorder") };
ButtonGroup buttonGroup = new ButtonGroup();
public SwingBorder() {
super("");
fileMenu.add(fileExit);
for (int i = 0; i < borderTypes.length; ++i) {
borders[i] = new JRadioButtonMenuItem(borderTypes[i]);
buttonGroup.add(borders[i]);
borderMenu.add(borders[i]);
}
menuBar.add(fileMenu);
menuBar.add(borderMenu);
setJMenuBar(menuBar);
frameContainer = getContentPane();
frameContainer.setLayout(new BorderLayout());
frameContainer.add("Center", panel);
setupEventHandlers();
setSize(450, 450);
setVisible(true);
}
void setupEventHandlers() {
addWindowListener(new WindowHandler());
fileExit.addActionListener(new MenuItemHandler());
for (int i = 0; i < borders.length; ++i)
borders[i].addItemListener(new ItemHandler());
}
public static void main(String[] args) {
SwingBorder app = new SwingBorder();
}
public class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public class MenuItemHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Exit"))
System.exit(0);
}
}
public class ItemHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
JRadioButtonMenuItem button = (JRadioButtonMenuItem) e.getItem();
String label = button.getText();
for (int i = 0; i < borderTypes.length; ++i) {
if (label.equals(borderTypes[i])) {
panel.setBorder(border[i]);
repaint();
}
}
}
}
}