• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*BasicStrokeで点線を作成 [#w2f78e6a]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-10-04~
更新日:&lastmod;
---
category: swing
folder: DashedLine
title: BasicStrokeで点線を作成
tags: [BasicStroke, Graphics2D]
author: aterai
pubdate: 2004-10-04T03:54:35+09:00
description: 破線パターンの配列からBasicStrokeを作成し、これを描画します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTKaxPM12I/AAAAAAAAAV8/ZQON-woHuIg/s800/DashedLine.png
---
* 概要 [#summary]
破線パターンの配列から`BasicStroke`を作成し、これを描画します。

#contents
**概要 [#l979ab53]
点線・破線を描画します。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTKaxPM12I/AAAAAAAAAV8/ZQON-woHuIg/s800/DashedLine.png)

http://terai.xrea.jp/swing/dashedline/screenshot.png
* サンプルコード [#sourcecode]
#code(link){{
JLabel label = new JLabel() {
  private BasicStroke dashedStroke;
  @Override protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (dashedStroke == null) {
      dashedStroke = new BasicStroke(
          5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f,
          getDashArray(), 0f);
    }
    Insets i = getInsets();
    int w = getWidth();
    int h = getHeight() / 2;
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setStroke(dashedStroke);
    g2.drawLine(i.left, h, w - i.right, h);
    g2.dispose();
  }
};
}}

**サンプルコード [#r8083614]
 JLabel label = new JLabel() {
   BasicStroke dashed2 = new BasicStroke();
   public void paintComponent(Graphics g) {
     Graphics2D g2 = (Graphics2D)g;
     super.paintComponent(g2);
     if(flag) dashed2 = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
                   icStroke.JOIN_MITER, 10.0f, getDashArray(), 0.0f);
     flag = false;
     g2.setStroke(dashed2);
     g2.drawLine(5, getHeight()/2, getWidth()-10, getHeight()/2);
   }
 };
* 解説 [#explanation]
上記のサンプルでは、`BasicStroke`の破線属性を指定して点線をコンポーネント内に描画しています。

-[[サンプルを起動>http://terai.xrea.jp/swing/dashedline/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/dashedline/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/dashedline/src.zip]]
**解説 [#o6e7c935]
BasicStrokeの破線属性を指定して点線を描画します。
- 破線のパターンは`JTextField`にカンマ区切りで記入した数値を配列に分解し、これを`BasicStroke`に渡して作成

上記のサンプルでは、カンマ区切りで記入した数値を配列に分解し、これを破線のパターンとしてBasicStrokeに渡しています。
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/BasicStroke.html BasicStroke (Java Platform SE 8)]
- [https://docs.oracle.com/javase/tutorial/2d/geometry/strokeandfill.html Stroking and Filling Graphics Primitives (The Java™ Tutorials > 2D Graphics > Working with Geometry)]

**参考リンク [#q0fcc75d]
-[[Stroking and Filling Graphics Primitives>http://java.sun.com/docs/books/tutorial/2d/display/strokeandfill.html]]
**コメント [#u22a4451]
* コメント [#comment]
#comment
#comment