Created
July 5, 2017 13:07
-
-
Save aterai/249d41a625ffbbf2c91729a6bc504fd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import java.awt.geom.*; | |
import java.awt.image.*; | |
import java.util.*; | |
import javax.swing.*; | |
import javax.swing.border.*; | |
public class RoundedBorderTest { | |
public JComponent makeUI() { | |
JTextField textField01 = new JTextField("aaaaaaaaaaaaaaa"); | |
textField01.setBorder(new RoundedBorder(10)); | |
textField01.setOpaque(false); | |
JTextField textField02 = new JTextField("bbbbbbbbbbbbbbb") { | |
@Override protected void paintComponent(Graphics g) { | |
Border border = getBorder(); | |
if (!isOpaque() && border instanceof RoundedBorder) { | |
Graphics2D g2 = (Graphics2D) g.create(); | |
g2.setPaint(getBackground()); | |
g2.fill(((RoundedBorder) border).getBorderShape(0, 0, getWidth() - 1, getHeight() - 1)); | |
g2.dispose(); | |
} | |
super.paintComponent(g); | |
} | |
}; | |
textField02.setOpaque(false); | |
textField02.setBorder(new RoundedBorder(10)); | |
JPanel p = new JPanel(new GridLayout(0, 1, 10, 10)); | |
p.setOpaque(false); | |
p.add(textField01); | |
p.add(textField02); | |
TexturePaint texture = makeCheckerTexture(); | |
JPanel pp = new JPanel(new BorderLayout()) { | |
@Override protected void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
if (!isOpaque()) { | |
Graphics2D g2 = (Graphics2D) g.create(); | |
g2.setPaint(texture); | |
g2.fillRect(0, 0, getWidth(), getHeight()); | |
g2.dispose(); | |
} | |
} | |
}; | |
pp.setOpaque(false); | |
pp.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); | |
pp.add(p, BorderLayout.NORTH); | |
return pp; | |
} | |
private static TexturePaint makeCheckerTexture() { | |
int cs = 6; | |
int sz = cs * cs; | |
BufferedImage img = new BufferedImage(sz, sz, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D g2 = img.createGraphics(); | |
g2.setPaint(new Color(100, 100, 100, 50)); | |
g2.fillRect(0, 0, sz, sz); | |
for (int i = 0; i * cs < sz; i++) { | |
for (int j = 0; j * cs < sz; j++) { | |
if ((i + j) % 2 == 0) { | |
g2.fillRect(i * cs, j * cs, cs, cs); | |
} | |
} | |
} | |
g2.dispose(); | |
return new TexturePaint(img, new Rectangle(sz, sz)); | |
} | |
public static void main(String[] args) { | |
EventQueue.invokeLater(() -> { | |
JFrame frame = new JFrame(); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.getContentPane().add(new RoundedBorderTest().makeUI()); | |
frame.setSize(320, 240); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
}); | |
} | |
} | |
class RoundedBorder implements Border { | |
int radius; | |
RoundedBorder(int radius) { | |
this.radius = radius; | |
} | |
public Insets getBorderInsets(Component c) { | |
return new Insets(this.radius + 1, this.radius + 1, this.radius + 2, this.radius); | |
} | |
public boolean isBorderOpaque() { | |
return false; | |
} | |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { | |
Graphics2D g2d = (Graphics2D) g.create(); | |
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); | |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); | |
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); | |
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); | |
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | |
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); | |
g2d.setColor(Color.BLACK); | |
//g2d.drawRoundRect(x, y, width - 1, height - 1, radius + 2, radius + 2); | |
g2d.draw(getBorderShape(x, y, width - 1, height - 1)); | |
g2d.dispose(); | |
} | |
//<ins> | |
public Shape getBorderShape(int x, int y, int w, int h) { | |
return new RoundRectangle2D.Double(x, y, w, h, radius + 2, radius + 2); | |
} | |
//</ins> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment