Java 如何平滑JFrame形状
我给我的JFrame窗口提供了一个圆角自定义形状,但如何平滑它(抗锯齿)很多问题将归结于您如何呈现内容,但基本概念是为您要绘制的Java 如何平滑JFrame形状,java,shape,Java,Shape,我给我的JFrame窗口提供了一个圆角自定义形状,但如何平滑它(抗锯齿)很多问题将归结于您如何呈现内容,但基本概念是为您要绘制的图形上下文提供呈现提示 例如,如果我正在绘制一个组件,我可能会使用类似于 // Create a "copy" of the graphics context so we don't modify any of it's existing // settings. This makes it easier to manage the graphics context
图形
上下文提供呈现提示
例如,如果我正在绘制一个组件,我可能会使用类似于
// Create a "copy" of the graphics context so we don't modify any of it's existing
// settings. This makes it easier to manage the graphics context as we
// may not want to effect anything else that might be using this graphics context
// into the future
Graphics2D g2d = (Graphics2D)g.create();
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON
);
g2d.setRenderingHints(hints);
//... continue drawing.
// Dispose of our "copy" of the graphics context
g2d.dispose();
查看更多详细信息
用示例更新
public class AATest {
public static void main(String[] args) {
new AATest();
}
public AATest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(215, 110);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
g2d.setColor(Color.RED);
drawShape(g2d, 5, 5);
g2d.dispose();
g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
drawShape(g2d, 110, 5);
g2d.dispose();
}
protected void drawShape(Graphics2D g2d, int x, int y) {
g2d.draw(new Ellipse2D.Float(x, y, 100, 100));
}
}
}
使用新示例更新
public class AATest {
public static void main(String[] args) {
new AATest();
}
public AATest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(215, 110);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
g2d.setColor(Color.RED);
drawShape(g2d, 5, 5);
g2d.dispose();
g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
drawShape(g2d, 110, 5);
g2d.dispose();
}
protected void drawShape(Graphics2D g2d, int x, int y) {
g2d.draw(new Ellipse2D.Float(x, y, 100, 100));
}
}
}
我使用的技巧之一是,我不使用“setShape”,而是简单地制作一个透明窗口,并使用自定义面板来提供我想要使用的形状
这方面的主要问题是,您现在要负责确保在形状内部绘制内容
public class ShapedWindow {
public static void main(String[] args) {
new ShapedWindow();
}
public ShapedWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JWindow frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));
frame.setContentPane(new ShapedPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
}
});
}
public class ShapedPane extends JPanel {
public ShapedPane() {
setOpaque(false);
setLayout(new GridBagLayout());
JButton button = new JButton("Close");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
add(button);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
g2d.setColor(getBackground());
g2d.fill(new Ellipse2D.Float(0, 0, getWidth(), getHeight()));
g2d.dispose();
}
}
}
但是,你将如何保存该形状?你说的“保存该形状”是什么意思?我必须先制作一个抗锯齿形状,然后说“设置形状”(形状)。但是,如果你不能保存一个图形2D绘制的形状,我该如何设置形状呢<代码>形状是一个抽象/虚拟的概念,可以呈现到
图形
上下文中。您可以影响图形上下文的配置方式。这就是它的工作原理您显然不了解在Swing/Java中绘画是如何工作的。通读一下,你可以试着设置每像素的透明度,如下所示:你是什么意思。创建我自己的抗锯齿?您的问题是形状的边缘与背景的连接过于尖锐。你的形状可能看起来很参差不齐。如果您慢慢淡出该形状,它将看起来不那么锯齿状。像素离边界越近,它应该越半透明。但是你知道一种制作抗锯齿形状的方法吗?如果对你来说没有意义,你也可以尝试以下方法: