角丸
2006/12/02
2008/11/14
java
swingx
角丸
スティーブ・ジョブズ氏は角丸四角形を基本図形に含めることを激しく主張していたvia オレンジニュース
意識していなかったのですが、角丸はあちらこちらでみかけますね。
もっと手軽に角丸を使いたいのですが、なかなか手軽にはいかない部分もあります。
webのデザインでもまだCSSにコーナー用の gif画像を用意したりであったりと 、手間がかかるようです。 javaアプリケーションでもまだ手軽というわけではなさそうですが、 swingxのライブラリを使用すると手軽に表現できそうです。
swingx
サンプルコードはorg.jdesktop.swingx.painterを利用して角丸画像を手軽に作成する方法です。上記の画像を作成します。パラメータを与える入力を部分を作成工夫すればいろいろな角丸な画像をつくれそうですね。
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestMainForOutRoundPanel extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private RoundPanel roundPanel = null;
private JPanel jPanel = null;
private JButton jButton = null;
private RoundPanel getRoundPanel() {
if (roundPanel == null) {
roundPanel = new RoundPanel();
}
return roundPanel;
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getJButton(), new GridBagConstraints());
}
return jPanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("out");
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedImage bufferedImage = new BufferedImage(roundPanel.getWidth(),
roundPanel.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = bufferedImage.createGraphics();
roundPanel.paintComponent2(graphic);
try {
ImageIO.write(
bufferedImage,
"png",
new File("filename.png"));
} catch (Exception ee) {
ee.printStackTrace();
}
}
});
}
return jButton;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestMainForOutRoundPanel thisClass = new TestMainForOutRoundPanel();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public TestMainForOutRoundPanel() {
super();
initialize();
}
private void initialize() {
this.setSize(474, 343);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getRoundPanel(), BorderLayout.CENTER);
jContentPane.add(getJPanel(), BorderLayout.SOUTH);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestMainForOutRoundPanel extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private RoundPanel roundPanel = null;
private JPanel jPanel = null;
private JButton jButton = null;
private RoundPanel getRoundPanel() {
if (roundPanel == null) {
roundPanel = new RoundPanel();
}
return roundPanel;
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(getJButton(), new GridBagConstraints());
}
return jPanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("out");
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BufferedImage bufferedImage = new BufferedImage(roundPanel.getWidth(),
roundPanel.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = bufferedImage.createGraphics();
roundPanel.paintComponent2(graphic);
try {
ImageIO.write(
bufferedImage,
"png",
new File("filename.png"));
} catch (Exception ee) {
ee.printStackTrace();
}
}
});
}
return jButton;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestMainForOutRoundPanel thisClass = new TestMainForOutRoundPanel();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public TestMainForOutRoundPanel() {
super();
initialize();
}
private void initialize() {
this.setSize(474, 343);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getRoundPanel(), BorderLayout.CENTER);
jContentPane.add(getJPanel(), BorderLayout.SOUTH);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.RenderingHints;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.RectanglePainter;
public class RoundPanel extends JXPanel {
public RoundPanel() {
RectanglePainter roundRect = new RectanglePainter(10,
10,
10,
10,
37,
37,
true,
Color.black,
10,
Color.ORANGE);
roundRect.setAntialiasing(RenderingHints.VALUE_ANTIALIAS_ON);
CompoundPainter compound = new CompoundPainter(roundRect);
setBackgroundPainter(compound);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension dimension = this.getSize();
int w = (int) (dimension.getWidth() / 2);
int h = (int) (dimension.getHeight() / 2);
Font font = new Font("", 0, 32);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("角丸(round corner)", 18, h);
}
public void paintComponent2(Graphics graphics) {
paintComponent(graphics);
}
}
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.RenderingHints;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.painter.CompoundPainter;
import org.jdesktop.swingx.painter.RectanglePainter;
public class RoundPanel extends JXPanel {
public RoundPanel() {
RectanglePainter roundRect = new RectanglePainter(10,
10,
10,
10,
37,
37,
true,
Color.black,
10,
Color.ORANGE);
roundRect.setAntialiasing(RenderingHints.VALUE_ANTIALIAS_ON);
CompoundPainter compound = new CompoundPainter(roundRect);
setBackgroundPainter(compound);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension dimension = this.getSize();
int w = (int) (dimension.getWidth() / 2);
int h = (int) (dimension.getHeight() / 2);
Font font = new Font("", 0, 32);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("角丸(round corner)", 18, h);
}
public void paintComponent2(Graphics graphics) {
paintComponent(graphics);
}
}
: