我想为JPanel创建自定义形状,这是一个如何塑造JPanel的示例:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FunkyShapedComponent {
public static void main(String[] args) {
new FunkyShapedComponent();
}
public FunkyShapedComponent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JPanel testPane = new JPanel();
testPane.setBackground(Color.RED);
testPane.setLayout(new GridBagLayout());
testPane.add(new FunkyPane());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(testPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FunkyPane extends JPanel {
public FunkyPane() {
setLayout(new GridBagLayout());
add(new JLabel("This is a simple test"));
setOpaque(false);
}
@Override
public Insets getInsets() {
return new Insets(10, 10, 10, 10);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth() - 1;
int height = getHeight() - 1;
int radius = Math.min(width, height) / 10;
Path2D p = new Path2D.Float();
p.moveTo(0, radius / 2);
p.curveTo(0, 0, 0, 0, radius / 2, 0);
p.curveTo(width / 4, radius, width / 4, radius, (width / 2) - radius, radius / 2);
p.curveTo(width / 2, 0, width / 2, 0, (width / 2) + radius, radius / 2);
p.curveTo(width - (width / 4), radius, width - (width / 4), radius, width - (radius / 2), 0);
p.curveTo(width, 0, width, 0, width, radius / 2);
p.curveTo(width - radius, height / 4, width - radius, height / 4, width - (radius / 2), (height / 2) - radius);
p.curveTo(width, height / 2, width, height / 2, width - (radius / 2), (height / 2) + radius);
p.curveTo(width - radius, height - (height / 4), width - radius, height - (height / 4), width, height - (radius / 2));
p.curveTo(width, height, width, height, width - (radius / 2), height);
p.curveTo(width - (width / 4), height - radius, width - (width / 4), height - radius, (width / 2) + radius, height - (radius / 2));
p.curveTo(width / 2, height, width / 2, height, (width / 2) - radius, height - (radius / 2));
p.curveTo((width / 4), height - radius, (width / 4), height - radius, (radius / 2), height);
p.curveTo(0, height, 0, height, 0, height - (radius / 2));
p.curveTo(radius, height - (height / 4), radius, height - (height / 4), (radius / 2), (height / 2) + radius);
p.curveTo(0, height / 2, 0, height / 2, (radius / 2), (height / 2) - radius);
p.curveTo(radius, (height / 4), radius, (height / 4), 0, (radius / 2));
p.closePath();
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(getBackground());
g2d.fill(p);
g2d.dispose();
}
}
}
这就是它的作用:
这就是我想要的形状:
我想要的是向具有该形状的面板添加组件,但我不知道如何管理path2D,如果有更好的方法,请告诉我
答案 0 :(得分:0)
我个人不会试图改变JPanel,它本质上是保留其他所有东西而不是你应该改变的东西。相反,我会根据您的需要创建自定义形状。我假设你希望形状隐藏其他东西;因为你最初使用的是JPanel,所以我最后会画出形状;这会产生预期的效果。
以下是您可以做的事情: 画出任意形状; Java不直接支持自定义形状 以下链接可解释所有内容: http://docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html
我希望这有助于:)
*只是让您知道示例代码只会导致图像下载。
答案 1 :(得分:0)
请参阅How to Create Translucent and Shaped Windows
要创建该形状,您可以使用两个椭圆屏蔽矩形的顶部和底部。示例here。
我倾向于使用JavaFX,例如this。
您的代码链接指向图片链接。