Search code examples
javaswingdrawrectgraphics2dpaintcomponent

How to create a rectangle in a rectangle?


In my paintComponent, I have drawRect, which draws a single rectangle. However, I want to make the outline of the rectangle thicker but I don't know how. So I thought of making another rectangle inside the existing one. I tried putting another drawRect but the rectangle isn't in the center.

Thanks for those who'll help!


Solution

  • g2d.setStroke(new BasicStroke(6));
    

    The argument passed to the paintComponent(Graphics) method of a Swing component should actually be a Graphics2D instance. It can be cast to one.

    See this example in which 3 strokes are layered.

    Stroke It

    import javax.swing.*;
    import java.awt.*;
    
    class StrokeIt {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    StrokePanel sp = new StrokePanel();
                    sp.setPreferredSize(new Dimension(400,100));
                    sp.setBackground(Color.BLUE);
                    JOptionPane.showMessageDialog(null, sp);
                }
            });
        }
    }
    
    class StrokePanel extends JPanel {
    
        int pad = 12;
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
    
            g2d.setColor(Color.RED);
            g2d.setStroke(new BasicStroke(10));
            g2d.drawRect( 0+pad, 0+pad,
                getWidth()-(2*pad), getHeight()-(2*pad) );
    
            g2d.setColor(Color.YELLOW);
            g2d.setStroke(new BasicStroke(6));
            g2d.drawRect( 0+pad, 0+pad,
                getWidth()-(2*pad), getHeight()-(2*pad) );
    
            g2d.setColor(Color.ORANGE);
            g2d.setStroke(new BasicStroke(2));
            g2d.drawRect( 0+pad, 0+pad,
                getWidth()-(2*pad), getHeight()-(2*pad) );
        }
    }