• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:BasicStrokeで点線を作成
#navi(../)
#tags(BasicStroke, Graphics2D)
RIGHT:Posted by &author(aterai); at 2004-10-04
* BasicStrokeで点線を作成 [#w2f78e6a]
点線・破線を描画します。
---
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`を作成し、これを描画します。

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

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

** サンプルコード [#r8083614]
* サンプルコード [#sourcecode]
#code(link){{
JLabel label = new JLabel() {
  BasicStroke dashed2;
  private BasicStroke dashedStroke;
  @Override protected void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    super.paintComponent(g2);
    if(dashed2==null) dashed2 = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
                  BasicStroke.JOIN_MITER, 10.0f, getDashArray(), 0.0f);
    g2.setStroke(dashed2);
    g2.drawLine(5, getHeight()/2, getWidth()-10, getHeight()/2);
    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();
  }
};
}}

** 解説 [#o6e7c935]
``BasicStroke``の破線属性を指定して点線を描画します。
* 解説 [#explanation]
上記のサンプルでは、`BasicStroke`の破線属性を指定して点線をコンポーネント内に描画しています。

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

** 参考リンク [#q0fcc75d]
- [http://java.sun.com/docs/books/tutorial/2d/geometry/strokeandfill.html Stroking and Filling Graphics Primitives]
* 参考リンク [#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)]

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