Swing/RoundedComboBox のバックアップの現在との差分(No.5)
TITLE:JComboBoxの角を丸める
Posted by aterai at 2012-04-23
JComboBoxの角を丸める
`JComboBox
の左上、右上の角を丸める
Border
`を設定します。
-
category: swing
folder: RoundedComboBox
title: JComboBoxの角を丸める
tags: [JComboBox, Border, Path2D, ArrowButton]
author: aterai
pubdate: 2012-04-23T11:59:28+09:00
description: JComboBoxの左上、右上の角を丸めるBorderを設定します。
image:
hreflang:
href: https://java-swing-tips.blogspot.com/2012/07/rounded-corner-jcombobox-border.html lang: en
概要
JComboBox
の左上、右上の角を丸めるBorder
を設定します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
サンプルコード
class RoundedCornerBorder extends AbstractBorder {
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int r = 12;
int w = width - 1;
int h = height - 1;
Area round = new Area(new RoundRectangle2D.Float(x, y, w, h, r, r));
Container parent = c.getParent();
if(parent!=null) {
if (parent != null) {
g2.setColor(parent.getBackground());
Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
corner.subtract(round);
g2.fill(corner);
}
g2.setPaint(c.getForeground());
g2.draw(round);
g2.dispose();
}
#spanadd
#spanend
@Override public Insets getBorderInsets(Component c) {
return new Insets(4, 8, 4, 8);
}
#spanadd
#spanend
@Override public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.right = 8;
insets.top = insets.bottom = 4;
return insets;
}
}
class KamabokoBorder extends RoundedCornerBorder {
@Override public void paintBorder(
Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int r = 12;
int w = width - 1;
int h = height - 1;
Path2D.Float p = new Path2D.Float();
Path2D p = new Path2D.Float();
p.moveTo(x, y + h);
p.lineTo(x, y + r);
p.quadTo(x, y, x + r, y);
p.lineTo(x + w - r, y);
p.quadTo(x + w, y, x + w, y + r);
p.lineTo(x + w, y + h);
p.closePath();
Area round = new Area(p);
Container parent = c.getParent();
if(parent!=null) {
if (parent != null) {
g2.setColor(parent.getBackground());
Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
corner.subtract(round);
g2.fill(corner);
}
g2.setPaint(c.getForeground());
g2.draw(round);
g2.dispose();
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、`Path2D#lineTo
、
Path2D#quadTo
を使ってかまぼこ型の図形を作成し、
JComboBoxのBorder
`に設定しています。
解説
上記のサンプルでは、Path2D#lineTo(...)
、Path2D#quadTo(...)
メソッドを使用してかまぼこ型の図形を作成し、JComboBox
のBorder
として設定しています。
以下のような方法でも、かまぼこ型の図形を作成することができます。
- 上:
MetalComboBoxUI
-
BasicComboBoxUI
を設定
-
- 中:
BasicComboBoxUI
-
RoundRectangle2D
に下の角を上書きするような矩形を追加して作成 -
UIManager.put("ComboBox.foreground", color)
などでJComboBox
の色を変更しているが、これだけではArrowButton
の色が変更できない - JComboBoxのBorderを変更する
-
- 下:
BasicComboBoxUI#createArrowButton()
-
BasicComboBoxUI#createArrowButton()
をオーバーライドしてArrowButton
の背景色などを変更
-
- -
- 上記の方法で角丸の
JComboBox
が作成可能なのはBasicLookAndFeel
とWindowsLookAndFeel
のみ? - 以下のような方法でもかまぼこ型の図形を作成可能
#spandel Area round = new Area(new RoundRectangle2D.Float(x, y, width-1, height-1, r, r)); #spanend #spanadd Area round = new Area( #spanend new RoundRectangle2D.Float(x, y, width - 1, height - 1, r, r)); Rectangle b = round.getBounds(); b.setBounds(b.x, b.y + r, b.width, b.height - r); round.add(new Area(b));
- 上: `
MetalComboBoxUI
`- `
BasicComboBoxUI
`を設定
- `
参考リンク
- 中: `
BasicComboBoxUI
`- `
RoundRectangle2D
` に、下の角を上書きするような矩形を追加して作成 - `
UIManager.put("ComboBox.foreground", color)
などで、
JComboBox
の色を変更しているが、
ArrowButton
`の色がうまく変更できない - JComboBoxのBorderを変更する
- `
コメント
- 編集可不可の切り替えと、
WindowsLookAndFeel
に適用したサンプルを追加 -- aterai
- 下: `
BasicComboBoxUI#createArrowButton()
`- `
BasicComboBoxUI#createArrowButton()
をオーバーライドして、
ArrowButton
`の背景色などを変更
- `
- -
メモ: これらの方法で、角丸の`
JComboBox
が作成できるのは、
BasicLookAndFeel
と
WindowsLookAndFeel
`の場合のみ?
参考リンク
コメント
- 編集可不可の切り替えと、`
WindowsLnF
`に適用したサンプルを追加 -- aterai