I am having problems setting the thickness of my border. I wanted to have a JPanel
with a rounded dashed border. I was able to set it rounded by overriding the paintComponent
to make it round. However, when I set the stroke to make the border thicker and dashed it does not work. I use the setStroke()
method. My code is the following
private void loadTopPane() {
JPanel topSection = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension arcs = new Dimension(15, 15); // Border corners arcs
// {width,height},
// change this to
// whatever you want
int width = getWidth();
int height = getHeight();
Graphics2D graphics = (Graphics2D) g;
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
float dash1[] = { 10.0f };
final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f,
dash1, 0.0f);
// Draws the rounded panel with borders.
graphics.setColor(getBackground());
graphics.fillRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
// background
graphics.setColor(getForeground());
graphics.drawRoundRect(0, 0, width - 1, height - 1, arcs.width, arcs.height);// paint
// border
graphics.setStroke(dashed);
}
};
topSection.setLayout(null);
topSection.setSize(1150, 175);
topSection.setBackground(new Color(222, 225, 226));
topSection.setBounds(25, 13, topSection.getPreferredSize().width, topSection.getPreferredSize().height);
topSection.add(new JLabel("TESTING"));
topSection.setBounds(20, 10, 1180, 180);
frame.add(topSection);
}
So the output shows me a JPanel
with a rounded border but it does not give me a border that is dashed and thicker. How can I fix this?