package utility.borders; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.border.AbstractBorder; /** * This border consists of dashed lines where you can specify * their thickness, length and colors. You can swap the specified colors * at any time programmatically. * @author Konrad Borowiecki */ public class DashedBorder extends AbstractBorder { private static final long serialVersionUID = 1L; private int thickness; private int dashSize; private Color color1; private Color color2; public DashedBorder(int thickness, int dashSize, Color color1, Color color2) { this.thickness = thickness; this.dashSize = dashSize; this.color1 = color1; this.color2 = color2; } @Override public synchronized void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { Graphics2D g2 = (Graphics2D) g; //size of a step for width int cW = width / dashSize; int restCW = width % dashSize; for(int i = 0; i < dashSize; i++) { int mod1 = cW * i; int mod2 = cW * (i + 1); //if this is the last loop's operation if(i == dashSize - 1) mod2 = mod2 + restCW + 1; if(i % 2 == 0) { g2.setColor(color2); //top g2.fillRect(x + mod1, y, mod2 - mod1, thickness); //bottom g2.fillRect(x + mod1, y + height - thickness, mod2 - mod1, thickness); } if(i % 2 == 1) { g2.setColor(color1); //top g2.fillRect(x + mod1, y, mod2 - mod1, thickness); //bottom g2.fillRect(x + mod1, y + height - thickness, mod2 - mod1, thickness); } } //size of step for height int cH = height / dashSize; int restCH = height % dashSize; for(int i = 0; i < dashSize; i++) { int mod1 = cH * i; int mod2 = cH * (i + 1); //if this is the last loop's operation if(i == dashSize - 1) mod2 = mod2 + restCH + 1; if(i % 2 == 0) { g2.setColor(color1); //left //g2.drawLine(pX, pY + mod1, pX, pY + mod2); g2.fillRect(x, y + mod1, thickness, mod2 - mod1); //right //g2.drawLine(pX + pW, pY + mod1, pX + pW, pY + mod2); g2.fillRect(x + width - thickness, y + mod1, thickness, mod2 - mod1); } if(i % 2 == 1) { g2.setColor(color2); //left g2.fillRect(x, y + mod1, thickness, mod2 - mod1); //right g2.fillRect(x + width - thickness, y + mod1, thickness, mod2 - mod1); } } } /** A helpful function allowing to swap colors after the object was created, * might be useful, for example, to create blinking effect. */ public synchronized void swapColors() { Color color1Temp = color1; Color color2Temp = color2; color1 = color2Temp; color2 = color1Temp; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { final JPanel p = new JPanel(); p.setBackground(Color.WHITE); p.setPreferredSize(new Dimension(200, 200)); final DashedBorder db = new DashedBorder(5, 20, Color.CYAN, Color.BLUE); p.setBorder(db); JPanel contentPane = new JPanel(); contentPane.add(p); JButton b = new JButton("Swap Colors"); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { db.swapColors(); p.repaint(); } }); contentPane.add(b); JFrame f = new JFrame(); f.setContentPane(contentPane); f.setSize(400, 300); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }); } }