Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore the default stroke in the BasicStroke class?

I have a program where I defined a bunch of new dash patterns and made them strokes using the BasicStroke class. Than I used the .setStroke() method and made lines with my dash patterns. Now I want to be able to setStroke() back to the default dash pattern (a solid line). How do I do this? Here is my code:

    float[] dash1 = {2f, 0f, 2f};
        float[] dash2 = {1f, 1f, 1f};
        float[] dash3 = {16f, 10f, 4f, 10f};
        float[] dash4 = {4f, 4f, 1f};

        Graphics2D g2d = (Graphics2D) g.create();
        g2d.drawLine(20, 40, 250, 40);

        BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1.0f, dash3, 0f);

        BasicStroke bs2 =new BasicStroke(1, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1.0f, dash3, 16f);

        BasicStroke bs3 = new BasicStroke(1, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1.0f, dash3, 2f);

        BasicStroke bs4 = new BasicStroke(1, BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_ROUND, 1.0f, dash4, 2f);



        g2d.setStroke(bs1);
        g2d.drawLine(20, 80, 250, 80);

        g2d.setStroke(bs2);
        g2d.drawLine(20, 120, 250, 120);

        g2d.setStroke(bs3);
        g2d.drawLine(20, 160, 250, 160);

        g2d.setStroke(bs4);
        g2d.drawLine(20, 200, 250, 200);
like image 423
Ashwin Gupta Avatar asked Mar 15 '23 14:03

Ashwin Gupta


1 Answers

You can store it in a variable and access it later like @StanislavL said

    Stroke defaultStroke;

    Graphics2D g2d = (Graphics2D) g.create();

    defaultStroke = g2d.getStroke();
    //
    //do your thing
    //
    //reset by
    g2d.setStroke(defaultStroke);  
like image 187
Madhan Avatar answered Apr 27 '23 00:04

Madhan