---
title: BasicStrokeで点線を作成
tags: [BasicStroke, Graphics2D]
author: aterai
pubdate: 2004-10-04T03:54:35+09:00
description: 破線パターンの配列からBasicStrokeを作成し、これを描画します。
---
* 概要 [#w2f78e6a]
破線パターンの配列から`BasicStroke`を作成し、これを描画します。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTKaxPM12I/AAAAAAAAAV8/ZQON-woHuIg/s800/DashedLine.png)

* サンプルコード [#r8083614]
#code(link){{
JLabel label = new JLabel() {
  BasicStroke dashedStroke;
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    super.paintComponent(g2);
    if (dashedStroke == null) {
      dashedStroke = new BasicStroke(
          5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f,
          getDashArray(), 0f);
    }
    g2.setStroke(dashedStroke);
    g2.drawLine(5, label.getHeight() / 2,
                label.getWidth() - 10, label.getHeight() / 2);
    g2.dispose();
  }
};
}}

* 解説 [#o6e7c935]
`BasicStroke`の破線属性を指定して点線を描画します。

上記のサンプルでは、カンマ区切りで記入した数値を配列に分解し、これを破線のパターンとして`BasicStroke`に渡しています。

* 参考リンク [#q0fcc75d]
- [http://docs.oracle.com/javase/tutorial/2d/geometry/strokeandfill.html Stroking and Filling Graphics Primitives (The Java™ Tutorials > 2D Graphics > Working with Geometry)]

* コメント [#u22a4451]
#comment
#comment