| package test.swing; |
| |
| import javax.swing.*; |
| import javax.swing.border.AbstractBorder; |
| import java.awt.*; |
| |
| |
| public class MainTest { |
| public static void main(String[] args) { |
| MainUI mainUI = new MainUI(); |
| mainUI.setVisible(true); |
| } |
| } |
| |
| class CustomLineBorder extends AbstractBorder |
| { |
| public static final int TOP = 0x1; |
| public static final int LEFT = 0x2; |
| public static final int BOTTOM = 0x4; |
| public static final int RIGHT = 0x8; |
| private Color mColor; |
| private int mThickness; |
| private int mTarget; |
| |
| public CustomLineBorder(Color colour, int thickness, int target) |
| { |
| mColor = colour; |
| mThickness = thickness; |
| mTarget = target; |
| } |
| |
| @Override |
| public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) |
| { |
| if (g != null) |
| { |
| g.setColor(mColor); |
| if ((mTarget & TOP) != 0) { |
| for (int i = 0; i < mThickness; i++) { |
| g.drawLine(x, y + i, width, y + i); |
| } |
| } |
| if ((mTarget & LEFT) != 0) { |
| for (int i = 0; i < mThickness; i++) { |
| g.drawLine(x + i, y, x + i, height); |
| } |
| } |
| if ((mTarget & BOTTOM) != 0) { |
| for (int i = 0; i < mThickness; i++) { |
| g.drawLine(x, height - i - 1, width, height - i - 1); |
| } |
| } |
| if ((mTarget & RIGHT) != 0) { |
| for (int i = 0; i < mThickness; i++) { |
| g.drawLine(width - i - 1, y, width - i - 1, height); |
| } |
| } |
| } |
| } |
| |
| @Override |
| public Insets getBorderInsets(Component c) |
| { |
| return (getBorderInsets(c, new Insets(0, 0, 0, 0))); |
| } |
| |
| @Override |
| public Insets getBorderInsets(Component c, Insets insets) |
| { |
| insets.top = 0; |
| insets.left = 0; |
| insets.bottom = 0; |
| insets.right = 0; |
| if ((mTarget & TOP) != 0) { |
| insets.top = mThickness; |
| } |
| if ((mTarget & LEFT) != 0) { |
| insets.left = mThickness; |
| } |
| if ((mTarget & BOTTOM) != 0) { |
| insets.bottom = mThickness; |
| } |
| if ((mTarget & RIGHT) != 0) { |
| insets.right = mThickness; |
| } |
| |
| return insets; |
| } |
| |
| @Override |
| public boolean isBorderOpaque() |
| { |
| return true; |
| } |
| } |
| |
| class MainUI extends JFrame { |
| MainUI() { |
| setPreferredSize(new Dimension(400, 420)); |
| |
| JPanel pane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 20)); |
| pane.setBackground(new Color(0x85, 0x85, 0x85)); |
| |
| JLabel label = new JLabel("Red, 1, TOP"); |
| label.setPreferredSize(new Dimension(300, 40)); |
| label.setOpaque(true); |
| label.setBackground(Color.WHITE); |
| label.setBorder(new CustomLineBorder(Color.RED, 1, CustomLineBorder.TOP)); |
| pane.add(label); |
| |
| label = new JLabel("Red, 5, LEFT"); |
| label.setPreferredSize(new Dimension(300, 40)); |
| label.setOpaque(true); |
| label.setBackground(Color.WHITE); |
| label.setBorder(new CustomLineBorder(Color.RED, 5, CustomLineBorder.LEFT)); |
| pane.add(label); |
| |
| label = new JLabel("Red, 5, Bottom"); |
| label.setPreferredSize(new Dimension(300, 40)); |
| label.setOpaque(true); |
| label.setBackground(Color.WHITE); |
| label.setBorder(new CustomLineBorder(Color.RED, 5, CustomLineBorder.BOTTOM)); |
| pane.add(label); |
| |
| label = new JLabel("Red, 15, Right"); |
| label.setPreferredSize(new Dimension(300, 40)); |
| label.setOpaque(true); |
| label.setBackground(Color.WHITE); |
| label.setBorder(new CustomLineBorder(Color.RED, 15, CustomLineBorder.RIGHT)); |
| pane.add(label); |
| |
| label = new JLabel("Blue, 5, TOP LEFT"); |
| label.setPreferredSize(new Dimension(300, 40)); |
| label.setOpaque(true); |
| label.setBackground(Color.WHITE); |
| label.setBorder(new CustomLineBorder(Color.BLUE, 5, CustomLineBorder.LEFT | CustomLineBorder.TOP)); |
| pane.add(label); |
| |
| label = new JLabel("Blue, 5, TOP RIGHT BOTTOM"); |
| label.setPreferredSize(new Dimension(300, 40)); |
| label.setOpaque(true); |
| label.setBackground(Color.WHITE); |
| label.setBorder(new CustomLineBorder(Color.BLUE, 5, CustomLineBorder.TOP | CustomLineBorder.RIGHT | CustomLineBorder.BOTTOM)); |
| pane.add(label); |
| |
| add(pane); |
| |
| pack(); |
| } |
| } |