Java by API/javax.swing.border/AbstractBorder — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
extends AbstractBorder
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
public class MainClass {
public static void main(final String args[]) {
JFrame frame = new JFrame("My Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border border = new BlackWhiteBorder();
JButton helloButton = new JButton("Hello");
helloButton.setBorder(border);
JButton braveButton = new JButton("Brave New");
braveButton.setBorder(border);
braveButton.setEnabled(false);
JButton worldButton = new JButton("World");
worldButton.setBorder(border);
Container contentPane = frame.getContentPane();
contentPane.add(helloButton, BorderLayout.NORTH);
contentPane.add(braveButton, BorderLayout.CENTER);
contentPane.add(worldButton, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
class BlackWhiteBorder extends AbstractBorder {
public boolean isBorderOpaque() {
return true;
}
public Insets getBorderInsets(Component c) {
return new Insets(3, 3, 3, 3);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Insets insets = getBorderInsets(c);
Color horizontalColor;
Color verticalColor;
if (c.isEnabled()) {
boolean pressed = false;
if (c instanceof AbstractButton) {
ButtonModel model = ((AbstractButton) c).getModel();
pressed = model.isPressed();
}
if (pressed) {
horizontalColor = Color.WHITE;
verticalColor = Color.BLACK;
} else {
horizontalColor = Color.BLACK;
verticalColor = Color.WHITE;
}
} else {
horizontalColor = Color.LIGHT_GRAY;
verticalColor = Color.LIGHT_GRAY;
}
g.setColor(horizontalColor);
g.translate(x, y);
// top
g.fillRect(0, 0, width, insets.top);
// bottom
g.fillRect(0, height - insets.bottom, width, insets.bottom);
g.setColor(verticalColor);
// left
g.fillRect(0, insets.top, insets.left, height - insets.top - insets.bottom);
// right
g.fillRect(width - insets.right, insets.top, insets.right, height
- insets.top - insets.bottom);
g.translate(-x, -y);
}
}
extends AbstractBorder (CurvedBorder)
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.AbstractBorder;
public class MainClass extends JPanel {
public MainClass() {
super(true);
JSlider mySlider = new JSlider();
mySlider.setMajorTickSpacing(20);
mySlider.setMinorTickSpacing(10);
mySlider.setPaintTicks(true);
mySlider.setPaintLabels(true);
CurvedBorder border = new CurvedBorder(10, Color.darkGray);
mySlider.setBorder(border);
add(mySlider);
}
public static void main(String s[]) {
JFrame frame = new JFrame("Custom Curved Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,150);
frame.setContentPane(new MainClass());
frame.setVisible(true);
}
}
class CurvedBorder extends AbstractBorder
{
private Color wallColor = Color.gray;
private int sinkLevel = 10;
public CurvedBorder() { }
public CurvedBorder(int sinkLevel) { this.sinkLevel = sinkLevel; }
public CurvedBorder(Color wall) { this.wallColor = wall; }
public CurvedBorder(int sinkLevel, Color wall) {
this.sinkLevel = sinkLevel;
this.wallColor = wall;
}
public void paintBorder(Component c, Graphics g, int x, int y,
int w, int h)
{
g.setColor(getWallColor());
// Paint a tall wall around the component
for (int i = 0; i < sinkLevel; i++) {
g.drawRoundRect(x+i, y+i, w-i-1, h-i-1, sinkLevel-i, sinkLevel);
g.drawRoundRect(x+i, y+i, w-i-1, h-i-1, sinkLevel, sinkLevel-i);
g.drawRoundRect(x+i, y, w-i-1, h-1, sinkLevel-i, sinkLevel);
g.drawRoundRect(x, y+i, w-1, h-i-1, sinkLevel, sinkLevel-i);
}
}
public Insets getBorderInsets(Component c) {
return new Insets(sinkLevel, sinkLevel, sinkLevel, sinkLevel);
}
public Insets getBorderInsets(Component c, Insets i) {
i.left = i.right = i.bottom = i.top = sinkLevel;
return i;
}
public boolean isBorderOpaque() { return true; }
public int getSinkLevel() { return sinkLevel; }
public Color getWallColor() { return wallColor; }
}
Subclass AbstractBorder to create Image Border
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.AbstractBorder;
public class ImageBorderHack {
public static void main(String[] args) {
JFrame frame = new JFrame("Hack #59: Image Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JButton button = new JButton("Image Border Test");
panel.add(button);
ImageBorder image_border = new ImageBorder(new ImageIcon("upperLeft.png").getImage(),
new ImageIcon("upper.png").getImage(), new ImageIcon("upperRight.png")
.getImage(),
new ImageIcon("leftCenter.png").getImage(),
new ImageIcon("rightCenter.png").getImage(),
new ImageIcon("bottomLeft.png").getImage(), new ImageIcon(
"bottomCenter.png").getImage(), new ImageIcon("bottomRight.png")
.getImage());
panel.setBorder(image_border);
frame.getContentPane().add(panel);
frame.pack();
// frame.setSize(200,200);
frame.setVisible(true);
}
}
class ImageBorder extends AbstractBorder {
Image topCenterImage, topLeftImage, topRight;
Image leftCenterImage, rightCenterImage;
Image bottomCenterImage, bottomLeftImage, bottomRightImage;
Insets insets;
public ImageBorder(Image top_left, Image top_center, Image top_right, Image left_center,
Image right_center, Image bottom_left, Image bottom_center, Image bottom_right) {
this.topLeftImage = top_left;
this.topCenterImage = top_center;
this.topRight = top_right;
this.leftCenterImage = left_center;
this.rightCenterImage = right_center;
this.bottomLeftImage = bottom_left;
this.bottomCenterImage = bottom_center;
this.bottomRightImage = bottom_right;
}
public void setInsets(Insets insets) {
this.insets = insets;
}
public Insets getBorderInsets(Component c) {
if (insets != null) {
return insets;
} else {
return new Insets(topCenterImage.getHeight(null), leftCenterImage.getWidth(null), bottomCenterImage
.getHeight(null), rightCenterImage.getWidth(null));
}
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
g.setColor(Color.white);
g.fillRect(x, y, width, height);
Graphics2D g2 = (Graphics2D) g;
int tlw = topLeftImage.getWidth(null);
int tlh = topLeftImage.getHeight(null);
int tcw = topCenterImage.getWidth(null);
int tch = topCenterImage.getHeight(null);
int trw = topRight.getWidth(null);
int trh = topRight.getHeight(null);
int lcw = leftCenterImage.getWidth(null);
int lch = leftCenterImage.getHeight(null);
int rcw = rightCenterImage.getWidth(null);
int rch = rightCenterImage.getHeight(null);
int blw = bottomLeftImage.getWidth(null);
int blh = bottomLeftImage.getHeight(null);
int bcw = bottomCenterImage.getWidth(null);
int bch = bottomCenterImage.getHeight(null);
int brw = bottomRightImage.getWidth(null);
int brh = bottomRightImage.getHeight(null);
fillTexture(g2, topLeftImage, x, y, tlw, tlh);
fillTexture(g2, topCenterImage, x + tlw, y, width - tlw - trw, tch);
fillTexture(g2, topRight, x + width - trw, y, trw, trh);
fillTexture(g2, leftCenterImage, x, y + tlh, lcw, height - tlh - blh);
fillTexture(g2, rightCenterImage, x + width - rcw, y + trh, rcw, height - trh - brh);
fillTexture(g2, bottomLeftImage, x, y + height - blh, blw, blh);
fillTexture(g2, bottomCenterImage, x + blw, y + height - bch, width - blw - brw, bch);
fillTexture(g2, bottomRightImage, x + width - brw, y + height - brh, brw, brh);
}
public void fillTexture(Graphics2D g2, Image img, int x, int y, int w, int h) {
BufferedImage buff = createBufferedImage(img);
Rectangle anchor = new Rectangle(x, y, img.getWidth(null), img.getHeight(null));
TexturePaint paint = new TexturePaint(buff, anchor);
g2.setPaint(paint);
g2.fillRect(x, y, w, h);
}
public BufferedImage createBufferedImage(Image img) {
BufferedImage buff = new BufferedImage(img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
Graphics gfx = buff.createGraphics();
gfx.drawImage(img, 0, 0, null);
gfx.dispose();
return buff;
}
}