如何恢复BasicStroke类中的默认笔划?

时间:2015-06-25 21:28:49

标签: java swing graphics graphics2d

我有一个程序,我在其中定义了一系列新的破折号模式,并使用BasicStroke类使它们成为笔画。比我用过的 .setStroke()方法,并使用我的破折号图案制作线条。现在我想能够setStroke()回到默认的破折号模式(实线)。我该怎么做呢? 这是我的代码:

    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);

1 个答案:

答案 0 :(得分:6)

您可以将其存储在变量中,然后像@StanislavL所说的那样访问它

    Stroke defaultStroke;

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

    defaultStroke = g2d.getStroke();
    //
    //do your thing
    //
    //reset by
    g2d.setStroke(defaultStroke);